Class: Belpost::ApiService

Inherits:
Object
  • Object
show all
Defined in:
lib/belpost/api_service.rb

Overview

Service class for handling HTTP requests to the BelPost API.

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, jwt_token:, timeout: 10, logger: Logger.new($stdout)) ⇒ ApiService

Initializes a new instance of the ApiService.

Parameters:

  • base_url (String)

    The base URL of the API.

  • jwt_token (String)

    The JWT token for authentication.

  • timeout (Integer) (defaults to: 10)

    The request timeout in seconds (default: 10).

  • logger (Logger) (defaults to: Logger.new($stdout))

    The logger for logging requests and responses.



16
17
18
19
20
21
# File 'lib/belpost/api_service.rb', line 16

def initialize(base_url:, jwt_token:, timeout: 10, logger: Logger.new($stdout))
  @base_url = base_url
  @jwt_token = jwt_token
  @timeout = timeout
  @logger = logger
end

Instance Method Details

#delete(path) ⇒ Models::ApiResponse

Performs a DELETE request to the specified path.

Parameters:

  • path (String)

    The API endpoint path.

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/belpost/api_service.rb', line 136

def delete(path)
  Retry.with_retry do
    uri = URI("#{@base_url}#{path}")
    request = Net::HTTP::Delete.new(uri)
    add_headers(request)

    log_request(request)
    response = execute_request(uri, request)
    log_response(response)

    begin
      Models::ApiResponse.new(
        data: JSON.parse(response.body),
        status_code: response.code.to_i,
        headers: response.to_hash
      )
    rescue JSON::ParserError => e
      raise ParseError, "Failed to parse JSON response: #{e.message}"
    end
  end
end

#get(path, params = {}) ⇒ Models::ApiResponse

Performs a GET request to the specified path.

Parameters:

  • path (String)

    The API endpoint path.

  • params (Hash) (defaults to: {})

    The query parameters (default: {}).

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/belpost/api_service.rb', line 28

def get(path, params = {})
  Retry.with_retry do
    uri = URI("#{@base_url}#{path}")
    uri.query = URI.encode_www_form(params) unless params.empty?
    request = Net::HTTP::Get.new(uri)
    add_headers(request)

    log_request(request)
    response = execute_request(uri, request)
    log_response(response)

    begin
      Models::ApiResponse.new(
        data: JSON.parse(response.body),
        status_code: response.code.to_i,
        headers: response.to_hash
      )
    rescue JSON::ParserError => e
      raise ParseError, "Failed to parse JSON response: #{e.message}"
    end
  end
end

#get_binary(path, params = {}) ⇒ Hash

Performs a GET request to the specified path and returns binary data.

Parameters:

  • path (String)

    The API endpoint path.

  • params (Hash) (defaults to: {})

    The query parameters (default: {}).

Returns:

  • (Hash)

    Hash containing binary data, status code and headers



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/belpost/api_service.rb', line 56

def get_binary(path, params = {})
  Retry.with_retry do
    uri = URI("#{@base_url}#{path}")
    uri.query = URI.encode_www_form(params) unless params.empty?
    request = Net::HTTP::Get.new(uri)
    add_headers(request)
    request["Accept"] = "*/*"  # Override Accept header to receive any content type

    log_request(request)
    response = execute_request(uri, request)
    log_response(response, binary: true)

    {
      data: response.body,
      status_code: response.code.to_i,
      headers: response.to_hash
    }
  end
end

#post(path, body) ⇒ Models::ApiResponse

Performs a POST request to the specified path with the given body.

Parameters:

  • path (String)

    The API endpoint path.

  • body (Hash)

    The request body as a hash.

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/belpost/api_service.rb', line 81

def post(path, body)
  Retry.with_retry do
    uri = URI("#{@base_url}#{path}")
    request = Net::HTTP::Post.new(uri)
    add_headers(request)
    request.body = body.to_json

    log_request(request)
    response = execute_request(uri, request)
    log_response(response)

    begin
      Models::ApiResponse.new(
        data: JSON.parse(response.body),
        status_code: response.code.to_i,
        headers: response.to_hash
      )
    rescue JSON::ParserError => e
      raise ParseError, "Failed to parse JSON response: #{e.message}"
    end
  end
end

#put(path, body) ⇒ Models::ApiResponse

Performs a PUT request to the specified path with the given body.

Parameters:

  • path (String)

    The API endpoint path.

  • body (Hash)

    The request body as a hash.

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/belpost/api_service.rb', line 109

def put(path, body)
  Retry.with_retry do
    uri = URI("#{@base_url}#{path}")
    request = Net::HTTP::Put.new(uri)
    add_headers(request)
    request.body = body.to_json

    log_request(request)
    response = execute_request(uri, request)
    log_response(response)

    begin
      Models::ApiResponse.new(
        data: JSON.parse(response.body),
        status_code: response.code.to_i,
        headers: response.to_hash
      )
    rescue JSON::ParserError => e
      raise ParseError, "Failed to parse JSON response: #{e.message}"
    end
  end
end