Skip to content

Commit

Permalink
Merge pull request #230 from recurly/add-region-to-client
Browse files Browse the repository at this point in the history
Add region argument to client to connect in EU data center
  • Loading branch information
cbarton authored Jan 31, 2022
2 parents 9e705ce + b337221 commit 35f13ff
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
10 changes: 10 additions & 0 deletions doc/GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ const myApiKey = '<myapikey>'
const client = new recurly.Client(myApiKey)
```

To access Recurly API in Europe, you will need to specify the EU Region in the `options`.

```js
const recurly = require('recurly')
// You should store your API key somewhere safe
// and not in plain text if possible
const myApiKey = '<myapikey>'
const client = new recurly.Client(myApiKey, { 'region': 'eu' })
```

**Note**: to import using typescript:
```ts
import * as recurly from "recurly";
Expand Down
18 changes: 16 additions & 2 deletions lib/recurly/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@ const BINARY_TYPES = [
]
const JSON_CONTENT_TYPE = 'application/json'
const ALLOWED_OPTIONS = ['body', 'params', 'headers']
const API_HOSTS = {
us: 'v3.recurly.com',
eu: 'v3.eu.recurly.com'
}

/**
* This is the base functionality for the Recurly.Client.
* @private
* @param {string} apiKey - The private api key for the site.
* @param {string} region - The private region for the site.
*/
class BaseClient {
constructor (apiKey) {
constructor (apiKey, options = {}) {
// API key should not be instance variable.
// This way it's not accidentally logged.
this._getApiKey = () => apiKey

let apiHost = API_HOSTS['us']

if (options.hasOwnProperty('region')) {
if (!(options.region in API_HOSTS)) {
throw new ApiError(`Invalid region type. Expected one of: ${Object.keys(API_HOSTS).join(', ')}`)
}
apiHost = API_HOSTS[options.region]
}

this._httpOptions = {
host: 'v3.recurly.com',
host: apiHost,
port: 443,
agent: new https.Agent({
keepAlive: true,
Expand Down
4 changes: 2 additions & 2 deletions test/mock_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const Pager = require('../lib/recurly/Pager')
const { Request, Response } = require('../lib/recurly/Http')

class MockClient extends BaseClient {
constructor (apiKey) {
super(apiKey)
constructor (apiKey, options = {}) {
super(apiKey, options)
this._sandbox = sinon.createSandbox()
}

Expand Down
16 changes: 16 additions & 0 deletions test/recurly/BaseClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ describe('BaseClient', () => {
assert.ok(userAgentRegex.test(client._getRequestOptions('GET', '/resources', {}).headers['User-Agent']))
assert.equal(client._getRequestOptions('GET', '/resources', {}).headers['Accept'], 'application/vnd.recurly.v2022-01-01')
})

it('should set host to EU DataCenter', () => {
const clientEU = new MockClient('myapikey', { 'region': 'eu' })
assert.equal(clientEU._httpOptions.host, 'v3.eu.recurly.com')
})

it('should set host to US DataCenter', () => {
assert.equal(client._httpOptions.host, 'v3.recurly.com')
})

it('should validate that region is an invalid value', () => {
assert.throws(() => {
const invalidClient = new MockClient('myapikey', { 'region': 'none' })
assert.equal(invalidClient._httpOptions.host, '')
}, recurly.ApiError)
})
})

describe('#_interpolatePath', () => {
Expand Down

0 comments on commit 35f13ff

Please sign in to comment.