Skip to content

Commit

Permalink
Add region to client
Browse files Browse the repository at this point in the history
  • Loading branch information
FabricioCoutinho committed Jan 24, 2022
1 parent e80e1c2 commit 8d8fae9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ api_key = '83749879bbde395b5fe0cc1a5abf8e5'
client = recurly.Client(api_key)
```

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

```python
api_key = '83749879bbde395b5fe0cc1a5abf8e5'
client = recurly.Client(api_key, "eu")
```

### Operations

The Client contains every `operation` you can perform on the site as a list of methods.
Expand Down
8 changes: 6 additions & 2 deletions recurly/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

PORT = 443
HOST = "v3.recurly.com"
EU_HOST = "v3.eu.recurly.com"
BINARY_TYPES = ["application/pdf"]
ALLOWED_OPTIONS = ["body", "params", "headers"]

Expand All @@ -26,10 +27,13 @@ def request_converter(value):


class BaseClient:
def __init__(self, api_key, timeout=None):
def __init__(self, api_key, timeout=None, region="us"):
self.__api_key = api_key
actual_timeout = timeout if timeout is not None else DEFAULT_REQUEST_TIMEOUT
self.__conn = http.client.HTTPSConnection(HOST, PORT, timeout=actual_timeout)

self.__conn = http.client.HTTPSConnection(
EU_HOST if region == "eu" else HOST, PORT, timeout=actual_timeout
)

def _make_request(self, method, path, body, **options):
try:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest.mock import Mock, MagicMock
from datetime import datetime
from collections import OrderedDict
from recurly.base_client import EU_HOST, HOST
import sys


Expand Down Expand Up @@ -269,3 +270,15 @@ def test_client_can_set_timeout(self):
timeout = 3
client = MockClient("apikey", timeout=timeout)
self.assertEqual(client.__dict__["_BaseClient__conn"].timeout, timeout)

def test_client_set_region_us(self):
client = MockClient("apikey")
self.assertEqual(client.__dict__["_BaseClient__conn"].host, HOST)

def test_client_set_region_eu(self):
client = MockClient("apikey", region="eu")
self.assertEqual(client.__dict__["_BaseClient__conn"].host, EU_HOST)

def test_client_set_region_unrelated(self):
client = MockClient("apikey", region="as")
self.assertEqual(client.__dict__["_BaseClient__conn"].host, HOST)

0 comments on commit 8d8fae9

Please sign in to comment.