From 087e9ad17042ad75a7e7645fd382bdb3d1bf5bc5 Mon Sep 17 00:00:00 2001 From: Halyna Tsekhmistro Date: Thu, 14 Nov 2024 12:03:56 +0200 Subject: [PATCH 1/2] add_PAT_auth: add PAT auth option --- README.md | 14 +++++++++++++- tap_asana/__init__.py | 40 ++++++++++++++++++++++++++++++++-------- tap_asana/asana.py | 12 +++++++++--- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0b3f847..a430755 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ $ pip install tap-asana 2. Create the config file - Create a JSON file called `config.json`. Its contents should look like: + Create a JSON file called `config.json`. In case of using OAuth credentials its contents should look like: ```json { @@ -45,6 +45,16 @@ $ pip install tap-asana } ``` + or in case of using Personal Access Token for auth in Asana API it should look like: + +```json +{ + "access_token": "yyy", + "start_date" : "2018-02-22T02:06:58.147Z", + "request_timeout": 300 +} +``` + The `start_date` specifies the date at which the tap will begin pulling data (for those resources that support this). @@ -52,6 +62,8 @@ $ pip install tap-asana The `request_timeout` specifies the timeout for the requests. Default: 300 + The `access_token` is a Personal Access Token (PAT) - the quickest and simplest way to authenticate in the API. PATs are generated in the Asana developer console. See the [PATs documentation](https://developers.asana.com/docs/personal-access-token) for more information. + 4. Run the Tap in Discovery Mode tap-asana -c config.json -d diff --git a/tap_asana/__init__.py b/tap_asana/__init__.py index c86aa03..a3d22c6 100644 --- a/tap_asana/__init__.py +++ b/tap_asana/__init__.py @@ -14,7 +14,7 @@ from tap_asana.context import Context import tap_asana.streams # Load stream objects into Context -REQUIRED_CONFIG_KEYS = [ +REQUIRED_CONFIG_KEYS_OAUTH = [ "start_date", "client_id", "client_secret", @@ -22,6 +22,10 @@ "refresh_token", ] +REQUIRED_CONFIG_KEYS_ACCESS_TOKEN = [ + "start_date", + "access_token", +] LOGGER = singer.get_logger() @@ -169,15 +173,35 @@ def main(): Run discover mode or sync mode. """ # Parse command line arguments - args = utils.parse_args(REQUIRED_CONFIG_KEYS) + try: + args = utils.parse_args(REQUIRED_CONFIG_KEYS_OAUTH) + except Exception as exc: + if "missing required keys" in str(exc): + args = utils.parse_args(REQUIRED_CONFIG_KEYS_ACCESS_TOKEN) + else: + raise Exception from exc + + # Set context. + if "access_token" in args.config: + creds = { + "access_token": args.config["access_token"], + } + else: + creds = { + "client_id": args.config["client_id"], + "client_secret": args.config["client_secret"], + "redirect_uri": args.config["redirect_uri"], + "refresh_token": args.config["refresh_token"], + } + # args = utils.parse_args(REQUIRED_CONFIG_KEYS) # Set context. - creds = { - "client_id": args.config["client_id"], - "client_secret": args.config["client_secret"], - "redirect_uri": args.config["redirect_uri"], - "refresh_token": args.config["refresh_token"], - } + # creds = { + # "client_id": args.config["client_id"], + # "client_secret": args.config["client_secret"], + # "redirect_uri": args.config["redirect_uri"], + # "refresh_token": args.config["refresh_token"], + # } # As we passed 'request_timeout', we need to add a whole 'args.config' rather than adding 'creds' Context.config = args.config diff --git a/tap_asana/asana.py b/tap_asana/asana.py index 105a36a..64bf95f 100644 --- a/tap_asana/asana.py +++ b/tap_asana/asana.py @@ -11,15 +11,21 @@ class Asana(): """Base class for tap-asana""" def __init__( - self, client_id, client_secret, redirect_uri, refresh_token, access_token=None + self, client_id=None, client_secret=None, redirect_uri=None, refresh_token=None, access_token=None ): # pylint: disable=too-many-arguments self.client_id = client_id self.client_secret = client_secret self.redirect_uri = redirect_uri self.refresh_token = refresh_token self.access_token = access_token - self._client = self._oauth_auth() or self._access_token_auth() - self.refresh_access_token() + + if all([self.refresh_token, self.client_id, self.client_secret, self.redirect_uri]): + self._client = self._oauth_auth() or self._access_token_auth() + self.refresh_access_token() + elif self.access_token: + self._client = self._access_token_auth() + else: + raise ValueError("Invalid configuration: Must provide either refresh_token with OAuth credentials or access_token.") def _oauth_auth(self): """Oauth authentication for tap""" From 6cab01e7facde3b6535820fc557956abeef29a65 Mon Sep 17 00:00:00 2001 From: Halyna Tsekhmistro Date: Thu, 14 Nov 2024 12:58:38 +0200 Subject: [PATCH 2/2] add_PAT_auth: remove commented lines --- tap_asana/__init__.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tap_asana/__init__.py b/tap_asana/__init__.py index a3d22c6..232614b 100644 --- a/tap_asana/__init__.py +++ b/tap_asana/__init__.py @@ -181,7 +181,6 @@ def main(): else: raise Exception from exc - # Set context. if "access_token" in args.config: creds = { "access_token": args.config["access_token"], @@ -193,16 +192,8 @@ def main(): "redirect_uri": args.config["redirect_uri"], "refresh_token": args.config["refresh_token"], } - # args = utils.parse_args(REQUIRED_CONFIG_KEYS) # Set context. - # creds = { - # "client_id": args.config["client_id"], - # "client_secret": args.config["client_secret"], - # "redirect_uri": args.config["redirect_uri"], - # "refresh_token": args.config["refresh_token"], - # } - # As we passed 'request_timeout', we need to add a whole 'args.config' rather than adding 'creds' Context.config = args.config Context.state = args.state