Class: FulfilApi::TplClient

Inherits:
Object
  • Object
show all
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.

Examples:

Using the TPL client

FulfilApi.tpl_client.get("inbound-transfers", page: 1)
FulfilApi.tpl_client.post("inbound-transfers/receive.json", { tracking_number: "123" })

Defined Under Namespace

Classes: ConfigurationError

Constant Summary collapse

DEFAULT_API_VERSION =
"v1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ TplClient

Returns a new instance of TplClient.

Parameters:



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.

Parameters:

  • cache_key (Array)

    A key uniquely identifying the connection.

Yield Returns:

  • (Faraday::Connection)

    The newly built connection.

Returns:

  • (Faraday::Connection)


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.

Parameters:

  • relative_path (String)

    The relative path to the endpoint.

  • url_parameters (Hash)

    The optional URL parameters for the API endpoint.

Returns:

  • (Array, Hash, String)

    The parsed response body.



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.

Parameters:

  • relative_path (String)

    The relative path to the endpoint.

  • body (Array, Hash, nil) (defaults to: {})

    The request body for the PATCH HTTP request.

Returns:

  • (Array, Hash, String)

    The parsed response body.



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.

Parameters:

  • relative_path (String)

    The relative path to the endpoint.

  • body (Array, Hash, nil) (defaults to: {})

    The request body for the POST HTTP request.

Returns:

  • (Array, Hash, String)

    The parsed response body.



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.

Parameters:

  • relative_path (String)

    The relative path to the endpoint.

  • body (Array, Hash, nil) (defaults to: nil)

    The optional request body for the PUT HTTP request.

Returns:

  • (Array, Hash, String)

    The parsed response body.



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