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_PAT_auth: add PAT auth option #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -45,13 +45,25 @@ $ 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).

The `client_id`, `client_secret`, `redirect_uri`, and `refresh_token` can be generated following these [Asana OAuth instructions](https://developers.asana.com/docs/oauth).

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
Expand Down
33 changes: 24 additions & 9 deletions tap_asana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
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",
"redirect_uri",
"refresh_token",
]

REQUIRED_CONFIG_KEYS_ACCESS_TOKEN = [
"start_date",
"access_token",
]

LOGGER = singer.get_logger()

Expand Down Expand Up @@ -169,16 +173,27 @@ 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.
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"],
}
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"],
}

# Set context.
# 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
Expand Down
12 changes: 9 additions & 3 deletions tap_asana/asana.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down