Files
telecom-platform/sdk/ruby/lib/telecom_sdk/http_client.rb
T
nutcas3 ea20dfb302 docs: Add Ruby SDK README with comprehensive API documentation and implement modular architecture with HTTParty integration
- Add README.md with installation instructions, architecture overview, and API examples for all SDK modules
- Refactor TelecomSDK::Client to use modular API clients (SubscriberAPI, UsageAPI, PaymentAPI, RatingPlanAPI, SystemAPI, GraphQLAPI)
- Add AuthProvider class with API key and JWT token support (generate_jwt_token, validate_jwt_token methods)
- Add HTTPClient base class with retry
2026-04-25 04:57:00 +03:00

82 lines
2.0 KiB
Ruby

require "httparty"
module TelecomSDK
# HTTP client for making API requests
class HTTPClient
include HTTParty
def initialize(base_url, auth_provider, timeout: 30, max_retries: 3, retry_delay: 1, enable_logging: false)
@base_url = base_url
@auth_provider = auth_provider
@timeout = timeout
@max_retries = max_retries
@retry_delay = retry_delay
@enable_logging = enable_logging
end
def get(path, params = {})
request("GET", path, nil, params)
end
def post(path, body = nil)
request("POST", path, body, nil)
end
def put(path, body = nil)
request("PUT", path, body, nil)
end
def delete(path)
request("DELETE", path, nil, nil)
end
private
def request(method, path, body, params)
url = "#{@base_url}#{path}"
headers = @auth_provider.get_headers
options = {
timeout: @timeout,
headers: headers
}
options[:query] = params if params
options[:body] = body.to_json if body
last_error = nil
(0..@max_retries).each do |attempt|
begin
response = HTTParty.send(method.downcase, url, options)
handle_response_errors(response)
return response.parsed_response
rescue StandardError => e
last_error = e
if attempt < @max_retries
sleep(@retry_delay * (2 ** attempt))
end
end
end
raise last_error || NetworkError, "Request failed after #{@max_retries} retries"
end
def handle_response_errors(response)
case response.code
when 401
raise AuthenticationError, "Authentication failed"
when 429
raise RateLimitError, "Rate limit exceeded"
when 400..499
raise APIError, "API error: #{response.parsed_response}"
when 500..599
raise ServerError, "Server error: #{response.code}"
end
end
def close
# HTTParty doesn't need explicit cleanup
end
end
end