Skip to content

Commit

Permalink
Add region to client.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
FabricioCoutinho authored and douglasmiller committed Jan 30, 2022
1 parent 10fd259 commit ea95625
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
8 changes: 8 additions & 0 deletions GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ client = Recurly::Client.new(api_key: API_KEY)
sub = client.get_subscription(subscription_id: 'abcd123456')
```

To access Recurly servers in Europe you have to initialize the client with the argument region with the value :eu.

```ruby
API_KEY = '83749879bbde395b5fe0cc1a5abf8e5'
client = Recurly::Client.new(api_key: API_KEY, region: :eu)
sub = client.get_subscription(subscription_id: 'abcd123456')
```

You can also pass the initializer a block. This will give you a client scoped for just that block:

```ruby
Expand Down
16 changes: 14 additions & 2 deletions lib/recurly/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ module Recurly
class Client
require_relative "./client/operations"

BASE_URL = "https://v3.recurly.com"
API_HOSTS = {
us: "https://v3.recurly.com",
eu: "https://v3.eu.recurly.com",
}
REGION = :us
CA_FILE = File.join(File.dirname(__FILE__), "../data/ca-certificates.crt")
BINARY_TYPES = [
"application/pdf",
Expand Down Expand Up @@ -52,13 +56,18 @@ class Client
# client = Recurly::Client.new(api_key: API_KEY2)
# sub = client.get_subscription(subscription_id: 'uuid-abcd7890')
#
# @param region [String] The DataCenter that is called by the API. Default to "us"
# @param base_url [String] The base URL for the API. Defaults to "https://v3.recurly.com"
# @param ca_file [String] The CA bundle to use when connecting to the API. Defaults to "data/ca-certificates.crt"
# @param api_key [String] The private API key
# @param logger [Logger] A logger to use. Defaults to creating a new STDOUT logger with level WARN.
def initialize(base_url: BASE_URL, ca_file: CA_FILE, api_key:, logger: nil)
def initialize(region: REGION, base_url: API_HOSTS[:us], ca_file: CA_FILE, api_key:, logger: nil)
raise ArgumentError, "'api_key' must be set to a non-nil value" if api_key.nil?

raise ArgumentError, "Invalid region type. Expected one of: #{API_HOSTS.keys.join(", ")}" if !API_HOSTS.key?(region)

base_url = API_HOSTS[region] if base_url == API_HOSTS[:us] && API_HOSTS.key?(region)

set_api_key(api_key)
set_connection_options(base_url, ca_file)

Expand Down Expand Up @@ -110,7 +119,9 @@ def head(path, **options)

def get(path, **options)
validate_options!(**options)

request = Net::HTTP::Get.new build_url(path, options)

set_headers(request, options[:headers])
http_response = run_request(request, options)
handle_response! request, http_response
Expand Down Expand Up @@ -168,6 +179,7 @@ def run_request(request, options = {})

begin
http.start unless http.started?

log_attrs = {
method: request.method,
path: request.path,
Expand Down
36 changes: 35 additions & 1 deletion spec/recurly/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
end
let(:net_http) {
Recurly::ConnectionPool.new.init_http_connection(URI.parse(Recurly::Client::BASE_URL), Recurly::Client::CA_FILE)
Recurly::ConnectionPool.new.init_http_connection(URI.parse(Recurly::Client::API_HOSTS[:us]), Recurly::Client::CA_FILE)
}
let(:connection_pool) {
pool = double("ConnectionPool")
Expand All @@ -37,9 +37,41 @@
end
end

context "#base_url" do
it "should use US base url" do
expect(client.instance_variable_get(:@base_uri).to_s).to eq(Recurly::Client::API_HOSTS[:us])
end

it "should use EU base url" do
client = Recurly::Client.new(**client_options.merge(region: :eu))
expect(client.instance_variable_get(:@base_uri).to_s).to eq(Recurly::Client::API_HOSTS[:eu])
end

describe "using a custom base url" do
let(:custom_url) { "partner-api.recurly.com" }

it "should use a custom base url in EU" do
client = Recurly::Client.new(**client_options.merge(region: :eu, base_url: custom_url))
expect(client.instance_variable_get(:@base_uri).to_s).to eq(custom_url)
end

it "should use a custom base url in US" do
client = Recurly::Client.new(**client_options.merge(base_url: custom_url))
expect(client.instance_variable_get(:@base_uri).to_s).to eq(custom_url)
end

it "should raise an ArgumentError when region is invalid" do
expect {
Recurly::Client.new(**client_options.merge(region: :none))
}.to raise_error(ArgumentError, "Invalid region type. Expected one of: #{Recurly::Client::API_HOSTS.keys.join(", ")}")
end
end
end

context "with sucessful responses" do
let(:response) do
resp = Net::HTTPOK.new(1.0, "200", "OK")

allow(resp).to receive(:body) do
"{ \"object\": \"account\" }"
end
Expand All @@ -51,6 +83,7 @@
describe "headers" do
let(:request) do
req_dbl = instance_double(Net::HTTP::Get)

expect(req_dbl).to receive(:[]=).with("Accept", /application\/vnd\.recurly/)
expect(req_dbl).to receive(:[]=).with("Authorization", /Basic .*/)
expect(req_dbl).to receive(:[]=).with("User-Agent", /^Recurly\/\d+(\.\d+){0,2}; ruby \d+(\.\d+){0,2}.*$/)
Expand Down Expand Up @@ -145,6 +178,7 @@
it "should return a the created account for create_account" do
body = { code: "benjamin-du-monde" }
req = Recurly::HTTP::Request.new(:post, "/accounts", JSON.dump(body))

expect(net_http).to receive(:request).and_return(response)
account = subject.create_account(body: body)
expect(account).to be_instance_of Recurly::Resources::Account
Expand Down

0 comments on commit ea95625

Please sign in to comment.