Class: FulfilApi::TplClient
- Inherits:
-
Object
- Object
- FulfilApi::TplClient
- Defined in:
- lib/fulfil_api/tpl_client.rb
Overview
The TplClient allows making proxy requests to Fulfil’s 3PL carrier API endpoint. It provides a simple interface for interacting with the 3PL supplier API using standard HTTP methods.
Defined Under Namespace
Classes: ConfigurationError
Constant Summary collapse
- DEFAULT_API_VERSION =
"v1"
Class Method Summary collapse
-
.connection_for(cache_key) ⇒ Faraday::Connection
Looks up a memoized Faraday::Connection for the given cache key, or builds and stores one by yielding the block.
-
.reset_connection_cache! ⇒ void
Clears the memoized connection cache.
Instance Method Summary collapse
-
#get(relative_path, **url_parameters) ⇒ Array, ...
Performs an HTTP GET request to a 3PL API endpoint.
-
#initialize(configuration) ⇒ TplClient
constructor
A new instance of TplClient.
-
#patch(relative_path, body = {}) ⇒ Array, ...
Performs an HTTP PATCH request to a 3PL API endpoint.
-
#post(relative_path, body = {}) ⇒ Array, ...
Performs an HTTP POST request to a 3PL API endpoint.
-
#put(relative_path, body = nil) ⇒ Array, ...
Performs an HTTP PUT request to a 3PL API endpoint.
Constructor Details
#initialize(configuration) ⇒ TplClient
Returns a new instance of TplClient.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fulfil_api/tpl_client.rb', line 50 def initialize(configuration) @configuration = configuration tpl_config = configuration.tpl || {} @auth_token = tpl_config[:auth_token].presence || raise(ConfigurationError, "Please provide a 3PL authentication token via config.tpl = { auth_token: ... }") @merchant_id = tpl_config[:merchant_id].presence || configuration.merchant_id.presence || raise(ConfigurationError, "Please provide a merchant ID") @api_version = tpl_config[:api_version].presence || DEFAULT_API_VERSION end |
Class Method Details
.connection_for(cache_key) ⇒ Faraday::Connection
Looks up a memoized Faraday::Connection for the given cache key, or
builds and stores one by yielding the block.
The cache is process-wide, so the underlying ‘net_http_persistent` adapter
can actually reuse its TCP/TLS connection pool across requests. Without
this, each `FulfilApi.tpl_client` call would instantiate a fresh Faraday
connection, defeating the purpose of the persistent adapter.
34 35 36 37 38 |
# File 'lib/fulfil_api/tpl_client.rb', line 34 def connection_for(cache_key) @connection_cache_mutex.synchronize do @connection_cache[cache_key] ||= yield end end |
.reset_connection_cache! ⇒ void
This method returns an undefined value.
Clears the memoized connection cache. Intended for use in test suites
that need to isolate connection state between tests.
44 45 46 |
# File 'lib/fulfil_api/tpl_client.rb', line 44 def reset_connection_cache! @connection_cache_mutex.synchronize { @connection_cache.clear } end |
Instance Method Details
#get(relative_path, **url_parameters) ⇒ Array, ...
Performs an HTTP GET request to a 3PL API endpoint.
68 69 70 |
# File 'lib/fulfil_api/tpl_client.rb', line 68 def get(relative_path, **url_parameters) request(:get, relative_path, url_parameters.presence) end |
#patch(relative_path, body = {}) ⇒ Array, ...
Performs an HTTP PATCH request to a 3PL API endpoint.
77 78 79 |
# File 'lib/fulfil_api/tpl_client.rb', line 77 def patch(relative_path, body = {}) request(:patch, relative_path, body) end |
#post(relative_path, body = {}) ⇒ Array, ...
Performs an HTTP POST request to a 3PL API endpoint.
86 87 88 |
# File 'lib/fulfil_api/tpl_client.rb', line 86 def post(relative_path, body = {}) request(:post, relative_path, body) end |
#put(relative_path, body = nil) ⇒ Array, ...
Performs an HTTP PUT request to a 3PL API endpoint.
95 96 97 98 99 |
# File 'lib/fulfil_api/tpl_client.rb', line 95 def put(relative_path, body = nil) return request(:put, relative_path) if body.nil? request(:put, relative_path, body) end |