asana Build Status PyPi Version

Python client library for Asana.

Authentication

Personal Access Token

Create a client using your Asana Personal Access Token:

client = asana.Client.access_token('PERSONAL_ACCESS_TOKEN')

OAuth 2

Asana supports OAuth 2. asana handles some of the details of the OAuth flow for you.

Create a client using your OAuth Client ID and secret:

client = asana.Client.oauth(
  client_id='ASANA_CLIENT_ID',
  client_secret='ASANA_CLIENT_SECRET',
  redirect_uri='https://yourapp.com/auth/asana/callback'
)

Redirect the user to the authorization URL obtained from the client's session object:

(url, state) = client.session.authorization_url()

When the user is redirected back to your callback, check the state URL parameter matches, then pass the code parameter to obtain a bearer token:

if request.params['state'] == state:
  token = client.session.fetch_token(code=request.params['code'])
  # ...
else:
  # error! possible CSRF attack

Note: if you're writing a non-browser-based application (e.x. a command line tool) you can use the special redirect URI urn:ietf:wg:oauth:2.0:oob to prompt the user to copy and paste the code into the application.

Usage

The client's methods are divided into several resources: attachments, events, jobs, portfolios, portfolio_memberships, projects, project_memberships, stories, tags, tasks, teams, users, user_task_lists, and workspaces.

Methods that return a single object return that object directly:

me = client.users.me()
print "Hello " + me['name']

workspace_id = me['workspaces'][0]['gid']
project = client.projects.create_in_workspace(workspace_id, { 'name': 'new project' })
print "Created project with id: " + project['gid']

Methods that return multiple items (e.x. find_all) return a page iterator by default. See the "Collections" section.

See the gen folder for methods available for each resource.

Options

Various options can be set globally on the Client.DEFAULTS object, per-client on client.options, or per-request as additional named arguments. For example:

# global:
asana.Client.DEFAULTS['page_size'] = 100

# per-client:
client.options['page_size'] = 100

# per-request:
client.tasks.find_all({ 'project': 1234 }, page_size=100)

Available options

Collections (methods returning an array as its 'data' property):

Events:

Asana Change Warnings

You will receive warning logs if performing requests that may be affected by a deprecation. The warning contains a link that explains the deprecation.

If you receive one of these warnings, you should:

You can place it on the client for all requests, or place it on a single request.

client.headers={'asana-enable': 'string_ids'}
or
me = client.users.me(headers={'asana-enable': 'string_ids'})

If you would rather suppress these warnings, you can set

client.LOG_ASANA_CHANGE_WARNINGS = False

Collections

Items Iterator

By default, methods that return a collection of objects return an item iterator:

workspaces = client.workspaces.find_all(item_limit=1)
print workspaces.next()
print workspaces.next() # raises StopIteration if there are no more items

Or:

for workspace in client.workspaces.find_all()
  print workspace

Raw API

You can also use the raw API to fetch a page at a time:

offset = None
while True:
  page = client.workspaces.find_all(offset=offset, iterator_type=None)
  print page['data']
  if 'next_page' in page:
    offset = page['next_page']['offset']
  else:
    break

Contributing

Feel free to fork and submit pull requests for the code! Please follow the existing code as an example of style and make sure that all your code passes lint and tests.

Code generation

The specific Asana resource classes under gen (_Tag, _Workspace, _Task, etc) are generated code, hence they shouldn't be modified by hand.

Deployment

Repo Owners Only. Take the following steps to issue a new release of the library.

Automatic Deployment

Run deploy.py [version]. See deploy.py -h for additional info.

Manual Deployment

  1. Merge in the desired changes into the master branch and commit them.
  2. Clone the repo, work on master.
  3. Edit package version in asana/__init__.py to indicate the semantic version change.
  4. Commit the change
  5. Tag the commit with v plus the same version number you set in the file. git tag v1.2.3
  6. Push changes to origin, including tags: git push origin master --tags

Travis CI will automatically build and deploy the tagged release to pypi.