lib/recurly/client.rb in recurly-3.0.0.beta.5 vs lib/recurly/client.rb in recurly-3.0.0
- old
+ new
@@ -6,53 +6,34 @@
module Recurly
class Client
require_relative "./client/operations"
- BASE_URL = "https://partner-api.recurly.com/"
+ BASE_URL = "https://v3.recurly.com/"
- # The last result of the *X-RateLimit-Limit* header
- # @return [Integer] The rate limit applied to this client.
- attr_reader :rate_limit
-
- # The last result of the *X-RateLimit-Remaining* header
- # @return [Integer] The number of remaining requests, decrements per request.
- attr_reader :rate_limit_remaining
-
- # The last result of the *X-RateLimit-Reset* header
- # @return [DateTime] The DateTime in which the request count will be reset.
- attr_reader :rate_limit_reset
-
# Initialize a client. It requires an API key.
#
# @example
# API_KEY = '83749879bbde395b5fe0cc1a5abf8e5'
- # SITE_ID = 'dqzlv9shi7wa'
- # client = Recurly::Client.new(site_id: SITE_ID, api_key: API_KEY)
- # # You can optionally use the subdomain instead of the site id
- # client = Recurly::Client.new(subdomain: 'mysite-prod', api_key: API_KEY)
+ # client = Recurly::Client.new(api_key: API_KEY)
# sub = client.get_subscription(subscription_id: 'abcd123456')
# @example
# # You can also pass the initializer a block. This will give you
# # a client scoped for just that block
- # Recurly::Client.new(subdomain: 'mysite-prod', api_key: API_KEY) do |client|
+ # Recurly::Client.new(api_key: API_KEY) do |client|
# sub = client.get_subscription(subscription_id: 'abcd123456')
# end
# @example
# # If you only plan on using the client for more than one site,
# # you should initialize a new client for each site.
#
- # # Give a `site_id`
- # client = Recurly::Client.new(api_key: API_KEY, site_id: SITE_ID)
- # # Or use the subdomain
- # client = Recurly::Client.new(api_key: API_KEY, subdomain: 'mysite-dev')
+ # client = Recurly::Client.new(api_key: API_KEY1)
+ # sub = client.get_subscription(subscription_id: 'uuid-abcd123456')
#
- # sub = client.get_subscription(subscription_id: 'abcd123456')
- #
# # you should create a new client to connect to another site
- # client = Recurly::Client.new(api_key: API_KEY, subdomain: 'mysite-prod')
- # sub = client.get_subscription(subscription_id: 'abcd7890')
+ # client = Recurly::Client.new(api_key: API_KEY2)
+ # sub = client.get_subscription(subscription_id: 'uuid-abcd7890')
#
# @param api_key [String] The private API key
# @param site_id [String] The site you wish to be scoped to.
# @param subdomain [String] Optional subdomain for the site you wish to be scoped to. Providing this makes all the `site_id` parameters optional.
def initialize(api_key:, site_id: nil, subdomain: nil, **options)
@@ -63,61 +44,64 @@
# execute block with this client if given
yield(self) if block_given?
end
def next_page(pager)
- run_request(:get, pager.next, nil, headers).tap do |response|
- raise_api_error!(response) unless (200...300).include?(response.status)
- end
+ req = HTTP::Request.new(:get, pager.next, nil)
+ faraday_resp = run_request(req, headers)
+ handle_response! req, faraday_resp
end
protected
def pager(path, **options)
- Pager.new(client: self, path: path, options: options)
+ path = scope_by_site(path, **options)
+ Pager.new(
+ client: self,
+ path: path,
+ options: options,
+ )
end
def get(path, **options)
- response = run_request(:get, path, nil, headers)
- raise_api_error!(response) unless (200...300).include?(response.status)
- JSONParser.parse(self, response.body)
+ path = scope_by_site(path, **options)
+ request = HTTP::Request.new(:get, path, nil)
+ faraday_resp = run_request(request, headers)
+ handle_response! request, faraday_resp
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
def post(path, request_data, request_class, **options)
- request = request_class.new(request_data)
- request.validate!
- logger.info("POST BODY #{JSON.dump(request_data)}")
- response = run_request(:post, path, JSON.dump(request.attributes), headers)
- raise_api_error!(response) unless (200...300).include?(response.status)
- JSONParser.parse(self, response.body)
+ request_class.new(request_data).validate!
+ path = scope_by_site(path, **options)
+ request = HTTP::Request.new(:post, path, JSON.dump(request_data))
+ faraday_resp = run_request(request, headers)
+ handle_response! request, faraday_resp
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
def put(path, request_data = nil, request_class = nil, **options)
- response = if request_data
- request = request_class.new(request_data)
- request.validate!
- logger.info("PUT BODY #{JSON.dump(request_data)}")
- run_request(:put, path, JSON.dump(request_data), headers)
- else
- run_request(:put, path, nil, headers)
- end
- raise_api_error!(response) unless (200...300).include?(response.status)
- JSONParser.parse(self, response.body)
+ path = scope_by_site(path, **options)
+ request = HTTP::Request.new(:put, path)
+ if request_data
+ request_class.new(request_data).validate!
+ logger.info("PUT BODY #{JSON.dump(request_data)}")
+ request.body = JSON.dump(request_data)
+ end
+ faraday_resp = run_request(request, headers)
+ handle_response! request, faraday_resp
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
def delete(path, **options)
- response = run_request(:delete, path, nil, headers)
- raise_api_error!(response) unless (200...300).include?(response.status)
- if response.body && !response.body.empty?
- JSONParser.parse(self, response.body)
- end
+ path = scope_by_site(path, **options)
+ request = HTTP::Request.new(:delete, path, nil)
+ faraday_resp = run_request(request, headers)
+ handle_response! request, faraday_resp
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
protected
@@ -128,14 +112,27 @@
private
# @return [Logger]
attr_reader :logger
- def run_request(method, url, body, headers)
- read_headers @conn.run_request(method, url, body, headers)
+ def run_request(request, headers)
+ read_headers @conn.run_request(request.method, request.path, request.body, headers)
end
+ def handle_response!(request, faraday_resp)
+ response = HTTP::Response.new(faraday_resp, request)
+ raise_api_error!(response) unless (200...300).include?(response.status)
+ resource = if response.body
+ JSONParser.parse(self, response.body)
+ else
+ Resources::Empty.new
+ end
+ # Keep this interface "private"
+ resource.instance_variable_set(:@response, response)
+ resource
+ end
+
def raise_network_error!(ex)
error_class = case ex
when Faraday::TimeoutError
Errors::TimeoutError
when Faraday::ConnectionFailed
@@ -154,13 +151,10 @@
error_class = Errors::APIError.error_class(error.type)
raise error_class.new(response, error)
end
def read_headers(response)
- @rate_limit = response.headers["x-ratelimit-limit"].to_i
- @rate_limit_remaining = response.headers["x-ratelimit-remaining"].to_i
- @rate_limit_reset = Time.at(response.headers["x-ratelimit-reset"].to_i).to_datetime
if !@_ignore_deprecation_warning && response.headers["Recurly-Deprecated"]&.upcase == "TRUE"
puts "[recurly-client-ruby] WARNING: Your current API version \"#{api_version}\" is deprecated and will be sunset on #{response.headers["Recurly-Sunset-Date"]}"
end
response
end
@@ -194,11 +188,17 @@
def set_site_id(site_id, subdomain)
if site_id
@site_id = site_id
elsif subdomain
@site_id = "subdomain-#{subdomain}"
+ end
+ end
+
+ def scope_by_site(path, **options)
+ if site = site_id || options[:site_id]
+ "/sites/#{site}#{path}"
else
- raise ArgumentError, "You must pass a site_id or subdomain argument to initialize the Client"
+ path
end
end
def set_faraday_connection(api_key)
options = {