Class: Mercadopago::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mercadopago/http/http_client.rb

Instance Method Summary collapse

Instance Method Details

#delete(url:, headers:, timeout: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mercadopago/http/http_client.rb', line 61

def delete(url:, headers:, timeout: nil)
  result = RestClient::Request.execute(method: 'delete', url: url, headers: headers, timeout: timeout)
  {
    status: result.code,
    response: JSON.parse(result.body)
  }
rescue RestClient::Exception => e
  {
    status: e.http_code,
    response: JSON.parse(e.response.body)
  }
end

#get(url:, headers:, params: nil, timeout: nil, maxretries: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mercadopago/http/http_client.rb', line 9

def get(url:, headers:, params: nil, timeout: nil, maxretries: nil)
  try = 0
  headers = {} if headers.nil?
  headers[:params] = params unless params.nil?

  begin
    result = RestClient::Request.execute(method: :get, url: url, headers: headers, timeout: timeout)
    {
      status: result.code,
      response: JSON.parse(result.body)
    }
  rescue RestClient::Exception => e
    try += 1
    if [429, 500, 502, 503, 504].include?(e.http_code) && (try < maxretries)
      sleep(1)
      retry
    end
    {
      status: e.http_code,
      response: JSON.parse(e.response.body)
    }
  end
end

#post(url:, data:, headers:, timeout: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mercadopago/http/http_client.rb', line 33

def post(url:, data:, headers:, timeout: nil)
  result = RestClient::Request.execute(method: :post, url: url, payload: data, headers: headers,
                                       timeout: timeout)
  {
    status: result.code,
    response: JSON.parse(result.body)
  }
rescue RestClient::Exception => e
  {
    status: e.http_code,
    response: JSON.parse(e.response.body)
  }
end

#put(url:, data:, headers:, timeout: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mercadopago/http/http_client.rb', line 47

def put(url:, data:, headers:, timeout: nil)
  result = RestClient::Request.execute(method: :put, url: url, payload: data, headers: headers,
                                       timeout: timeout)
  {
    status: result.code,
    response: JSON.parse(result.body)
  }
rescue RestClient::Exception => e
  {
    status: e.http_code,
    response: JSON.parse(e.response.body)
  }
end