Tuesday, July 13, 2021

APIs

What is an API? 

API (Application Programming Interface) is a collection of tools that allows different applications to interact. 

In other words, an API is a set of function that allows application to access data and interact with external software components, operating systems, servers or micro-services.

To put it in a more simple way, an API delivers a users response to the system and send the system's response back to the user.

For example, you click on "Add to cart" in an Amazon website; an API tells the website you added an item to your cart and the website puts the item in your cart and your cart is updated.


Everyday Examples of API

  • Searching for hotels
    • A travel site may be linked to 10 other travel sites to find the best deals for you. When you input details like Victoria, 2 nights, 1 room, 2 adults, you send this request to those 10 different travel sites. The API takes your request with the inputs you have provided and pings the 10 sites, who in return,  send back the deals they have found. You then pick the best fit. Here the API is a messenger for your requests.
  • Finding a Facebook profile
    • Want to find your long lost childhood friend on Facebook? Just type in their name in the search box and the Facebook API informs Facebook's servers that you are looking for that specific name you have given. Facebook then gives you a list of all the profiles that match that name.
  • Finding a new restaurant
    • Let's say you are just bored of cooking and also in the mood of trying out something new. You grab your smartphone and look up restaurants near by. In a fraction of seconds, you are shown a dozen of restaurants near your home. Thanks to Google API; they are able to easily display all relevant information including ratings, business hours and even time they are likely to be busy.
  • Staying up to date on Twitter
    • You just want to know what's happening in the world of sports. You know that Twitter is the fastest news provider nowadays. So you open up Twitter and navigate to the 'Sports' section in your Twitter profile. Twitter's API allows you to easily see various tweets relating to Sports. You are even able to share your favorite team's score by retweeting the final score. From here Twitter knows how to take this tweet and display it to everyone who follows you.
Some of the biggest names on the web like Reddit, Spotify, Twitter and Facebook offer free APIs and make data available. Other services and companies charge for these APIs.

Using APIs, we can access web data, analyze the data, generate insights and make predictions.

API requests

APIs are hosted on web servers. API requests are essentially your program asking for data. The API usually returns this data in JSON format. We make an API request to the web servers with the data we want. The server then responds. In Python, API requests are made using the requests library. 

Requests is an elegant and simple HTTP library for Python, built for human beings.

GET request

There are many types of API requests. The most common is a GET request which we use for Data Collection.

Usage Example:

We can use a simple GET request to collect information from the OpenNotify API.

OpenNotify Endpoints:

http://api.open-notify.org/iss-now.json
http://api.open-notify.org/astros.json

An endpoint is a server route for retrieving specific data from an API.

iss-now.json endpoint gets the current latitude and longitude position of the ISS. 
Why not use a dataset for this task, you may ask? A dataset wouldn't be a good tool for this task because the information often changes and it involves some calculation on the server.

script.py
Variables
status_codeint (<class 'int'>)
200
responseResponse (<class 'mock_requests.Response'>)
<mock_requests.Response at 0x7f56498565c0>

Understanding Status Codes

Web servers returns status codes every time they receive an API request. A status code reports what happened with a request. Here are some of the codes that are relevant to GET requests:

  • 200 - Everything went OK and the server returned a result (if any)
  • 301 - The server is redirecting you to a different endpoint. This can happen when a company moves to a different domain or when an endpoint's name has changed.
  • 401 - The server thinks you are not authenticated.
  • 400 - The server thinks you made a bad request. 
  • 403 - The resource you're trying to access is forbidden and you do not have the right permissions to see it.
  • 404 - The server didn't find the resources you tried to access.
Examples:

script.py
Variables
responseResponse (<class 'mock_requests.Response'>)
<mock_requests.Response at 0x7f56497a1470>
status_codeint (<class 'int'>)
404

script.py
Variables
responseResponse (<class 'mock_requests.Response'>)
<mock_requests.Response at 0x7f56497b0898>
status_codeint (<class 'int'>)
400

Adding query parameters as a dictionary

If we look at the documentation for the OpenNotify API, we see that the ISS Pass endpoint needs to parameters.

The ISS Pass endpoint tells us when the ISS will pass over a given location on the Earth. To request this information, we need to pass the coordinates for a specific location to the API. We do this by passing two parameters: latitude and longitude.

To do this, we can add an optional keyword argument, 'params', to our request. In this case, we need two parameters longitude and latitude. We can make a dictionary that contains these parameters and then pass them into the function.

We can also do the same thing directly by adding the query parameters to the url like this: http://api.open-notify.org/iss-pass.json?lat=40.71&lon=-74

However, it's always better to set up the parameters as a dictionary because the requests library takes care of the formatting.

script.py
Output
b'{\n "message": "success", \n "request": {\n "altitude": 100, \n "datetime": 1441417753, \n "latitude": 40.71, \n "longitude": -74.0, \n "passes": 5\n }, \n "response": [\n {\n "duration": 330, \n "risetime": 1441445639\n }, \n {\n "duration": 629, \n "risetime": 1441451226\n }, \n {\n "duration": 606, \n "risetime": 1441457027\n }, \n {\n "duration": 542, \n "risetime": 1441462894\n }, \n {\n "duration": 565, \n "risetime": 1441468731\n }\n ]\n}' b'{\n "message": "success", \n "request": {\n "altitude": 100, \n "datetime": 1441417753, \n "latitude": 40.71, \n "longitude": -74.0, \n "passes": 5\n }, \n "response": [\n {\n "duration": 329, \n "risetime": 1441445639\n }, \n {\n "duration": 629, \n "risetime": 1441451226\n }, \n {\n "duration": 606, \n "risetime": 1441457027\n }, \n {\n "duration": 542, \n "risetime": 1441462894\n }, \n {\n "duration": 565, \n "risetime": 1441468731\n }\n ]\n}' b'{\n "message": "success", \n "request": {\n "altitude": 100, \n "datetime": 1441417753, \n "latitude": 37.78, \n "longitude": -122.41, \n "passes": 5\n }, \n "response": [\n {\n "duration": 369, \n "risetime": 1441456672\n }, \n {\n "duration": 626, \n "risetime": 1441462284\n }, \n {\n "duration": 581, \n "risetime": 1441468104\n }, \n {\n "duration": 482, \n "risetime": 1441474000\n }, \n {\n "duration": 509, \n "risetime": 1441479853\n }\n ]\n}' <class 'bytes'>

JSON Format

API lets a developer make a specific "call" or "request" in order to send or receive data. This communication is done using a programming language called "JSON"(JavaScript Object Notation). 
In other words, JSON is the main format for sending and receiving data through APIs.

It can be used to make a defined action such as updating or deleting data.

There are four basic request methods that can be made with API:

They are:
  • GET - Gathers data (Pulling all coupon codes)
  • PUT - Updates pieces of data (Updating product pricing)
  • POST - Creates (Creating a new product category)
  • DELETE - Deletes (Deleting a blog post)

So what is JSON and why is it used?

JSON is used to represent data on a server. It is fairly easy to read by humans and easy for machines/applications to understand. It is outputted in key/value pairs. Keys are fixed objects defined by the application and will remain the same. The values will be associated to the key. For example, Subway is the Key and 24722 is the value.

Example:

{
    "Subway": 24722,
    "McDonalds": 14098,
    "Starbucks": 10821,
    "Pizza Hut": 7600
}

Python json library

Python offers great support for JSON through its json library. We can convert lists and dictionaries to JSON and JSON to lists and dictionaries. 

Our ISS Pass data, for example, is dictionary encoded as a string in JSON format.

{
  "message": "success", 
  "request": {
    "altitude": 100, 
    "datetime": 1626210005, 
    "latitude": 40.71, 
    "longitude": -74.0, 
    "passes": 5
  }, 
  "response": [
    {
      "duration": 220, 
      "risetime": 1626220537
    }, 
    {
      "duration": 632, 
      "risetime": 1626226088
    }, 
    {
      "duration": 638, 
      "risetime": 1626231894
    }, 
    {
      "duration": 569, 
      "risetime": 1626237784
    }, 
    {
      "duration": 585, 
      "risetime": 1626243649
    }
  ]
}

The Python JSON library has two main methods

  • dumps - takes in a Python object and converts to a JSON string
  • loads - takes in a JSON string and converts it to a Python object

script.py
Output
['Taco Bell', 'Shake Shack', 'Chipotle'] <class 'list'> ["Taco Bell", "Shake Shack", "Chipotle"] <class 'str'> ['Taco Bell', 'Shake Shack', 'Chipotle'] <class 'list'> {"Subway": 24722, "Pizza Hut": 7600, "Starbucks": 10821, "McDonalds": 14098} <class 'str'> {'Subway': 24722, 'Pizza Hut': 7600, 'McDonalds': 14098, 'Starbucks': 10821} <class 'dict'>

Getting JSON from a request as Python Objects using .json()

We can get the content of the response as a Python object using the .json() method on the response.

script.py
Output
<class 'dict'> {'request': {'passes': 5, 'latitude': 37.78, 'longitude': -122.41, 'altitude': 100, 'datetime': 1441417753}, 'message': 'success', 'response': [{'duration': 369, 'risetime': 1441456672}, {'duration': 626, 'risetime': 1441462284}, {'duration': 581, 'risetime': 1441468104}, {'duration': 482, 'risetime': 1441474000}, {'duration': 509, 'risetime': 1441479853}]} 369 <class 'int'>

    Response headers

    When the server generates a response, it also sends metadata in addition to the status code and the requested data. The metadata has information on how it generated the data and how to decode it. This information appears in the response headers. We can access response headers using the .headers property.

    The headers will appear as a dictionary as below.

    script.py
    Output
    {'content-length': '520', 'connection': 'keep-alive', 'date': 'Sat, 05 Sep 2015 01:49:13 GMT', 'content-type': 'application/json', 'via': '1.1 vegur', 'server': 'gunicorn/19.3.0'} application/json
    Variables
    content_typestr (<class 'str'>)
    'application/json'
    responseResponse (<class 'mock_requests.Response'>)
    <mock_requests.Response at 0x7f50f6a5b240>


      Find the number of people in space

      script.py
      Output
      {'people': [{'name': 'Gennady Padalka', 'craft': 'ISS'}, {'name': 'Mikhail Kornienko', 'craft': 'ISS'}, {'name': 'Scott Kelly', 'craft': 'ISS'}, {'name': 'Oleg Kononenko', 'craft': 'ISS'}, {'name': 'Kimiya Yui', 'craft': 'ISS'}, {'name': 'Kjell Lindgren', 'craft': 'ISS'}, {'name': 'Sergey Volkov', 'craft': 'ISS'}, {'name': 'Andreas Mogensen', 'craft': 'ISS'}, {'name': 'Aidyn Aimbetov', 'craft': 'ISS'}], 'number': 9, 'message': 'success'} 9



      Here are the takeaways from this article

      • What is an API?
      • Everyday examples of API
      • JSON format
      • So what is JSON and why JSON is used?
      • APIs requests (GET, PUT, POST, DELETE)
      • Get request in detail
      • Understanding status codes from API response
      • Python JSON library
      • Python JSON library methods
      • Getting JSON from a request using .json()
      • Response headers
      • Final get request example - Finding the no.of people in space

      Skills acquired:
      • How to access web data using APIs (Data Collection using APIs)
      • Querying and retrieving data using APIs
      • How to create and process API requests for Data Collection (GET Requests)
      • How to parse responses
      Tools Used:
      • Python's requests library
      • Python's json library main methods - json.dumps() and json.loads()
      • .json() method





























      Thursday, April 2, 2015

      Miss Independent

      It was one of those Saturday morning classes... Technical Communication being my favorite subject, I did not have to take an effort to be attentive. Besides that, the female faculty by nature had the knack of keeping her students glued to her while she's addressing. No boring lectures whatsoever! She often cited examples from Bollywood and anything related to Bollywood strikes my fancy.

      Conversations and conversationalists were the topics of discussion that day. She lectured on the qualities of a good conversation and how good and witty conversationalist ShahRukh Khan is.
      Now...that she had brought Bollywood into the discussion again, I couldn't help it, just got too excited not to participate in the discussion. Like a lot others in the class, I also decided to give my opinions on who I think is good as a conversationalist. It was an interactive session, 2 hours simply flew.

      Next was the boring computer networks session. The coffee and biscuits I had in the break after the first session didn't help much to be active. I dozed off for the first one hour and when I woke up I still had 55 mins to kill. I passed time by reading the forward messages in my friend's mobile and by playing the routine name-place-animal-thing game with my friend near me who was also uninterested in the lecture as me. Rachael was nowhere to be seen in the whole of the morning sessions.

      Lunch time. I was standing in the unusually long queue to get my share of food. Having nothing good to do in the queue, I started observing people's dressing and fashion senses and bitched about a few to one of my confidants ;) That's when Rachael showed up. She was walking past me with her group of friends.
      'I don't need anyone to take care of me. I can do that myself.' I overheard her conversation with one of the guys in her group. 
      No wonder! Of all these years of admiring her, I pretty well knew this nature of hers. But wait a minute! There is something different in her this time. An obvious difference. Easily noticeable.  
      Yeah, It's her hair!!! She had cut it too short this time. Short layers with bangs in the front. When I got a chance to ask her why too short, she didn't bother to answer much and waste time. 'Just like that', she smiled and continued to focus on the numbers in her scientific math calculator.
      The long hair lover that I am, always feel bad even when others cut their hair too short. But I also know the feeling of freshness every time I get a new haircut and styling.  'She must be feeling the same', I answered myself convincingly.

      End of the day in college. After 8 hours lectures and a severe head ache I got in to the shuttle on my way back to my hostel and all the way I was listening to Shreya Goshal's songs from my iPhone  music collections. Another source of inspiration. Her songs, especially Kannada, remind me of Rachael and they both go hand in hand. Both of them doing very well and successful in their respective careers could probably be the reason.

      I reached hostel and my roommates were already waiting for me, to go to the street side kachori shop together. They just gave me 10 mins to refresh and dragged me as soon as they could. We went. Everyone shared their own experiences of the day and the new boyfriend stories. We complained about office politics for a while and when the topic diverted to me and my day, the only thing I spoke about was Rachael having a new haircut. My roommates just laughed it off and teased me to consider their long time advice to start seeing guys instead of girls :P

      Near bed time. I opened my laptop to check Facebook, my last task of the day. I scrolled up and down. Nothing new and interesting came up on the screen. I just commented on few pictures that caught my eyes and wished happy birthday to a few not so close friends. When I was about to log off, it was her, Rachael updating her status to....."Miss. Independent" and sharing an YouTube video of the same name.
      I listened to the music and the lyrics several times that night and finished my day, feeling independent!

      It's been 8 years since that day. She introduced the song....it's lyrics and music stayed with me. As I write this blog thinking about Rachael and the profound influence she made in my life, I change my WhatsApp status to

      'She got her own thing
      And that's why I love her' 


      Cheers mate!!!

      Sunday, May 23, 2010

      Just like that....

      Well, its been quite a while since I have posted anything original in this blog space. Blame my job which has kept me away from one of my favorite activities!! At the same time, Many thanks that it has also kept me so close to my another very favorite activity SLEEPING!! :P

      This blog is not going to be a well constructed one.. but just ramblings! So read it at your own risk :P

      After a not-so-pleasant experience at flat, I have moved to a PG again.. back to an independent life!! All izzzzzz well, I just do what I want to do, go where I want to go, Sleep whenever I feel like sleeping... get up whenver I want to, read, shop, eat, work, enjoy and what not! Life is just cool.. No worries!!

      It was a Friday evening... After five days of routine office works, I felt very bored and wanted to do something different at the cost of nothing. I felt like doing something which I have never ever done before. When I asked my roomies for a good suggestion, they asked me to dance in the middle of the road... my roomies are wonderful you know :D

      Though the suggestion costed nothing, it was rejected just to help the people walking in the roads(note my service mind here ;) ). Planning to do something in BLR at the cost of nothing seemed to be a big deal.

      It was then I noticed one of my roomies make up kit and a bag full of nail polishes.. Boy, she had nail paint in all possible colors.. Black, Blue, Red, Pink, Green(!!), Orange, Yellow(!!)... Haaah...Boring times became colorful all of a sudden. I wanted to paint my nails right away with all those colors and make life colorful. I chose to wear copper sulphate blue color salwar to college the next day and so I painted my nails blue. Imagine me painting my nails with copper sulphate blue, wearing the same color salwar and going to college. No doubt I got some unusual comments among my friends circle but who cared??? ‘Its my nail, my roomie’s nail paint.. Why do you bother’ was my immediate response :D During the entire session, I was just reading my nails and was enjoying the beauty of blue. Crazy it may sound but Fun it was really!!!

      Black is one of my most favorite colors and I choose to have everything in black... be it a bag or a watch or a headband or a mobile or a lappy. I like black and now I have started to paint my nails black as well. Thanks to my roomie who has influenced me so much to paint even my nails black!!!

      Sometimes even the silly, simple, crazy things you do, make you feel sooooo good. I have always read life is what you make it and I am experiencing it now. If you make it colorful, it is colorful and if you make it sick, that is what it is. But the best part is, at the end of the day, the choice is yours to make and you have the control over your choices.

      Cheers!!!

      Saturday, March 6, 2010

      Do you think you are indispensable?



      Sometimes when you are feeling important,
      Sometimes when your ego is in bloom
      Sometimes when you take it for granted
      That you are best qualified in the room
      Sometimes when you feel you are parting
      Would leave an unfillable hole…
      Just follow these simple instructions
      And see what they do to your soul;
      Take a bucket and fill it with water
      Put your hand in it up to the wrist
      Pull it out… And the hole that remains
      Is a measure of how much you will be missed!
      You can splash all you wish when you enter
      You may stir up the water galore
      But stop… and you will find that in no time
      It looks just the same as before!!
      The moral in this quaint example
      Is to do just the best you can
      Be proud to yourself, but remember
      There is no indispensable man!!

      PS: The above words are not mine, it’s one of my reads and I wanted to share this with YOU in this space.


      Tuesday, March 2, 2010

      A lil' philosophy here...

      As a child of God, I am greater than anything that can happen to me.
      ~ Abdul Kalam

      Every blade of grass has its angel that bends over it and whispers “Grow, grow”.
      ~ The Talmud

      In three words I can sum up everything I've learned about life. It goes on.
      ~ Robert Frost

      Everything should be as simple as it is, but not simpler.
      ~ Albert Einstein

      I have not failed 700 times. I have not failed once. I have succeeded in proving that those 700 ways will not work. When I have eliminated the ways that will not work, I will find the way that will work.
      ~Thomas Edison

      What the mind of man can conceive and believe, it can achieve.
      ~ Napoleon Hill

      I shall be telling this with a sigh
      Somewhere ages and ages hence:
      two roads diverged in a wood, and I --
      I took the one less traveled by,
      And that has made all the difference.
      ~Robert Frost

      I love writing. I love the swirl and swing of words as they tangle with human emotions.
      ~ James A. Michener

      I have a very strong feeling that the opposite of love is not hate — it's apathy. It's not giving a damn.
      ~Leo Buscaglia

      I've learned that even when you think you have no more to give, when a friend cries out to you, you will find the strength to help.
      ~Anonymous

      I've learned that no matter how good a friend is, they're going to hurt you every once in a while and you must forgive them for that.
      ~ Anonymous

      A single rose can be my garden... a single friend, my world.
      ~Leo Buscaglia

      The friend is the man who knows all about you, and still likes you.
      ~Elbert Hubbard

      I've got dreams in hidden places and extra smiles for when I'm blue.
      ~Author Unknown

      Soul-mates are people who bring out the best in you. They are not perfect but are always perfect for you.
      ~Author Unknown

      You cannot be lonely if you like the person you're alone with.
      ~Wayne W. Dyer

      How lucky I am to have something that makes saying goodbye so hard.
      ~From the movie Annie