From 2ac0d136cce649785c3c14c75cae4e320cfbc893 Mon Sep 17 00:00:00 2001 From: Adrien Date: Fri, 17 Feb 2023 12:51:10 +0100 Subject: [PATCH] Patch 1 fixing create project (#17) * More care at handling private projects and JsonApi exceptions not defining 'detail'. * --- --- pytransifex/api.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/pytransifex/api.py b/pytransifex/api.py index caab97a..14aa9cb 100644 --- a/pytransifex/api.py +++ b/pytransifex/api.py @@ -62,18 +62,36 @@ def create_project( project_name = project_slug try: - res = tx_api.Project.create( - name=project_name, - slug=project_slug, - source_language=source_language, - private=private, - organization=self.organization, - ) + if private: + return tx_api.Project.create( + name=project_name, + slug=project_slug, + source_language=source_language, + private=private, + organization=self.organization, + ) + else: + if repository_url := kwargs.get("repository_url", None): + return tx_api.Project.create( + name=project_name, + slug=project_slug, + source_language=source_language, + private=private, + repository_url=repository_url, + organization=self.organization, + ) + else: + raise ValueError( + f"Private projects need to pass a 'repository_url' (non-empty string) argument." + ) + logging.info("Project created!") - return res + except JsonApiException as error: - if "already exists" in error.detail: # type: ignore + if hasattr(error, "detail") and "already exists" in error.detail: # type: ignore return self.get_project(project_slug=project_slug) + else: + logging.error(f"Unable to create project; API replied with {error}") @ensure_login def get_project(self, project_slug: str) -> None | Resource: