Python API programming
API(Application Programming Interface)
web API (over the network or internet using HTTP request verbs)
: GET, POST, PUT
: HEAD, CONNECT, OPTIONS, TRACE, PATCH
https://api.coindesk.com/v1/bpi/currentprice.json
vscode ~ new file ~ select language : json ~ Shift+Option(alt)+ F ==> json 예쁘게 보기
{
"time": {
"updated": "Dec 19, 2021 04:19:00 UTC",
"updatedISO": "2021-12-19T04:19:00+00:00",
"updateduk": "Dec 19, 2021 at 04:19 GMT"
},
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"chartName": "Bitcoin",
"bpi": {
"USD": {
"code": "USD",
"symbol": "$",
"rate": "47,860.7833",
"description": "United States Dollar",
"rate_float": 47860.7833
},
"GBP": {
"code": "GBP",
"symbol": "£",
"rate": "36,130.8711",
"description": "British Pound Sterling",
"rate_float": 36130.8711
},
"EUR": {
"code": "EUR",
"symbol": "€",
"rate": "42,580.7817",
"description": "Euro",
"rate_float": 42580.7817
}
}
}
$ curl --include -X GET "https://api.coindesk.com/v1/bpi/currentprice.json"
HTTP/2 200
content-type: application/javascript
content-length: 678
access-control-allow-origin: *
cache-control: max-age=15
date: Sun, 19 Dec 2021 06:05:59 GMT
expires: Sun, 19 Dec 2021 06:06:07 UTC
server: nginx/1.18.0
x-powered-by: Fat-Free Framework
x-cache: Miss from cloudfront
via: 1.1 df8ca7180650f326a42a059eda17d880.cloudfront.net (CloudFront)
x-amz-cf-pop: ICN54-C1
x-amz-cf-id: NOTs9OY2mKiCD27sdVqq5VgJQPF0uKimfC0J69Aq-B_ViKZHLkwqYA==
{"time":{"updated":"Dec 19, 2021 06:05:00 UTC","updatedISO":"2021-12-19T06:05:00+00:00","updateduk":"Dec 19, 2021 at 06:05 GMT"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","chartName":"Bitcoin","bpi":{"USD":{"code":"USD","symbol":"$","rate":"47,677.7875","description":"United States Dollar","rate_float":47677.7875},"GBP":{"code":"GBP","symbol":"£","rate":"35,992.7246","description":"British Pound Sterling","rate_float":35992.7246},"EUR":{"code":"EUR","symbol":"€","rate":"42,417.9740","description":"Euro","rate_float":42417.974}}}%
import os
import json
result = os.popen('curl --include -X GET "https://api.coindesk.com/v1/bpi/currentprice.json"')
output_json = result.read()
print(output_json)
json
https://docs.python.org/3.10/library/json.html
형태 변화 없이 형변환만 str으로
import json # dictionary --> str json json_val = json.dumps(dict_val) # json --> dictionary dict_val = json.loads(json_val)
API사용하는 이유
- 여러 Application 을 통합
- cross-platform Application 만들기
- Application의 기능 강화
- Application에 외부접근 제공
- app backend로써
API Design Patterns
복잡한 HTTP methods를 간략하기 위해 , HTTP architecture patterns을 사용
- RESTful APIs
- GraphQL APIs
- CRUD APIs
- SOAP APIs
RESTful APIs 의 methods
- GET
- POST
- PUT
- DELETE
Building API
import os import json import requests URL = "https://api.coindesk.com/v1/bpi/currentprice.json" response = requests.get(URL, verify=False) print(response) #json.loads(response.content) data = json.loads(response.content.decode("utf-8")) bitcoin_price= data["bpi"]["USD"]['rate_float'] bitcoin_price
