Class: CDEKApiClient::Client
- Inherits:
-
Object
- Object
- CDEKApiClient::Client
- 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
-
#location ⇒ CDEKApiClient::Location
readonly
The location API interface.
-
#logger ⇒ Logger
readonly
The logger instance.
-
#order ⇒ CDEKApiClient::Order
readonly
The order API interface.
-
#tariff ⇒ CDEKApiClient::Tariff
readonly
The tariff API interface.
-
#token ⇒ String
readonly
The access token for API authentication.
-
#webhook ⇒ CDEKApiClient::Webhook
readonly
The webhook API interface.
Instance Method Summary collapse
-
#authenticate ⇒ String
Authenticates with the API and retrieves an access token.
-
#initialize(client_id, client_secret, logger: Logger.new($stdout)) ⇒ Client
constructor
Initializes the client with API credentials and logger.
-
#request(method, path, body: nil) ⇒ Hash, Array
Makes an HTTP request to the API.
Constructor Details
#initialize(client_id, client_secret, logger: Logger.new($stdout)) ⇒ Client
Initializes the client with API credentials and logger.
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
#location ⇒ CDEKApiClient::Location (readonly)
Returns the location API interface.
25 26 27 |
# File 'lib/cdek_api_client/client.rb', line 25 def location @location end |
#logger ⇒ Logger (readonly)
Returns the logger instance.
21 22 23 |
# File 'lib/cdek_api_client/client.rb', line 21 def logger @logger end |
#order ⇒ CDEKApiClient::Order (readonly)
Returns the order API interface.
23 24 25 |
# File 'lib/cdek_api_client/client.rb', line 23 def order @order end |
#tariff ⇒ CDEKApiClient::Tariff (readonly)
Returns the tariff API interface.
27 28 29 |
# File 'lib/cdek_api_client/client.rb', line 27 def tariff @tariff end |
#token ⇒ String (readonly)
Returns the access token for API authentication.
19 20 21 |
# File 'lib/cdek_api_client/client.rb', line 19 def token @token end |
#webhook ⇒ CDEKApiClient::Webhook (readonly)
Returns the webhook API interface.
29 30 31 |
# File 'lib/cdek_api_client/client.rb', line 29 def webhook @webhook end |
Instance Method Details
#authenticate ⇒ String
Authenticates with the API and retrieves an access token.
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.
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.}") { 'error' => e. } end |