-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
105 lines (87 loc) · 2.95 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import urllib
import os
host_server = os.environ.get('host_server', 'localhost')
database_name = os.environ.get('database_name', 'fastapi')
db_username = os.environ.get('db_username', 'root')
db_password = (os.environ.get('db_password', ''))
DATABASE_URL = 'mysql://{}:{}@{}/{}'.format(db_username,db_password, host_server, database_name)
# Importing pymysql and accessing cursors
import pymysql.cursors
# Creating a connection with mysql
connection = pymysql.connect(
host = "localhost",
user = "root",
database = "fastapi",
cursorclass=pymysql.cursors.DictCursor)
with connection:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
# importing sqlalchemy in order to write easier database code / expressions
import sqlalchemy
metadata = sqlalchemy.MetaData()
# Creating a table using sqlalchemy
notes = sqlalchemy.Table(
"notes",
metadata,
# Creating a column...
sqlalchemy.Column("id", sqlalchemy.Integer),
sqlalchemy.Column("text", sqlalchemy.String),
sqlalchemy.Column("completed", sqlalchemy.Boolean),
)
#Setting up the sqlalchemy engine
engine = sqlalchemy.create_engine(DATABASE_URL,encoding='latin1', echo=True)
#Creating the table
metadata.create_all(engine)
#importing pydantic
from pydantic import BaseModel
class NoteIn(BaseModel):
text: str
completed: bool
#Response to an api call
class Note(BaseModel):
id: int
text: str
completed: bool
#Imporing fastapi related modules
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from typing import List
# Creating an instance of the fastapi
app = FastAPI(title="Rest Api using fastapi mysql async endpoints")
#adding cors
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*']
)
# for async features, Import databases
import databases
database = databases.Database(DATABASE_URL)
# Connecting the database on start of the app.
@app.on_event("startup")
async def startup():
await database.connect()
# disconnecting the database on shutdown of the app.
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
#adding notes.
@app.post("/notes/", response_model=Note)
async def create_note(note: NoteIn):
query = notes.insert().values(text= note.text, completed = note.completed)
# Get inserted record
last_record_id = await database.execute(query)
return {**note.dict(), "id": last_record_id}