Skip to content

Commit

Permalink
feat: project_id as property (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmAximani0 authored Oct 26, 2023
1 parent 4eac4e1 commit 05ff99b
Show file tree
Hide file tree
Showing 47 changed files with 1,040 additions and 212 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ from crowdin_api import CrowdinClient

class FirstCrowdinClient(CrowdinClient):
TOKEN = "__token__"
PROJECT_ID = 1 # Optional, set project id for all API's
ORGANIZATION = "organizationName" # Optional, for Crowdin Enterprise only
TIMEOUT = 60 # Optional, sets http request timeout.
RETRY_DELAY = 0.1 # Optional, sets the delay between failed requests
Expand Down
10 changes: 9 additions & 1 deletion crowdin_api/api_resources/abstract/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@


class BaseResource(metaclass=ABCMeta):
def __init__(self, requester: APIRequester, page_size=25):
def __init__(
self, requester: APIRequester, project_id: Optional[int] = None, page_size=25
):
self.requester = requester
self.project_id = project_id
self.page_size = page_size
self._flag_fetch_all = None
self._max_limit = None

def get_project_id(self):
if self.project_id is None:
raise ValueError("You must set project id for client")
return self.project_id

def _get_page_params(self, page: int):
if page < 1:
raise ValueError("The page number must be greater than or equal to 1.")
Expand Down
8 changes: 8 additions & 0 deletions crowdin_api/api_resources/abstract/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@


class TestBaseResource:
def test_get_project_id(self, base_absolut_url):
project_id = 1
resource = BaseResource(
requester=APIRequester(base_url=base_absolut_url),
project_id=project_id,
)
assert resource.get_project_id() == project_id

@pytest.mark.parametrize(
"in_params,out_params",
(
Expand Down
49 changes: 30 additions & 19 deletions crowdin_api/api_resources/bundles/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_bundles_exports_path(self, projectId: int, bundleId: int, exportId: Opti

def list_bundles(
self,
projectId: int,
projectId: Optional[int] = None,
offset: Optional[int] = None,
limit: Optional[int] = None,
):
Expand All @@ -43,6 +43,7 @@ def list_bundles(
"""

params = self.get_page_params(offset=offset, limit=limit)
projectId = projectId or self.get_project_id()

return self._get_entire_data(
method="get",
Expand All @@ -52,11 +53,11 @@ def list_bundles(

def add_bundle(
self,
projectId: int,
name: str,
format: str,
sourcePatterns: Iterable[str],
exportPattern: str,
projectId: Optional[int] = None,
ignorePatterns: Optional[Iterable[str]] = None,
isMultilingual: Optional[bool] = None,
includeProjectSourceLanguage: Optional[bool] = None,
Expand All @@ -73,6 +74,8 @@ def add_bundle(
https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.post
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="post",
path=self.get_bundles_path(projectId=projectId),
Expand All @@ -89,7 +92,7 @@ def add_bundle(
}
)

def get_bundle(self, projectId: int, bundleId: int):
def get_bundle(self, bundleId: int, projectId: Optional[int] = None):
"""
Get Bundle.
Expand All @@ -100,12 +103,14 @@ def get_bundle(self, projectId: int, bundleId: int):
https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.get
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="get",
path=self.get_bundles_path(projectId=projectId, bundleId=bundleId),
)

def delete_bundle(self, projectId: int, bundleId: int):
def delete_bundle(self, bundleId: int, projectId: Optional[int] = None):
"""
Delete Bundle.
Expand All @@ -116,12 +121,19 @@ def delete_bundle(self, projectId: int, bundleId: int):
https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.delete
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="delete",
path=self.get_bundles_path(projectId=projectId, bundleId=bundleId),
)

def edit_bundle(self, projectId: int, bundleId: int, data: Iterable[BundlePatchRequest]):
def edit_bundle(
self,
bundleId: int,
data: Iterable[BundlePatchRequest],
projectId: Optional[int] = None,
):
"""
Edit Bundle.
Expand All @@ -132,17 +144,16 @@ def edit_bundle(self, projectId: int, bundleId: int, data: Iterable[BundlePatchR
https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.patch
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="patch",
path=self.get_bundles_path(projectId=projectId, bundleId=bundleId),
request_data=data,
)

def download_bundle(
self,
projectId: int,
bundleId: int,
exportId: str
self, bundleId: int, exportId: str, projectId: Optional[int] = None
):
"""
Download bundle.
Expand All @@ -154,16 +165,14 @@ def download_bundle(
https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.exports.download.get
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="get",
path=f"{self.get_bundles_exports_path(projectId=projectId, bundleId=bundleId, exportId=exportId)}/download",
)

def export_bundle(
self,
projectId: int,
bundleId: int
):
def export_bundle(self, bundleId: int, projectId: Optional[int] = None):
"""
Export bundle.
Expand All @@ -174,16 +183,15 @@ def export_bundle(
https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.exports.post
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="post",
path=self.get_bundles_exports_path(projectId=projectId, bundleId=bundleId),
)

def check_bundle_export_status(
self,
projectId: int,
bundleId: int,
exportId: str
self, bundleId: int, exportId: str, projectId: Optional[int] = None
):
"""
Check Bundle Export Status.
Expand All @@ -195,15 +203,17 @@ def check_bundle_export_status(
https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.exports.get
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="get",
path=self.get_bundles_exports_path(projectId=projectId, bundleId=bundleId, exportId=exportId),
)

def get_bundle_list_files(
self,
projectId: int,
bundleId: int,
projectId: Optional[int] = None,
offset: Optional[int] = None,
limit: Optional[int] = None,
):
Expand All @@ -218,6 +228,7 @@ def get_bundle_list_files(
"""

params = self.get_page_params(offset=offset, limit=limit)
projectId = projectId or self.get_project_id()

return self._get_entire_data(
method="get",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class TestBundlesResource:
def get_resource(self, base_absolut_url):
return self.resource_class(requester=APIRequester(base_url=base_absolut_url))

def test_resource_with_id(self, base_absolut_url):
project_id = 1
resource = self.resource_class(
requester=APIRequester(base_url=base_absolut_url), project_id=project_id
)
assert resource.get_project_id() == project_id

@pytest.mark.parametrize(
"incoming_data, path",
(
Expand Down
12 changes: 10 additions & 2 deletions crowdin_api/api_resources/dictionaries/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DictionariesResource(BaseResource):

def list_dictionaries(
self,
projectId: int,
projectId: Optional[int] = None,
languageIds: Optional[Iterable[str]] = None,
page: Optional[int] = None,
offset: Optional[int] = None,
Expand All @@ -33,21 +33,29 @@ def list_dictionaries(

params = self.get_page_params(page=page, offset=offset, limit=limit)
params["languageIds"] = None if languageIds is None else ",".join(languageIds)
projectId = projectId or self.get_project_id()

return self._get_entire_data(
method="get",
path=f"projects/{projectId}/dictionaries",
params=params,
)

def edit_dictionary(self, projectId: int, languageId: str, data: Iterable[DictionaryPatchPath]):
def edit_dictionary(
self,
languageId: str,
data: Iterable[DictionaryPatchPath],
projectId: Optional[int] = None,
):
"""
Edit Dictionary.
Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.projects.dictionaries.patch
"""

projectId = projectId or self.get_project_id()

return self.requester.request(
method="patch",
path=f"projects/{projectId}/dictionaries/{languageId}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class TestDictionariesResource:
def get_resource(self, base_absolut_url):
return self.resource_class(requester=APIRequester(base_url=base_absolut_url))

def test_resource_with_project_id(self, base_absolut_url):
project_id = 1
resource = self.resource_class(
requester=APIRequester(base_url=base_absolut_url), project_id=project_id
)
assert resource.get_project_id() == project_id

@pytest.mark.parametrize(
"incoming_data, request_params",
(
Expand Down
Loading

0 comments on commit 05ff99b

Please sign in to comment.