Closed kawabata2018 closed 3 years ago
SQLAlchemy - Pythonで使うORマッパー https://qiita.com/ariku/items/75799665acd09520bed2
Booksテーブル | カラム名 | データ型 | 備考 |
---|---|---|---|
isbn | char(13) | PK | |
title | varchar(255) | ||
author | varchar(255) | ||
publisher | varchar(255) | ||
cover | varchar(255) | 表紙画像へのURL |
(例)API Gatewayから渡されたリクエストをトリガーにして、書籍情報をJSON形式で返すLambda関数
import requests
import json
class Book:
def __init__(self, isbn: str, title: str, author: str, publisher: str, cover: str) -> None:
self.isbn = isbn
self.title = title
self.author = author
self.publisher = publisher
self.cover = cover
def handler(event: dict, context: dict) -> dict:
try:
isbn = event.get("queryStringParameters").get("isbn")
openbd_api_url = f"https://api.openbd.jp/v1/get?isbn={isbn}"
headers = {"content-type": "application/json"}
res = requests.get(openbd_api_url, headers=headers)
data = res.json()
print(data)
response = []
if data:
if data[0] is not None:
summary = data[0]["summary"]
summary_isbn = summary["isbn"]
summary_title = summary["title"]
summary_author = summary["author"]
summary_publisher = summary["publisher"]
summary_cover = summary["cover"]
book = Book(summary_isbn, summary_title, summary_author, summary_publisher, summary_cover)
response.append(book.__dict__)
return {
"statusCode": 200,
'headers': {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
"body": json.dumps(response, ensure_ascii=False)
}
except Exception as e:
print(e)
return {
"statusCode": 500,
'headers': {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
"body": json.dumps("ERROR")
}
大規模になるとServerless Framework
を使うのがいい模様
背景
To do
完了条件