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

Move config parameters from class to instance #20 #51

Merged
merged 3 commits into from
Oct 14, 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ projects = client.projects.list_projects(offset=10, limit=20)
projects = client.projects.list_projects(page=2) # offset=25, limit=25
```

Alternatively, you can create an instance of the CrowdinClient class with params like this:

```python
from crowdin_api import CrowdinClient

# use the lower-case version of any of the constants above,
# at least provide token
client = CrowdinClient(token='__token__')

# ... continue as above

```

### Add a file

```python
Expand Down
42 changes: 32 additions & 10 deletions crowdin_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,42 @@
class CrowdinClient:
API_REQUESTER_CLASS: Type[APIRequester] = APIRequester

TIMEOUT: int = 60
RETRY_DELAY: Union[int, float] = 0.1 # 100ms
MAX_RETRIES: int = 5
HTTP_PROTOCOL: str = "https"
BASE_URL: str = "api.crowdin.com/api/v2/"

HEADERS = {}

ORGANIZATION: Optional[str] = None
TIMEOUT = 60
RETRY_DELAY = 0.1 # 100ms
MAX_RETRIES = 5
HTTP_PROTOCOL = "https"
TOKEN = None
ORGANIZATION = None
BASE_URL = "api.crowdin.com/api/v2/"
HEADERS = {}
USER_AGENT = "crowdin-api-client-python"
PAGE_SIZE = 25
HEADERS = {}

def __init__(self):
def __init__(self,
# TODO: replace this with union type expressions
# once we do not have to support <3.10 anymore
organization: Optional[str] =None,
token: Optional[str]=None,
base_url: Optional[str]=None,
user_agent: Optional[str]=None,
page_size: Optional[int]=None,
timeout: Optional[int]=None,
retry_delay: Union[int, float, None]=None,
max_retries: Optional[int]=None,
http_protocol: Optional[str]=None,
headers: Optional[dict]=None
):
self.ORGANIZATION=organization or self.ORGANIZATION
self.TOKEN=token or self.TOKEN
self.BASE_URL=base_url or self.BASE_URL
self.USER_AGENT=user_agent or self.USER_AGENT
self.PAGE_SIZE=page_size or self.PAGE_SIZE
self.TIMEOUT=timeout or self.TIMEOUT
self.RETRY_DELAY=retry_delay or self.RETRY_DELAY
self.MAX_RETRIES=max_retries or self.MAX_RETRIES
self.HTTP_PROTOCOL=http_protocol or self.HTTP_PROTOCOL
self.HEADERS=headers or self.HEADERS
self._api_requestor = None

if self.ORGANIZATION is None:
Expand Down
33 changes: 33 additions & 0 deletions crowdin_api/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ class TestClient(CrowdinClient):

assert TestClient().url == result

@pytest.mark.parametrize(
"http_protocol,organization,base_url,result",
(
(
"http",
None,
"api.crowdin.com",
"http://api.crowdin.com",
),
(
"https",
None,
"api.crowdin.com",
"https://api.crowdin.com",
),
(
"http",
"crowdin",
"api.crowdin.com",
"http://crowdin.api.crowdin.com",
),
(
"https",
"crowdin",
"api.crowdin.com",
"https://crowdin.api.crowdin.com",
),
),
)
def test_url_with_instance(self, http_protocol, organization, base_url, result):
client = CrowdinClient(http_protocol=http_protocol, base_url=base_url, organization=organization)
assert client.url == result

@pytest.mark.parametrize(
"headers,token,result",
(
Expand Down