Skip to content

Commit

Permalink
apply feedback
Browse files Browse the repository at this point in the history
  - add some documentation
  - wording
  - remove dead code, Zenpy doens't raise on failed authentication
  • Loading branch information
mbergeron committed Oct 1, 2018
1 parent 0164930 commit ba26107
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Tap for Zendesk

### Using OAuth

OAuth is the default authentication method for `tap-zendesk`. To use OAuth, you will need to fetch an `access_token` from a configured Zendesk integration. See https://support.zendesk.com/hc/en-us/articles/203663836 for more details on how to integrate your application with Zendesk.

**config.json**
```json
{
Expand All @@ -23,7 +25,7 @@ Tap for Zendesk

### Using API Tokens

Use the following configuration:
For a simplified, but less granular setup, you can use the API Token authentication which can be generated from the Zendesk Admin page. See https://support.zendesk.com/hc/en-us/articles/226022787-Generating-a-new-API-token- for more details about generating an API Token. You'll then be able to use the admins's `email` and the generated `api_token` to authenticate.

**config.json**
```json
Expand Down
82 changes: 43 additions & 39 deletions tap_zendesk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
"subdomain",
]

API_CONFIG_KEYS = REQUIRED_CONFIG_KEYS + [
"email",
"api_token",
]

OAUTH_CONFIG_KEYS = REQUIRED_CONFIG_KEYS + [
# default authentication
OAUTH_CONFIG_KEYS = [
"access_token",
]

# email + api_token authentication
API_TOKEN_CONFIG_KEYS = [
"email",
"api_token",
]

def do_discover(client):
LOGGER.info("Starting discover")
Expand Down Expand Up @@ -126,43 +127,46 @@ def do_sync(client, catalog, state, start_date):
singer.write_state(state)
LOGGER.info("Finished sync")

def oauth_auth():
try:
oauth_args = singer.utils.parse_args(OAUTH_CONFIG_KEYS)
creds = {
"subdomain": args['subdomain'],
"oauth_token": oauth_args.config['access_token'],
}

client = Zenpy(**creds)
LOGGER.info("OAuth authentication successful.")
return client
except ZenpyException:
LOGGER.error("OAuth authentication failed.")
except Exception:
LOGGER.error("Cannot find OAuth configuration.")

def api_token_auth():
try:
api_args = singer.utils.parse_args(API_CONFIG_KEYS)
creds = {
"subdomain": api_args.config['subdomain'],
"email": api_args.config['email'],
"token": api_args.config['api_token']
}

client = Zenpy(**creds)
LOGGER.info("API Token authentication successful.")
return client
except ZenpyException:
LOGGER.error("API Token authentication failed.")
except Exception:
LOGGER.error("Cannot find API Token configuration.")
def oauth_auth(args):
if not set(OAUTH_CONFIG_KEYS).issubset(args.config.keys()):
LOGGER.debug("OAuth authentication unavailable.")
return None

creds = {
"subdomain": args.config['subdomain'],
"oauth_token": args.config['access_token'],
}

client = Zenpy(**creds)
LOGGER.info("Using OAuth authentication.")
return client

def api_token_auth(args):
if not set(API_TOKEN_CONFIG_KEYS).issubset(args.config.keys()):
LOGGER.debug("API Token authentication unavailable.")
return None

creds = {
"subdomain": args.config['subdomain'],
"email": args.config['email'],
"token": args.config['api_token']
}

client = Zenpy(**creds)
LOGGER.info("Using API Token authentication.")
return client

@singer.utils.handle_top_exception(LOGGER)
def main():
parsed_args = singer.utils.parse_args(REQUIRED_CONFIG_KEYS)
client = oauth_auth() or api_token_auth()

# OAuth has precedence
client = oauth_auth(parsed_args) or api_token_auth(parsed_args)

if not client:
LOGGER.error("""No suitable authentication keys provided.
OAuth:\t\taccess_token
API Token:\t\temail, api_token""")

if parsed_args.discover:
do_discover(client)
Expand Down

0 comments on commit ba26107

Please sign in to comment.