Class: CdekApi::APIRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/cdek_api/api_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder: nil) ⇒ APIRequest

Returns a new instance of APIRequest.



4
5
6
# File 'lib/cdek_api/api_request.rb', line 4

def initialize(builder: nil)
  @request_builder = builder
end

Instance Method Details

#delete(params: nil, headers: nil, first_time: true) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cdek_api/api_request.rb', line 81

def delete(params: nil, headers: nil, first_time: true)
  validate_access_token

  begin
    response = self.rest_client.delete do |request|
      configure_request(request: request, params: params, headers: headers)
    end
    parse_response(response)
  rescue StandardError => e
    if e.response.dig(:status) == 401 && first_time
      CdekApi::Request.access_token = CdekApi.generate_access_token.try(:dig, "access_token")
      self.delete(params: params, headers: headers, first_time: false)
    else
      handle_error(e)
    end
  end
end

#get(params: nil, headers: nil, first_time: true) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cdek_api/api_request.rb', line 63

def get(params: nil, headers: nil, first_time: true)
  validate_access_token

  begin
    response = self.rest_client.get do |request|
      configure_request(request: request, params: params, headers: headers)
    end
    parse_response(response)
  rescue StandardError => e
    if e.response.dig(:status) == 401 && first_time
      CdekApi::Request.access_token = CdekApi.generate_access_token.try(:dig, "access_token")
      self.get(params: params, headers: headers, first_time: false)
    else
      handle_error(e)
    end
  end
end

#patch(params: nil, headers: nil, body: {}, first_time: true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cdek_api/api_request.rb', line 26

def patch(params: nil, headers: nil, body: {}, first_time: true)
  validate_access_token

  begin
    response = self.rest_client.patch do |request|
      body[:senderId] = CdekApi.senders.first["id"] if body[:senderId].nil?
      configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
    end
    parse_response(response)
  rescue StandardError => e
    if e.response.dig(:status) == 401 && first_time
      CdekApi::Request.access_token = CdekApi.generate_access_token.try(:dig, "access_token")
      self.patch(params: params, headers: headers, body: body, first_time: false)
    else
      handle_error(e)
    end
  end
end

#post(params: nil, headers: nil, body: {}, first_time: true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cdek_api/api_request.rb', line 8

def post(params: nil, headers: nil, body: {}, first_time: true)
  validate_access_token

  begin
    response = self.rest_client.post do |request|
      configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
    end
    parse_response(response)
  rescue StandardError => e
    if e.response.try(:dig, :status) == 401 && first_time
      CdekApi::Request.access_token = CdekApi.generate_access_token.try(:dig, "access_token")
      self.post(params: params, headers: headers, body: body, first_time: false)
    else
      handle_error(e)
    end
  end
end

#put(params: nil, headers: nil, body: {}, first_time: true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cdek_api/api_request.rb', line 45

def put(params: nil, headers: nil, body: {}, first_time: true)
  validate_access_token

  begin
    response = self.rest_client.put do |request|
      configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
    end
    parse_response(response)
  rescue StandardError => e
    if e.response.dig(:status) == 401 && first_time
      CdekApi::Request.access_token = CdekApi.generate_access_token.try(:dig, "access_token")
      self.put(params: params, headers: headers, body: body, first_time: false)
    else
      handle_error(e)
    end
  end
end