As a data scientist you will come across many API, mostly you will find packages(API wrapper) and you dont have to go through the pain of parsing the raw API output. But now and than there might not be any wrapper for certain API and you have to write your own wrapper. Knowing how to write API wrapper is important skill that you need to learn as data scientist.
Output from API are mostly received as json or xml format. Output result usually have a lot of information and we have to parse the output.
Lets build a google maps geo gecoding API
Maps geocoding API allows us to get the geographic coordinates (lat and lng) plus other additional information like formatted address, administrative division of any address or google business name. E.g '09-mobile, yangon' is {'lat': 16.8300042, 'lng': 96.1297039}.
Important:
Read the API documentation, documentation is your best friends while querying API. You may have packages which does the job but still its good habit to go through original documents. Here is Geocoding Documentation.
As per document you need the key, you can get it through two way:
1. Go to your developer console make project/select exiting than enable the geocoding API and get the API key from credentials.
2. Or follow this link, its will guide you through.
Its good practice to get key using first method as first method can be used to enable any API and activate various credentials. Second method is specific design to get api key for geocoding API only.
Once you get the key, chek the structure of request and send the query using request library in python. Check the documents to see what parameters are allowed and how output will be return. Here is simple code.
Here the output of API query in Json(beautify):
You see json has alot of information, We only need location and formatted address. I like to think Json as combination of dict and list. My rule for parsing is simple, if nesting has { use key and if [ use index.
Let me show you example:
1. If you want to get 'status'
See the bracket that enclose 'status' , here its just {, so we do json['status']. Its good idea to beautify the json so that you know how elements are nested.
2. Lets say we want to get 'formatted_address'
Its under {'results':[{.., so you do json['results'][0]['formatted_address'].
3. Lets get 'location'
Its under {'results':[{'geometry'{.., so you do json['results'][0]['geometry']['location'].
Writing the wrapper is piece of cake if you know the API parameters and result structure.
Quote from Book I am reading:
“If you’re a lazy and not-too-bright computer scientist, machine learning is the ideal occupation, because learning algorithms do all the work but let you take all the credit.”― Pedro Domingos, The Master Algorithm
No comments:
Post a Comment