lib/recurly/client.rb in recurly-3.0.0.beta.2 vs lib/recurly/client.rb in recurly-3.0.0.beta.3
- old
+ new
@@ -1,8 +1,9 @@
require 'faraday'
require 'logger'
require_relative './schema/json_parser'
+require_relative './client/adapter'
module Recurly
class Client
require_relative './client/operations'
@@ -40,62 +41,34 @@
# # 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_KEY, subdomain: 'mysite-dev')
#
# 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')
+ # client = Recurly::Client.new(api_key: API_KEY, subdomain: 'mysite-prod')
# sub = client.get_subscription(subscription_id: '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)
- if site_id
- @site_id = site_id
- elsif subdomain
- @site_id = "subdomain-#{subdomain}"
- else
- raise ArgumentError, "You must pass a site_id or subdomain argument to initialize the Client"
- end
+ set_site_id(site_id, subdomain)
+ set_options(options)
+ set_faraday_connection(api_key)
- @log_level = options[:log_level] || Logger::WARN
- @logger = Logger.new(STDOUT)
- @logger.level = @log_level
-
- @conn = Faraday.new(url: BASE_URL) do |faraday|
- if @log_level == Logger::INFO
- faraday.response :logger
- end
- faraday.basic_auth(api_key, '')
- faraday.adapter :net_http do |http| # yields Net::HTTP
- # Let's not use the bundled cert in production yet
- # but we will use these certs for any other staging or dev environment
- unless BASE_URL.end_with?('.recurly.com')
- http.ca_file = File.join(File.dirname(__FILE__), '../data/ca-certificates.crt')
- end
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
- http.open_timeout = 50
- http.read_timeout = 60
- http.keep_alive_timeout = 60
- end
- end
-
- # TODO this is undocumented until we finalize it
- @extra_headers = options[:headers] || {}
-
# execute block with this client if given
yield(self) if block_given?
end
def next_page(pager)
- run_request(:get, pager.next, nil, headers)
+ run_request(:get, pager.next, nil, headers).tap do |response|
+ raise_api_error!(response) if response.status != 200
+ end
end
protected
def pager(path, **options)
@@ -131,11 +104,11 @@
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
def delete(path, **options)
- response = run_request(:delete, path, options.compact, headers)
+ response = run_request(:delete, path, nil, headers)
raise_api_error!(response) if ![200, 201].include?(response.status)
JSONParser.parse(self, response.body)
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
@@ -191,8 +164,37 @@
end
def interpolate_path(path, **options)
path = path.gsub("{", "%{")
path % options
+ end
+
+ def set_site_id(site_id, subdomain)
+ if site_id
+ @site_id = site_id
+ elsif subdomain
+ @site_id = "subdomain-#{subdomain}"
+ else
+ raise ArgumentError, "You must pass a site_id or subdomain argument to initialize the Client"
+ end
+ end
+
+ def set_faraday_connection(api_key)
+ @conn = Faraday.new(url: BASE_URL) do |faraday|
+ if @log_level == Logger::INFO
+ faraday.response :logger
+ end
+ faraday.basic_auth(api_key, '')
+ configure_net_adapter(faraday)
+ end
+ end
+
+ def set_options(options)
+ @log_level = options[:log_level] || Logger::WARN
+ @logger = Logger.new(STDOUT)
+ @logger.level = @log_level
+
+ # TODO this is undocumented until we finalize it
+ @extra_headers = options[:headers] || {}
end
end
end