Providing Solutions For Life

Google APIs for python - don't feel unfortunate anymore if you have to work on these

From the past 4 years I've been unfortunate enough to work on Google API's which are those for which even Google cannot search their documentation, may be because it doesn't exist, stackoverflow no matter how much it overflows cannot give us anything much on these.

Below is the summary of all my experience to crack the Google APIs.

I've spent days together to crack one thing at times and I wished I had known these tricks so that it wouldn't have taken me this long.

So firstly try to read as much as possible from the getting started guide. This will take you to a world of links, go to those link and grab in what ever is possible (don't worry if you don't understand anything). [trade secret of software engineers is make the simple sound as complex as possible, and intimidate others!]

But the world of python is different! 
"Simple is better than complex." -- zen of python

So the magical commands in python are :-

help() (keeping the best for the last part)

also inspect.getargspec()

Lets discover each one of them:-

dir() - To list the names of built-in functions and variables. 

(though it doesn't give a complete list. If you want a list of those use the standard module __builtin__ eg. dir(__builtin__)
For example if you have a class and instantiated an object for eg.
service ='admin', 'reports_v1', http=http)
and you want to know what are the functions this object can give us.
just run dir(service) to get the list as below, (ignore the u' they are the UTF encoding stuff)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_basic_methods', '_add_nested_resources', '_add_next_methods', '_baseUrl', '_developerKey', '_dynamic_attrs', '_http', '_model', '_requestBuilder', '_resourceDesc', '_rootDesc', '_schema', '_set_dynamic_attr', '_set_service_methods', u'activities', u'customerUsageReports', u'userUsageReport']
so we can see that the above result there could be 3 methods. Now to know what methods are there inside these methods, we can do this dir(service.customerUsageReports()) and get a list
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_basic_methods', '_add_nested_resources', '_add_next_methods', '_baseUrl', '_developerKey', '_dynamic_attrs', '_http', '_model', '_requestBuilder', '_resourceDesc', '_rootDesc', '_schema', '_set_dynamic_attr', '_set_service_methods', u'get', u'get_next']
so we come to see that there are two more methods get and get_next for us to use!

Now if at some point you would like to know what the datatype of the object is use 


this will give the type of the object eg. <class 'apiclient.discovery.Resource'>
Now for the best one help(), my fascination for python has increased all the more after knowing about this command. It's awesome. See how it can help us
Now how do we know in the really mediocre documented Google world, what are the parameters "get method" takes


gives the below
method(self, **kwargs) method of apiclient.discovery.Resource instance    Retrieves a report which is a collection of properties / statistics for a specific customer.        Args:      date: string, Represents the date in yyyy-mm-dd format for which the data is to be fetched. (required)      pageToken: string, Token to specify next page.      parameters: string, Represents the application name, parameter name pairs to fetch in csv as app_name1:param_name1, app_name2:param_name2.        Returns:      An object of the form:            { # JSON template for a collection of usage reports.        "nextPageToken": "A String", # Token for retrieving the next page        "kind": "admin#reports#usageReports", # Th

seeing this we know that it takes the date parameter in a certain string format. This saves lives! :)

Adding to the above, we see the below which could also be used and might help, though I haven't been much helped by this

 import inspect

Get the names and default values of a Python function’s arguments. A tuple of four things is returned: (args, varargs, keywords, defaults). args is a list of the argument names (it may contain nested lists).

No more head breaking folks!  

instead break the code!