Article that can be a human resource who understands and masters the mechanism of API (with Python code)

Introduction

I'm studying programming and what is API? Have you ever felt that?

** "What can you do with the API in the first place?" ** "How do you use this?"

To solve these problems, it is good to understand ** the mechanism common to all APIs **. I'll show you the specific Python code, so by the time you finish reading, you should be familiar with how to use the API.

The chapter structure is as follows.

--What is API (merits of using API) --Understanding the API --Example of API service --Python code sample

※※ Since various information is also posted on SNS, if you feel that it is okay to read the article I would be grateful if you could follow Twitter account "Saku731". ※※

What is API (merits of using API)

API is an abbreviation for "** A ** pplication ** P ** rogramming ** I ** nterface". The literal translation is as follows.

--Application --Using Programming --Connect (Interface)

Going a little deeper, the biggest advantage that APIs are needed is "** Borrow useful functions created by other people to the Web service you created **" I think this is all.

In other words, you don't have to work hard to develop from scratch. For example, when developing an EC site, which one would you choose?

--Create your own ** credit card payment function ** --Use the ** credit card payment API ** provided by the credit card company

In this way, the option ** "Do not make it yourself" ** is the greatest merit of the API.

Understand the API

Now that you know the benefits of the API, let's understand how it works. There are three main types of knowledge required.

―― 1) How the Web works ―― 2) HTTP request ―― 3) HTTP response

1) How the Web works

The Internet, which I casually use, is made possible thanks to the Web. The Web is a "** mechanism for sending all kinds of data such as texts and images on the Internet **".

So, of course, you need to understand the Web to borrow a Web service created by another person using the API. To understand the Web, let's hold down the following three.

It is like this when organized in sentences.

What is the Web? "According to the communication promise of HTTP" "To and from the location (server) specified by the URL" A mechanism for "exchanging hypertext (information)".

I have to read many books to understand the Web in detail, It is enough to understand that it is "** technology for exchanging information with the server specified by URL **".

The API is a mechanism that makes it easy to exchange information between ** your own web service ** and ** web service (server) created by another person ** using this web technology.

And, in order to use the API concretely, it is necessary to use HTTP (communication convention) adopted by the Web. There are two types, "Send information to the other party ** HTTP request **" and "Receive information from the other party ** HTTP response **", so let's understand them later.

2) HTTP request

The first is an HTTP request that sends data from you to the other party. It is a process to send a request such as "Please let me use the function" ** to a Web service created by another person via API.

The API usage seems to be different for each Web service, but there is a common foundation. The following information should be prepared in common when using any API.

--Information for connecting to a Web service (obtained when registering the service) --API endpoint (URL to connect to the service) --API key (password required for connection) --Information to be sent to the Web service (can be confirmed in the API specifications) --Method (mainly POST or GET) --Query parameters (specify details such as API / service type to use) --Header (Enter authentication information such as data type and API key * Specify by POST method) --Body (Enter the information you want to send and receive with the API. * Specify with the POST method)

These ** major items are common to all APIs , but there are differences in the details. Check the required contents in the " API specifications **" published by each service.

If you use an API that can get the weather in each area, the information will be organized in this way. You don't have to be aware of it at first because it is written in the API specification whether to use POST or GET.

In addition to POST and GET, there are methods called PUT and DELETE. It is roughly explained as follows.

--GET: Get data --POST: Create new data --PUT: Update the data --DELETE: Delete data

Weather forecast API (for POST method)

--Information for connecting to a web service --API endpoint: https: /world_weather.com/api * This is a fictitious URL. --API key: aaabbbccc --Information to send to the web service --Method: POST --Query parameter: service = Waityer (specify the service to use) --Header: application / json (data type) --Body: date = today, area = Tokyo (Enter the information you want to send and receive)

The sample program is as follows.

#Library import
import requests

#Information for connecting to the API
API_Endpoint = https:/world_weather.com/api
API_Key = aaabbbccc

#Information to send to the API
headers = {'Content-Type': 'application/json', 'key':API_Key}
body = {date='today', area=Tokyo}

#Executing API connection
result = requests.post(API_Endpoint, data=json.dumps(body), headers=headers)

Weather forecast API (for GET method)

--Information for connecting to a web service --API endpoint: https: /world_weather.com/api --API key: aaabbbccc --Information to send to the web service --Method: GET --Query parameters: service = Wednesday, date = today, area = Tokyo (In the GET method, all are stored in the query parameters)

The GET method is generally written as follows, although it depends on the service.

** ① Principle: Include all information in the endpoint (URL) **

#Library import
import requests

#Information for connecting to the API ("?"Behind"&”To connect the conditions)
API_Endpoint = https:/world_weather.com/api?Key=aaabbbccc&date='today'&area='Tokyo'

#Executing API connection
result = requests.get(API_Endpoint)

** ② requests module function: Separate information like POST **

This description method is similar to POST. On the back side, the data is sent in the same form as "(1) Principle", so the result is the same.

#Library import
import requests

#Information for connecting to the API
API_Endpoint = https:/world_weather.com/api
API_Key = aaabbbccc

#Information to send to the API
headers = {'key':API_Key}
params = {date='today', area=Tokyo}

#Executing API connection
result = requests.get(API_Endpoint, headers=headers, params=params)

3) HTTP response

When you send an HTTP request to a web service via the API, you will receive a "reply" called ** HTTP Response **. By displaying this "Reply" on your own Web service, you can save yourself the trouble of developing from scratch.

For example, "** Request **: Please tell me the weather. Today, Japan, Tokyo ”

image.png

"** Response **: Sunny then cloudy"

image.png

API service example

Now that you have an overview of the API, let's use a free API service that you can actually use.

Service overview of API to be used

Try using the Livedoor weather information service ** Weather Hacks **. You can get the weather forecast of 142 places all over Japan for free until "Today, tomorrow, the day after tomorrow".

http://weather.livedoor.com/weather_hacks/webservice

image.png

Take a look at the API specifications

Click the link above to see the API specification. The number of items and names vary depending on the service, but the order to check is as follows.

  1. What is the API endpoint?
  2. How to get API Key?
  3. POST or GET?
  4. What are the query parameters?
  5. What information should I send in the header body?

image.png

Let's organize them in order. This time it's a free service, so it's very simple.

  1. ** What is the API endpoint? ** ** http://weather.livedoor.com/forecast/webservice/json/v1
  1. ** How to get API Key? ** ** Since there is no description in particular, it seems to be a service that can be used without authentication (no API Key required).

  2. ** POST or GET? ** ** This service uses the ** GET ** method. In the case where the method is not specified even at first glance of the specification like this time, it is determined as follows. --If there is a description to be used in the form of "endpoint (URL) + query parameter": GET in many cases --Other than that: Mostly POST

  3. ** What are the query parameters? ** ** It seems that you should specify the area (city) for which you want to get the weather forecast. Let's use "Kurume: 400040" written in the sample.

  4. ** What information should I send in the header / body? ** ** Since the GET method is used, there is no need to prepare a header body.

Python code sample

Now that you have the information you need for your request, let's make a request to the API in Python.

Send API request

It's okay if you copy and paste the code and execute it as it is.

#Library required for API request
import requests
#URL + query parameters
url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=400040'
#Send API request
tenki_data = requests.get(url).json()

Displaying the returned result with print () makes the display very difficult to read. You need to understand how to use ** JSON data ** as it cannot be used as it is.

print(tenki_data)

image.png

Check JSON data of API response

The data returned from the API is often in ** JSON format **. I will omit the detailed explanation, but it is common to convert from JSON format to dictionary type in the .json () part included in requests.get (url) .json () of the code before using it. It is a target.

How to use the dictionary type is described in the API specification.

http://weather.livedoor.com/weather_hacks/webservice

image.png

First, it says that you can get the "title / heading" by specifying title in the dictionary type.

print('title:', tenki_data['title'])

image.png

Next, if you specify forecasts, you can get the" weather forecast for each forecast date ". However, it requires a little complicated procedure peculiar to JSON format, so let's keep the flow together.

print(tenki_data['forecasts'])

image.png

First, when forecasts is specified, the list type whose first and last are enclosed in[]is displayed. You can expect to specify here as "Today:[0], Tomorrow:[1], The day after tomorrow[2]" according to the data you want to acquire.

#Specify "Today" data
print(tenki_data['forecasts'][0])

image.png

It was pretty refreshing. If you come to this point, you can get the data you like according to the purpose, so let's display "Forecast date: dataLabel, Weather: telop" this time.

#Forecast date
print('Forecast date:', tenki_data['forecasts'][0]['dateLabel'])
#weather
print('weather:', tenki_data['forecasts'][0]['telop'])

image.png

In this way, a series of steps of "API specification check-request-response" will flow. You can get various other information, so please try various things by referring to the API specifications.

at the end

The point that APIs are used differently depending on the service is very troublesome. However, if you think based on the following introduced this time, you can master most of the API.

** Information required to use the API **

--Information for connecting to a Web service (obtained when registering the service) --API endpoint (URL to connect to the service) --API key (password required for connection) --Information to be sent to the Web service (can be confirmed in the API specifications)) --Method (mainly POST or GET) --Query parameters (specify details such as API / service type to use) --Header (Enter authentication information such as data type and API key * Specify by POST method) --Body (Enter the information you want to send and receive with the API. * Specify with the POST method)

** Confirmation procedure **

  1. What is the API endpoint?
  2. How to get API Key?
  3. POST or GET?
  4. What are the query parameters?
  5. What information should I send in the header body?

This is the end of "API mechanism and how to use it". I hope it will be useful information for your development.

~~ Also, at the end of the sentence, we are doing "** Team Development Experience Project **" for a limited time. ~~ ~~ If you are interested, please check [Application Sheet] for details. ~~ (Addition) The deadline has been closed because it is full. The next time is scheduled for March 2019, so if you would like to be informed, please fill in [Reservation Form].

Postscript (Supplementary materials will be added at any time)

--How to operate the Response object returned by the response - https://note.nkmk.me/python-requests-usage/ - http://python.zombie-hunting-club.com/entry/2017/11/06/211118#7-%E7%94%BB%E5%83%8F%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92%E5%8F%96%E5%BE%97%E4%BF%9D%E5%AD%98%E3%81%99%E3%82%8B

--If the response is an audio file, use the following code to save it in WAVE format.

response = requests.post(API_Endpoint, data=json.dumps(body), headers=headers)
with open('response.wav', 'wb') as saveFile:
    saveFile.write(response.content)

Recommended Posts

Article that can be a human resource who understands and masters the mechanism of API (with Python code)
[Python] A program to find the number of apples and oranges that can be harvested
Understand the probabilities and statistics that can be used for progress management with a python program
[Python] A program that finds the maximum number of toys that can be purchased with your money
[Python] Code that can be written with brain death at the beginning when scraping as a beginner
Play with the password mechanism of GitHub Webhook and Python
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
[Python] Make a graph that can be moved around with Plotly
I made a shuffle that can be reset (reverted) with Python
The story of making a module that skips mail with python
[Python] Draw elevation data on a sphere with Plotly and draw a globe that can be rotated round and round
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
[Python] A program that calculates the number of socks to be paired
I tried to get the authentication code of Qiita API with Python.
Draw a graph that can be moved around with HoloViews and Bokeh
Hit a method of a class instance with the Python Bottle Web API
"Manim" that can draw animation of mathematical formulas and graphs with Python
Get the number of articles accessed and likes with Qiita API + Python
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
[CleanArchitecture with Python] Apply CleanArchitecture step by step to a simple API and try to understand "what kind of change is strong" in the code base.
[Python] Wouldn't it be the best and highest if you could grasp the characteristics of a company with nlplot?
[Python] I made a web scraping code that automatically acquires the news title and URL of Nikkei Inc.
[Python] Creating a tool that can list, select, and execute python files with tkinter & about the part that got caught
[Python3] Code that can be used when you want to change the extension of an image at once
[Python] The movement of the decorator that can be understood this time ② The decorator that receives the argument
A class for PYTHON that can be operated without being aware of LDAP
Around the authentication of PyDrive2, a package that operates Google Drive with Python
I made a familiar function that can be used in statistics with Python
[Python] A program that calculates the number of updates of the highest and lowest records
Get the stock price of a Japanese company with Python and make a graph
Convert the character code of the file with Python3
A discussion of the strengths and weaknesses of Python
[Python] I examined the practice of asynchronous processing that can be executed in parallel with the main thread (multiprocessing, asyncio).
Run the output code with tkinter, saying "A, pretending to be B" in python
Automate background removal for the latest portraits in a directory with Python and API
Build API server for checking the operation of front implementation with python3 and Flask
I tried to automate the article update of Livedoor blog with Python and selenium.
Geographic information visualization of R and Python that can be expressed in Power BI
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
Morphological analysis and tfidf (with test code) that can be done in about 1 minute
In Python3.8 and later, the inverse mod can be calculated with the built-in function pow.
A mechanism to call a Ruby method from Python that can be done in 200 lines
Visualize the range of interpolation and extrapolation with python
# Function that returns the character code of a string
PHP and Python samples that hit the ChatWork API
A memo that I touched the Datastore with python
[Python] Get user information and article information with Qiita API
[Python] A program that compares the positions of kangaroos.
Collect tweets about "Corona" with python and automatically detect words that became a hot topic due to the influence of "Corona"
A server that returns the number of people in front of the camera with bottle.py and OpenCV
A library that monitors the life and death of other machines by pinging from Python
How to start a simple WEB server that can execute cgi of php and python
Get the trading price of virtual currency and create a chart with API of Zaif exchange
Format summary of formats that can be serialized with gensim
Try hitting the Twitter API quickly and easily with Python
A note about hitting the Facebook API with the Python SDK
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Requirements definition-articles that can be human resources who can design systems
Detect objects of a specific color and size with Python
Create code that outputs "A and pretending B" in python