Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

When to use types vs dicts with this library #148

@jceresini

Description

@jceresini

I've had a hard time using this library. I first used an example I found to create a task using this simple dictionary

I found the example by going here https://cloud.google.com/tasks/docs/quickstart which linked me to here https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/flexible/tasks/create_app_engine_queue_task.py

The example looks like this:

    task = {
            'app_engine_http_request': {  # Specify the type of request.
                'http_method': tasks_v2.HttpMethod.POST,
                'relative_uri': '/example_task_handler'
            }
    }

I modified it to be an http_request rather than app_engine_http_request after reading through the docs here https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks/create. I also want to use the OAuthToken feature so I can have cloud tasks trigger a call to another google cloud API. Because the http_method above uses tasks_v2_HttpMethod.POST, I assumed I needed to use tasks_v2.OAuthToken() in my task definition. I ended up with this (I'm including a full snippet, but note the task definition):

from google.cloud import tasks_v2

client = tasks_v2.CloudTasksClient()

task = {
    "http_request": {
        "http_method": tasks_v2.HttpMethod.POST,
        "url": "https://pubsub.googleapis.com/v1/projects/my-project/topics/testtopic:publish",
        "body": b"eyJtZXNzYWdlcyI6IFt7ImRhdGEiOiAiVkdocGN5QnBjeUJoSUhSbGMzUUsifV19Cg==",
        "oauth_token": tasks_v2.OAuthToken(service_account_email='[email protected]'),
    }
}

parent = 'projects/my-project/locations/us-central1/queues/my-queue'

resp = client.create_task(parent=parent, task=task)

But when I run that code I get the following error:

Traceback (most recent call last):
  File "/path/to/example_tasks.py", line 18, in <module>
    resp = client.create_task(parent=parent, task=task)
  File "/path/to/.venv/lib/python3.9/site-packages/google/cloud/tasks_v2/services/cloud_tasks/client.py", line 1700, in create_task
    request.task = task
  File "/path/to/.venv/lib/python3.9/site-packages/proto/message.py", line 632, in __setattr__
    pb_value = marshal.to_proto(pb_type, value)
  File "/path/to/.venv/lib/python3.9/site-packages/proto/marshal/marshal.py", line 208, in to_proto
    pb_value = rule.to_proto(value)
  File "/path/to/.venv/lib/python3.9/site-packages/proto/marshal/rules/message.py", line 32, in to_proto
    return self._descriptor(**value)
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.cloud.tasks.v2.OAuthToken got OAuthToken.

That error seems odd, because it looks like I passed it what it expected.I suspect that is a bug, but below you'll see I got it working with a dict so I wasn't sure... so I submitted this as a support request rather than a bug.

After some fumbling around I noticed if I pass it a dictionary it seems to work. The example payload here is:

task = {
    "http_request": {
        "http_method": tasks_v2.HttpMethod.POST,
        "url": "https://pubsub.googleapis.com/v1/projects/my-project/topics/testtopic:publish",
        "body": b"eyJtZXNzYWdlcyI6IFt7ImRhdGEiOiAiVkdocGN5QnBjeUJoSUhSbGMzUUsifV19Cg==",
        "oauth_token": {"service_account_email":"[email protected]"},
    }
}

The documentation here isn't very helpful either: https://googleapis.dev/python/cloudtasks/latest/

I'm working on a POC using CloudTasks, and I'm making progress by testing various things, and trying to read through the code, but it been a bit frustrating so far.

Metadata

Metadata

Assignees

Labels

api: cloudtasksIssues related to the googleapis/python-tasks API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions