Asana Python Library

repository·master·Indexed 43 days ago

https://github.com/asana/python-asana

A Python client wrapper for the Asana REST API that allows developers to programmatically manage tasks, users, projects, goals, and other Asana resources. The library provides specialized API classes such as TasksApi, ProjectsApi, GoalsApi, and CustomFieldsApi to interact with the Asana platform via the https://app.asana.com/api/1.0 base URI.

Tokens
243.2K
Snippets
300
Records
348
Agent score
53%

What's inside Asana

  1. Overview of asana.RolesApi

    master
    The asana.RolesApi class provides methods to manage Role-Based Access Control (RBAC) roles within an Asana workspace. It allows you to create, delete, retrieve, and list roles. All requests are relative to https://app.asana.com/api/1.0.
  2. Overview of asana.GoalsApi

    master

    The asana.GoalsApi class provides methods to manage Asana Goals. It allows you to create, retrieve, update, and delete goals, as well as manage goal-specific features like custom field settings, followers (collaborators), and metrics.

    Available Methods

    MethodHTTP RequestDescription
    add_custom_field_setting_for_goalPOST /goals/{goal_gid}/addCustomFieldSettingAdd a custom field to a goal
    add_followersPOST /goals/{goal_gid}/addFollowersAdd a collaborator to a goal
    create_goalPOST /goalsCreate a goal
    create_goal_metricPOST /goals/{goal_gid}/setMetricCreate a goal metric
    delete_goalDELETE /goals/{goal_gid}Delete a goal
    get_goalGET /goals/{goal_gid}Get a goal
    get_goalsGET /goalsGet goals
    get_parent_goals_for_goalGET /goals/{goal_gid}/parentGoalsGet parent goals from a goal
    remove_custom_field_setting_for_goalPOST /goals/{goal_gid}/removeCustomFieldSettingRemove a custom field from a goal
    remove_followersPOST /goals/{goal_gid}/removeFollowersRemove a collaborator from a goal
    update_goalPUT /goals/{goal_gid}Update a goal
    update_goal_metricPOST /goals/{goal_gid}/setMetricCurrentValueUpdate a goal metric
  3. Manage budgets with asana.BudgetsApi

    master

    The asana.BudgetsApi class provides methods to create, retrieve, update, and delete budgets within Asana. All requests are relative to https://app.asana.com/api/1.0.

    Available Methods:

    • create_budget(body): Creates a new budget.
    • delete_budget(budget_gid): Deletes an existing budget.
    • get_budget(budget_gid, opts=None): Returns the complete record for a single budget.
    • get_budgets(parent): Gets all budgets for a specific parent (currently only supports project GIDs).
    • update_budget(body, budget_gid, opts=None): Updates an existing budget. Only fields provided in the body are changed.
  4. Use asana.MembershipsApi to manage memberships

    master

    The asana.MembershipsApi class provides methods to manage memberships for goal, project, portfolio, custom_type, or custom_field resources. Members can be either Teams or Users.

    Available operations:

    • create_membership: Create a new membership.
    • delete_membership: Remove an existing membership.
    • get_membership: Retrieve a specific membership record by its GID.
    • get_memberships: List multiple memberships with filtering and pagination.
    • update_membership: Update fields of an existing membership.
  5. Manage tags with asana.TagsApi

    master

    The asana.TagsApi class provides methods to manage tags within Asana, including creating, retrieving, updating, and deleting them.

    Available Methods:

    • create_tag(body, opts): Create a new tag.
    • create_tag_for_workspace(body, workspace_gid, opts): Create a tag specifically within a workspace or organization.
    • delete_tag(tag_gid): Delete an existing tag.
    • get_tag(tag_gid, opts): Retrieve a single tag's full record.
    • get_tags(opts): Retrieve multiple tags (returns a generator).
    • get_tags_for_task(task_gid): Get tags associated with a specific task.
    • get_tags_for_workspace(workspace_gid): Get all tags in a workspace.
    • update_tag(tag_gid, body, opts): Update an existing tag.
  6. Configure environment variables for testing

    master

    The local test suite requires a .env file in the root directory containing the following environment variables.

    Note on TEXT_CUSTOM_FIELD_GID: Ensure that at least one task in your Asana workspace has this custom field, and that the value of this custom field on that task is exactly custom_value.

    PERSONAL_ACCESS_TOKEN=<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>
    TEAM_GID=<YOUR_TEAM_GID>
    TEXT_CUSTOM_FIELD_GID=<YOUR_TEXT_CUSTOM_FIELD_GID>
    USER_GID=<YOUR_USER_GID>
    WORKSPACE_GID=<YOUR_WORKSPACE_GID>
  7. Install the asana Python library

    master

    You can install the asana package using pip or via Setuptools. The library requires Python 3.4 or higher.

    Using pip:

    pip install asana

    Using Setuptools:

    python setup.py install --user

    After installation, you can use it in your code with import asana.

    pip install asana
  8. Debug Swagger Codegen templates and models

    master

    When customizing templates (e.g., api.mustache), you can use debug flags during the java execution to inspect the data being passed to the template engine. This helps you identify which object properties are available for use in your templates.

    Debug Flags

    Pass these flags to the java command to inspect different parts of the generation process:

    • -DdebugSwagger: Prints the OpenAPI Specification as interpreted by the codegen.
    • -DdebugModels: Prints models passed to the template engine.
    • -DdebugOperations: Prints operations passed to the template engine.
    • -DdebugSupportingFiles: Prints additional data passed to the template engine.

    Example: Debugging Operations

    To see the data available for operations (useful when editing api.mustache), run:

    java -DdebugOperations -cp /path/to/swagger-codegen-cli.jar:/path/to/your.jar io.swagger.codegen.v3.cli.SwaggerCodegen -l AsanaPythonClientCodegen -i /path/to/swagger.yaml -o ./test
    java -DdebugOperations -cp /path/to/swagger-codegen-cli.jar:/path/to/your.jar io.swagger.codegen.v3.cli.SwaggerCodegen -l AsanaPythonClientCodegen -i /path/to/swagger.yaml -o ./test
  9. Add asana-enable or asana-disable headers

    master

    You can add deprecation flags using the asana-enable or asana-disable headers. You can do this globally on the ApiClient or specifically for a single request using header_params.

    # Option 1: Add asana-enable header for the entire client
    api_client.default_headers['asana-enable'] = 'string_ids'
    
    # Option 2: Add asana-enable header for a specific request
    api_response = users_api_instance.get_user(user_gid, opts, header_params={'asana-enable': 'string_ids'})
  10. Initialize the Asana API client

    master

    To interact with the Asana API, you must create a Configuration object, set your access_token, and then instantiate an ApiClient. This client is then passed to specific API class instances (like UsersApi or TasksApi) to perform operations.

    import asana
    
    configuration = asana.Configuration()
    configuration.access_token = '<YOUR_ACCESS_TOKEN>'
    api_client = asana.ApiClient(configuration)
  11. Make asynchronous requests with multithreading

    master

    The client library supports asynchronous requests via multithreading by passing async_req=True to the method call.

    Important Notes:

    • This feature disables auto-pagination.
    • You must implement your own logic to handle Asana API rate limits.
    import asana
    from asana.rest import ApiException
    from pprint import pprint
    
    configuration = asana.Configuration()
    configuration.access_token = '<YOUR_ACCESS_TOKEN>'
    api_client = asana.ApiClient(configuration)
    
    tasks_api_instance = asana.TasksApi(api_client)
    threads = []
    for i in range(1, 15+1):
        body = {
            "data": {
                "name": f"Task: {i}",
                "projects": ["<YOUR_PROJECT_GID>"]
            }
        }
        opts = {}
        threads.append(tasks_api_instance.create_task(body, opts, async_req=True))
    
    for thread in threads:
        try:
            pprint(thread.get())
        except ApiException as e:
            print("Exception when calling TasksApi->create_task: %s\n" % e)