Class: Mercadopago::HttpClient

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

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).

Parameters:

  • url (String)

    fully qualified URL

  • headers (Hash)

    HTTP headers

  • timeout (Float, nil) (defaults to: nil)

    request timeout in seconds

Returns:

  • (Hash{Symbol => Object})

    :status and :response (parsed JSON body or nil)



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.

Parameters:

  • url (String)

    fully qualified URL

  • headers (Hash)

    HTTP headers (query params can be nested under :params)

  • params (Hash, nil) (defaults to: nil)

    query-string parameters appended to the URL

  • timeout (Float, nil) (defaults to: nil)

    request timeout in seconds

  • maxretries (Integer, nil) (defaults to: nil)

    maximum number of retry attempts

Returns:

  • (Hash{Symbol => Object})

    :status (Integer HTTP code) and :response (parsed JSON body)



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.

Parameters:

  • url (String)

    fully qualified URL

  • data (String, nil)

    JSON-encoded request body

  • headers (Hash)

    HTTP headers (should include Content-Type)

  • timeout (Float, nil) (defaults to: nil)

    request timeout in seconds

Returns:

  • (Hash{Symbol => Object})

    :status and :response (parsed JSON body)



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.

Parameters:

  • url (String)

    fully qualified URL

  • data (String, nil)

    JSON-encoded request body

  • headers (Hash)

    HTTP headers (should include Content-Type)

  • timeout (Float, nil) (defaults to: nil)

    request timeout in seconds

Returns:

  • (Hash{Symbol => Object})

    :status and :response (parsed JSON body)



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