Class: CDEKApiClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cdek_api_client/client.rb

Overview

Client class for interacting with the CDEK API.

Constant Summary collapse

BASE_URL =
ENV.fetch('CDEK_API_URL', 'https://api.edu.cdek.ru/v2')
TOKEN_URL =
"#{BASE_URL}/oauth/token".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, logger: Logger.new($stdout)) ⇒ Client

Initializes the client with API credentials and logger.

Parameters:

  • client_id (String)

    the client ID.

  • client_secret (String)

    the client secret.

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

    the logger instance.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cdek_api_client/client.rb', line 36

def initialize(client_id, client_secret, logger: Logger.new($stdout))
  @client_id = client_id
  @client_secret = client_secret
  @logger = logger
  @token = authenticate

  @order = CDEKApiClient::API::Order.new(self)
  @location = CDEKApiClient::API::Location.new(self)
  @tariff = CDEKApiClient::API::Tariff.new(self)
  @webhook = CDEKApiClient::API::Webhook.new(self)
end

Instance Attribute Details

#locationCDEKApiClient::Location (readonly)

Returns the location API interface.

Returns:

  • (CDEKApiClient::Location)

    the location API interface.



25
26
27
# File 'lib/cdek_api_client/client.rb', line 25

def location
  @location
end

#loggerLogger (readonly)

Returns the logger instance.

Returns:

  • (Logger)

    the logger instance.



21
22
23
# File 'lib/cdek_api_client/client.rb', line 21

def logger
  @logger
end

#orderCDEKApiClient::Order (readonly)

Returns the order API interface.

Returns:

  • (CDEKApiClient::Order)

    the order API interface.



23
24
25
# File 'lib/cdek_api_client/client.rb', line 23

def order
  @order
end

#tariffCDEKApiClient::Tariff (readonly)

Returns the tariff API interface.

Returns:

  • (CDEKApiClient::Tariff)

    the tariff API interface.



27
28
29
# File 'lib/cdek_api_client/client.rb', line 27

def tariff
  @tariff
end

#tokenString (readonly)

Returns the access token for API authentication.

Returns:

  • (String)

    the access token for API authentication.



19
20
21
# File 'lib/cdek_api_client/client.rb', line 19

def token
  @token
end

#webhookCDEKApiClient::Webhook (readonly)

Returns the webhook API interface.

Returns:

  • (CDEKApiClient::Webhook)

    the webhook API interface.



29
30
31
# File 'lib/cdek_api_client/client.rb', line 29

def webhook
  @webhook
end

Instance Method Details

#authenticateString

Authenticates with the API and retrieves an access token.

Returns:

  • (String)

    the access token.

Raises:

  • (StandardError)

    if authentication fails.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cdek_api_client/client.rb', line 52

def authenticate
  uri = URI(TOKEN_URL)
  response = Net::HTTP.post_form(uri, {
                                   grant_type: 'client_credentials',
                                   client_id: @client_id,
                                   client_secret: @client_secret
                                 })

  raise "Error getting token: #{response.body}" unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body)['access_token']
end

#request(method, path, body: nil) ⇒ Hash, Array

Makes an HTTP request to the API.

Parameters:

  • method (String)

    the HTTP method (e.g., ‘get’, ‘post’).

  • path (String)

    the API endpoint path.

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

    the request body.

Returns:

  • (Hash, Array)

    the parsed response.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cdek_api_client/client.rb', line 71

def request(method, path, body: nil)
  uri = URI("#{BASE_URL}/#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = build_request(method, uri, body)
  response = http.request(request)
  handle_response(response)
rescue StandardError => e
  @logger.error("HTTP request failed: #{e.message}")
  { 'error' => e.message }
end