Class: DigiwinDsp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/digiwin_dsp/client.rb

Constant Summary collapse

RETRY_STATUSES =
[429, 500, 502, 503, 504].freeze
USER_AGENT =
"digiwin_dsp/#{VERSION} (Faraday/#{Faraday::VERSION})".freeze
STATUS_ERROR_MAP =
{
  400 => ValidationError,
  401 => AuthenticationError,
  403 => AuthenticationError,
  409 => DuplicateRequestError,
  429 => RateLimitError
}.freeze
ENVELOPE_FAILURE_MAP =

Order matters — more-specific patterns first.

[
  [/\ADuplicated:/, DuplicateRequestError],
  [/\AProcessing:資料處理中/, RateLimitError],
  [/\AProcessing:取消訂單處理中/, ValidationError],
  [/\AWrongStatus:/, ValidationError],
  [/\A系統異常:/, ServerError]
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration: DigiwinDsp.configuration, authenticator: nil) ⇒ Client

Returns a new instance of Client.



28
29
30
31
# File 'lib/digiwin_dsp/client.rb', line 28

def initialize(configuration: DigiwinDsp.configuration, authenticator: nil)
  @configuration = configuration
  @authenticator = authenticator || Authenticator.new(configuration)
end

Instance Method Details

#post(path, body, idempotency_key: nil, headers: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/digiwin_dsp/client.rb', line 33

def post(path, body, idempotency_key: nil, headers: {})
  @configuration.validate!
  response = connection.post(normalize_path(path)) do |req|
    req.headers["X-Idempotency-Key"] = idempotency_key if idempotency_key
    headers.each { |k, v| req.headers[k] = v }
    req.body = body
  end
  handle_response(response)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
  raise NetworkError, e.message
end