Class: Wajub::BaseClient
- Inherits:
-
Object
- Object
- Wajub::BaseClient
- Defined in:
- lib/wajub/http/base_client.rb
Direct Known Subclasses
BalanceResource, CrudResource, DisputesResource, EventsResource, GlobalResource, IdentityResource, ListenResource, PaymentsResource, ShieldResource, TaxResource
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#idempotency_key_prefix ⇒ Object
readonly
Returns the value of attribute idempotency_key_prefix.
-
#last_request ⇒ Object
readonly
Returns the value of attribute last_request.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
- #delete(path, options = nil) ⇒ Object
- #get(path, params = nil) ⇒ Object
-
#initialize(api_key, base_url, idempotency_key_prefix = 'wajub', transport: nil) ⇒ BaseClient
constructor
A new instance of BaseClient.
- #post(path, body = nil, options = nil) ⇒ Object
- #put(path, body = nil, options = nil) ⇒ Object
- #request(method, path, body: nil, options: nil, query: nil) ⇒ Object
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_key ⇒ Object (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_url ⇒ Object (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_prefix ⇒ Object (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_request ⇒ Object (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 |
#transport ⇒ Object (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, = nil) request('DELETE', path, body: nil, 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, = nil) request('POST', path, body: body, 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, = nil) request('PUT', path, body: body, 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 } ||= RequestOptions.new headers.merge!(.headers) if .headers headers['X-Sync'] = .sync if .sync idempotency_key = .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.) 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 |