Class: Wajub::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wajub/http/base_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, base_url, idempotency_key_prefix = 'wajub', transport: nil) ⇒ BaseClient

Returns a new instance of BaseClient.



17
18
19
20
21
22
23
# File 'lib/wajub/http/base_client.rb', line 17

def initialize(api_key, base_url, idempotency_key_prefix = 'wajub', transport: nil)
  @api_key = HttpUtils.normalize_api_key(api_key)
  @base_url = base_url.sub(%r{/+\z}, '')
  @idempotency_key_prefix = idempotency_key_prefix
  @transport = transport || NetHttpTransport.new(@base_url, user_agent: "wajub-ruby/#{VERSION}")
  @last_request = nil
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



15
16
17
# File 'lib/wajub/http/base_client.rb', line 15

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



15
16
17
# File 'lib/wajub/http/base_client.rb', line 15

def base_url
  @base_url
end

#idempotency_key_prefixObject (readonly)

Returns the value of attribute idempotency_key_prefix.



15
16
17
# File 'lib/wajub/http/base_client.rb', line 15

def idempotency_key_prefix
  @idempotency_key_prefix
end

#last_requestObject (readonly)

Returns the value of attribute last_request.



15
16
17
# File 'lib/wajub/http/base_client.rb', line 15

def last_request
  @last_request
end

#transportObject (readonly)

Returns the value of attribute transport.



15
16
17
# File 'lib/wajub/http/base_client.rb', line 15

def transport
  @transport
end

Instance Method Details

#delete(path, options = nil) ⇒ Object



37
38
39
# File 'lib/wajub/http/base_client.rb', line 37

def delete(path, options = nil)
  request('DELETE', path, body: nil, options: options)
end

#get(path, params = nil) ⇒ Object



25
26
27
# File 'lib/wajub/http/base_client.rb', line 25

def get(path, params = nil)
  request('GET', path, body: nil, options: nil, query: params)
end

#post(path, body = nil, options = nil) ⇒ Object



29
30
31
# File 'lib/wajub/http/base_client.rb', line 29

def post(path, body = nil, options = nil)
  request('POST', path, body: body, options: options)
end

#put(path, body = nil, options = nil) ⇒ Object



33
34
35
# File 'lib/wajub/http/base_client.rb', line 33

def put(path, body = nil, options = nil)
  request('PUT', path, body: body, options: options)
end

#request(method, path, body: nil, options: nil, query: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wajub/http/base_client.rb', line 41

def request(method, path, body: nil, options: nil, query: nil)
  headers = {
    'Accept' => 'application/json',
    'Authorization' => @api_key
  }

  options ||= RequestOptions.new
  headers.merge!(options.headers) if options.headers
  headers['X-Sync'] = options.sync if options.sync

  idempotency_key = options.idempotency_key
  if idempotency_key.nil? && !%w[GET DELETE].include?(method)
    idempotency_key = HttpUtils.create_idempotency_key(@idempotency_key_prefix)
  end
  headers['Idempotency-Key'] = idempotency_key if idempotency_key

  payload = body.nil? ? nil : JSON.generate(body)
  headers['Content-Type'] = 'application/json' if payload

  attempt = 0

  loop do
    begin
      response = @transport.call(
        method: method,
        path: path,
        headers: headers,
        body: payload,
        query: query
      )
    rescue StandardError => e
      if attempt < RetryPolicy.max_retries
        attempt += 1
        sleep RetryPolicy.delay_seconds(attempt)
        next
      end
      raise ApiConnectionError.new(e.message)
    end

    @last_request = response[:request]
    status = response[:status]
    data = response[:body].is_a?(Hash) ? response[:body] : HttpUtils.decode_json(response[:body].to_s)

    return data if status >= 200 && status < 300

    if RetryPolicy.should_retry_status?(status) && attempt < RetryPolicy.max_retries
      attempt += 1
      sleep RetryPolicy.delay_seconds(attempt, response: response[:headers] || {})
      next
    end

    retry_after = response.dig(:headers, 'retry-after')&.to_i
    raise Errors.from_response(status, data, retry_after: retry_after)
  end
end