Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add region argument to client to connect in EU data center #529

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 API in Europe, you will need to specify the EU Region in the argument region.

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

### Operations

The Client contains every `operation` you can perform on the site as a list of methods.
Expand Down
21 changes: 18 additions & 3 deletions recurly/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
from pydoc import locate
import urllib.parse
from datetime import datetime
from enum import Enum

PORT = 443
HOST = "v3.recurly.com"
BINARY_TYPES = ["application/pdf"]
ALLOWED_OPTIONS = ["body", "params", "headers"]
API_HOSTS = {"us": "v3.recurly.com", "eu": "v3.eu.recurly.com"}
HOST = API_HOSTS["us"]


def request_converter(value):
Expand All @@ -26,10 +28,23 @@ def request_converter(value):


class BaseClient:
def __init__(self, api_key, timeout=None):
def __init__(self, api_key, timeout=None, **options):
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)
api_host = HOST

if "region" in options:
if options["region"] not in API_HOSTS:
raise TypeError(
"Invalid region type. Expected one of: %s"
% (", ".join(API_HOSTS.keys()))
)

api_host = API_HOSTS[options["region"]]

self.__conn = http.client.HTTPSConnection(
api_host, PORT, timeout=actual_timeout
)

def _make_request(self, method, path, body, **options):
try:
Expand Down
16 changes: 16 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 API_HOSTS
import sys


Expand Down Expand Up @@ -269,3 +270,18 @@ 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_default_region(self):
client = MockClient("apikey")
self.assertEqual(client.__dict__["_BaseClient__conn"].host, API_HOSTS["us"])

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

def test_client_set_invalid_region(self):
with self.assertRaises(TypeError) as e:
client = MockClient("apikey", region="none")

err = e.exception
self.assertEqual(str(err), "Invalid region type. Expected one of: us, eu")