Asana / python-asana

Official Python client library for the Asana API v1
MIT License
303 stars 105 forks source link

The API instance requests return a complete response instead of adhering to the definition in opt_fields #181

Open DianaGonzalezMorales opened 1 year ago

DianaGonzalezMorales commented 1 year ago

The response using the notation "api_instance = asana.TasksApi(api_client)" for any resource type, generates a complete response instead of the one defined by opt_fields. I tried using "api_client.call_api," and there was no issue.

Screenshot_1

Screenshot_2

Python version 3.11.4 python-asana version: 4.0.10

jv-asana commented 1 year ago

Hi @DianaGonzalezMorales,

This isn't particularly a bug. If you take a look at the response for your # USING THE API INSTANCE request, only the properties that you've asked for in the opt_fields query parameters you provided have values and the other properties are None. The reason for this behavior is because of how the Asana API works with python classes. If you check the response type of each approach (i.e. print(type(api_response))) you will notice that:

# USING THE API INSTANCE returns a <class 'asana.models.task_response_data.TaskResponseData'>

# USING THE API CLIENT returns a <class 'dict'>

Because the approach # USING THE API INSTANCE returns a class object, we needed a way to make class object responses support our API's opt_fields feature. In order to do that we had to define all the possible properties that a resource could return such that when the API returns a response query that uses opt_fields the SDK is able to map the values for those properties.

At a high level this is how our SDK works for the case # USING THE API INSTANCE

Request -> Class -> HTTP Call -> Asana API
Class Response <- Class <- JSON Response <- ASANA API

If we did not define all the possible properties for the resource class what will happen is when a user makes a query with opt_fields using the approach # USING THE API INSTANCE-> the Asana API will return a response with those opt_fields value but since those properties don't have a mapping to a property in the class definition the SDK will throw an error.

For the # USING THE API CLIENT case it returns an object because you can think if it as:

Request -> HTTP call -> Asana API
Dict Response <- Dict <- JSON Response <- Asana API

This is why you see that # USING THE API CLIENT returns what you expected and why # USING THE API INSTANCE has extra data.

As you can see this is a tricky problem to solve because of our opt_fields feature which doesn't behave like other APIs. One potential issue that can show up is if you try to access a property that you did not ask for with opt_fields -> by default that property is None but in reality if you had made a request with opt_fields for that property it probably has a value other than None. This is why it is important to remember what properties you can access.

If you have any feedback/suggestions please let us know.