Opt_pretty not working on python library

I recently installed the Asana library version 3.2.2 for Python. I have been following the official documentation, and everything works, with the exception of the opt_pretty option. I’ve read the code and traced the problem to the json() function in the Response object. When it calls complexjson.loads() , it always removes its new lines. I have searched, but I haven’t found anything about whether this is normal behavior or not. However, when I call the text property (a function with the property decorator) or decode it, it returns the response with its new lines. So, there must be a way, I think?


Is this a real problem, or am I just not passing an argument when calling client.workspaces.get_workspaces(opt_pretty=True) or when creating the Client? This is the code I am running.

import asana
import os

PAT = os.getenv('ASANAPAT')

client = asana.Client.access_token(f'{PAT}')
client.headers={
    'Asana-Enable': "new_user_task_lists",
    'Asana-Disable': "new_goal_memberships"}

workspaces = client.workspaces.get_workspaces(opt_pretty=True)
workspaces = list(workspaces)

print("\nTEST FILE: ", workspaces)
1 Like

Hi @Antony_S,

It looks like you went deep into debugging our python v3.2.2 library. The def json(self, **kwargs): code that you shared in your screen shot is from the python requests library (i.e., this is an external library not managed by us) that our python client library uses to make requests.

From testing and looking at our code I don’t think there is a point in specifying the opt_pretty argument since the underlying python requests library that we are using to make the requests seems to be removing the white spaces (as you have discovered) that our API returns with opt_pretty.

Is there a reason why you would like to use the opt_pretty API feature? We typically don’t recommend specifying the opt_pretty query param unless you are debugging with our API because"This will take extra time and increase the response size so it is advisable only to use this during debugging.".

Have you tried using python’s pretty print (i.e., pprint)? Here’s a sample code:

import asana
import os
from pprint import pprint

PAT = os.getenv('ASANAPAT')

client = asana.Client.access_token(f'{PAT}')
client.headers={
    'Asana-Enable': "new_user_task_lists",
    'Asana-Disable': "new_goal_memberships"}

workspaces = client.workspaces.get_workspaces(opt_pretty=True)
workspaces = list(workspaces)

print("\nTEST FILE: ")
pprint(workspaces)

Sample output:

TEST FILE: 
[{'gid': '1205952416374495',
  'name': 'My Workspace',
  'resource_type': 'workspace'}]
2 Likes

So, it’s a real bug, I had no reason to use opt_pretty, other than, as you said, debugging, but does version 4.x of the code solve this? Also, I was using json.dump to indent the result but pprint looks easy and better thanks!

Oh yes json.dump also works. Our version 4.x will have the use of opt_pretty removed from the sample code. I don’t think opt_pretty has a use for client libraries since you can do things like pretty print and json.dump. I think most request libraries will try to remove the underlying white spaces that opt_pretty returns from our API anyways so it makes sense for us not to document it being used with our client libraries.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.