본문 바로가기
Developing../Python

API tutorials in Python - REST API 중심으로

by bents 2021. 2. 5.

다룰 내용

  • What an API is and what can you use it for (web API 중심)
  • What status codes, HTTP headers, and HTTP methods are
  • How can you use Python to consume public data using APIs
  • How to use authentication when consuming APIs with Python

주요 Web API

  1. SOAP (Simple Object Access Protocol) is typically associated with the enterprise world, has a stricter contract-based usage, and is mostly designed around actions.
  2. REST (Representational State Transfer) is typically used for public APIs and is ideal for fetching data from the web. It’s much lighter and closer to the HTTP specification than SOAP.
  3. GraphQL :  flexible query language for APIs , where the clients decide.

Even though GraphQL is on the rise and is being adopted by bigger and bigger companies, including GitHub and Shopify, the truth is that the majority of public APIs are still REST APIs

# 주요 용어

1) BaseURL은 내가 접근할 최상단 도메인 주소라고 생각하면 됨.

2) An endpoint is a part of the URL that specifies what resource you want to fetch.

*잘만들어진 APIs 는 API reference,있음

3) HTTPS 은 the encrypted version of HTTP으로, making all traffic between the client and the server much safer.

public APIs으로 private or sensitive 정보(키값)보낼 때, 절대 http:// endpoints로 보내지 말것.

https:// base URL 로 정보를 보내야 한다.

 

- Random User Generator API

import requests
response = requests.get("https://randomuser.me/api/")
response.text

- 특정API 제공하는 사이트

import requests
response = requests.get("https://api.thedogapi.com/")
response.text # '{"message":"The Dog API"}'

# 엔드포인트 변경!
response = requests.get("https://api.thedogapi.com/v1/breeds")
response.text # '[{"weight":{"im..

#  주요 구성요소 of an API 

클라이언트와 서버의 소통은 request와 response으로 이뤄짐.

  • Requests - API의 request call 정보 : the base URL, the endpoint, the method used, the headers, and so on.
  • Responses - server의 반환정보 : the data or content, the status code, and the headers.
>>> request = response.request
>>> request.url
'https://api.thedogapi.com/v1/breeds'
>>> request.path_url
'/v1/breeds'
>>> request.method
'GET'
>>> request.headers
{'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*', 'Connection': 'keep-alive'}
>>> response
<Response [200]>
>>> response.text
'[{"weight":{"imperial":"6 - 13","metric":"3 - 6"},
"height":{"imperial":"9 - 11.5","metric":"23 - 29"},"id":1,
"name":"Affenpinscher", ...}]'
>>> response.status_code
200
>>> response.headers
{'Cache-Control': 'post-check=0, pre-check=0', 'Content-Encoding': 'gzip',
'Content-Type': 'application/json; charset=utf-8',
'Date': 'Sat, 25 Jul 2020 17:23:53 GMT'...}

1. Status-codes : response 상태정보

>>> response.status_code
200
>>> response.reason
'OK'

# 200은 성공, 404는 not found
# 400대는 모두 에러다.

2. HTTP Headers (custom Headers) 의 종류

  • Accept
  • Content type -  default : JSON , other :  XML or media types ( images or video )
  • User-Agent
  • Server
headers = {"X-Request-Id": "<my-request-id>"}
response = requests.get("https://example.org", headers=headers)

response.headers
# {'Content-Encoding': 'gzip',
# 'Content-Type': 'application/json; charset=utf-8',
# 'Date': 'Sat, 25 Jul 2020 19:52:07 GMT'...}

response.request.headers
# {'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding': 'gzip, deflate',
# 'Accept': '*/*', 'Connection': 'keep-alive',
# 'X-Request-Id': '<my-request-id>'}

response.headers.get

3. Response Contents

To properly read the response contents according to the different Content-Type headers, the requests package comes with a couple of different Response attributes you can use to manipulate the response data:

  • .text returns the response contents in Unicode format. (textual data)
  • .content returns the response contents in bytes. (images and other nontextual data)

Content-type을 확인하고 데이터포맷에 맞게 사용하자!

response = requests.get("https://api.thedogapi.com/v1/breeds/1")
response.headers.get("Content-Type")
# 'application/json; charset=utf-8'
response.json()
response.json()["name"]
response = requests.get("http://placegoat.com/200/200")
response.headers.get("Content-Type")
# 'image/jpeg'
file = open("goat.jpeg", "wb")
file.write(response.content)
file.close()

4. HTTP Methods

API로 실행할 수 있는 액션이 여러가지(CRUD operations+) 있다. ( methods, also called verbs)

대부분 PublicAPI는 Get만 제공함. 

# In this case, not all endpoints will allow for POST, PUT, or DELETE methods
requests.post("https://api.thedogapi.com/v1/breeds/1")
requests.get("https://api.thedogapi.com/v1/breeds/1")
requests.put("https://api.thedogapi.com/v1/breeds/1")
requests.delete("https://api.thedogapi.com/v1/breeds/1")

5. Query Prameters

원하는 정보만 SQL쿼리처럼 가져오기

query_params = {"q": "labradoodle"}
endpoint = "https://api.thedogapi.com/v1/breeds/search"
requests.get(endpoint, params=query_params).json()

## Custom API

import requests
from datetime import date, timedelta

today = date.today()
yesterday = today - timedelta(days=1)
country = "germany"
endpoint = f"https://api.covid19api.com/country/{country}/status/confirmed"
params = {"from": str(yesterday), "to": str(today)}
response = requests.get(endpoint, params=params).json()
total_confirmed = 0
response 

for day in response:
    cases = day.get("Cases", 0)
    total_confirmed += cases

print(f"Total Confirmed Covid-19 cases in {country}: {total_confirmed}")

# 중요한 기능들

1. Authentication steps before you can use

  • API keys
  • OAuth 
  • 인증에러 : 401, 403
endpoint = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos"
# Replace DEMO_KEY below with your own key if you generated one.
api_key = "DEMO_KEY"
query_params = {"api_key": api_key, "earth_date": "2020-07-01"}
response = requests.get(endpoint, params=query_params)
response

2. Pagination

 pagination 은 한번에 데이터를 가져오지 않고 여러 페이지(그룹)으로 나눠어서 가져오는 액션을 말함.

  • A page attribute that defines which page you’re currently requesting (page)
  • A size attribute that defines the size of each page (per_page )
response = requests.get("https://api.github.com/events?per_page=1&page=0")
response.json()[0]["id"]

3. Rate limiting

 restricts the number of requests that users can make in a given time frame.

짧은 시간내 과도하게 접근하면 디도스 공격같은 어뷰징 접근이라고 생각해서 막아야 함.

endpoint = "https://api.github.com/events"
for i in range(100):
  response = requests.get(endpoint)
  print(f"{i} - {response.status_code}")
  if response.status_code != 200:
    break

response.json()

## 실습과제 (OAuth)

  • industryIdentifiers
  • averageRating and ratingsCount
  • imageLinks

 

# Source

 

1. api 전체 목록 : realpython.com/tutorials/api/

 

Python API Tutorials – Real Python

Tutorials related to Python API design or interacting with APIs using Python. REST APIs in web applications would be one example where Python shines.

realpython.com

 

2. api preview : realpython.com/preview/python-api/

 

Python & APIs: A Winning Combo for Reading Public Data – Real Python

Knowing how to consume an API is one of those magical skills that, once mastered, will crack open a whole new world of possibilities, and consuming APIs using Python is a great way to learn such a skill. A lot of apps and systems you use on a daily basis a

realpython.com

 

'Developing.. > Python' 카테고리의 다른 글

Concurrency in python- Async IO  (0) 2021.02.05
ML module 을 REST api 배포하기  (0) 2021.02.05
Socket Programming in Python (Guide)  (0) 2021.02.05
PEP 8 -- Style Guide & Structure  (0) 2021.02.05
markdown to html file  (0) 2021.01.31