Class: Mercadopago::HttpClient
- Inherits:
-
Object
- Object
- Mercadopago::HttpClient
- Defined in:
- lib/mercadopago/http/http_client.rb
Overview
Low-level HTTP transport layer built on top of RestClient.
Provides the four standard HTTP verbs (GET, POST, PUT, DELETE) and normalises every response into a { status:, response: } hash. GET requests include automatic retry with a 1-second back-off for transient errors (429, 500, 502, 503, 504).
This class is used internally by MPBase. You can subclass or replace it via SDK.new(token, http_client: MyClient.new) to inject a custom transport (e.g. for testing or logging).
Constant Summary collapse
- RETRYABLE_STATUSES =
[429, 500, 502, 503, 504].freeze
Instance Method Summary collapse
-
#delete(url:, headers:, timeout: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP DELETE request.
-
#get(url:, headers:, params: nil, timeout: nil, maxretries: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP GET request with automatic retry on transient errors.
-
#post(url:, data:, headers:, timeout: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP POST request.
-
#put(url:, data:, headers:, timeout: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP PUT request.
Instance Method Details
#delete(url:, headers:, timeout: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP DELETE request.
Returns nil as the response body when the server sends an empty body, which is common for successful deletions (204-style responses).
77 78 79 |
# File 'lib/mercadopago/http/http_client.rb', line 77 def delete(url:, headers:, timeout: nil) build_result(execute(:delete, url, headers: headers, timeout: timeout), allow_empty: true) end |
#get(url:, headers:, params: nil, timeout: nil, maxretries: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP GET request with automatic retry on transient errors.
Retries up to maxretries times with a 1-second sleep between attempts when the server responds with 429 or 5xx status codes.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mercadopago/http/http_client.rb', line 33 def get(url:, headers:, params: nil, timeout: nil, maxretries: nil) try = 0 max = maxretries.to_i loop do response = execute(:get, url, headers: headers, params: params, timeout: timeout) return build_result(response) unless RETRYABLE_STATUSES.include?(response.status) && try < max - 1 try += 1 sleep(1) end end |
#post(url:, data:, headers:, timeout: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP POST request.
53 54 55 |
# File 'lib/mercadopago/http/http_client.rb', line 53 def post(url:, data:, headers:, timeout: nil) build_result(execute(:post, url, headers: headers, body: data, timeout: timeout)) end |
#put(url:, data:, headers:, timeout: nil) ⇒ Hash{Symbol => Object}
Performs an HTTP PUT request.
64 65 66 |
# File 'lib/mercadopago/http/http_client.rb', line 64 def put(url:, data:, headers:, timeout: nil) build_result(execute(:put, url, headers: headers, body: data, timeout: timeout)) end |