This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
81 lines (65 loc) · 2.09 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
import psycopg2
from typing import Union
from fastapi import FastAPI
import datetime
import os
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
# database connection
try:
conn = psycopg2.connect(database=os.environ.get('DB_NAME'),
host=os.environ.get('DB_HOST'),
user=os.environ.get('DB_USER'),
port=os.environ.get('DB_PORT'))
except:
print("unable to connect to db")
cursor = conn.cursor()
ALLOWED_CARRIERS = ["globe", "smart", "sun",
"dito", "tm", "tnt" "cherry", "gomo"]
@app.get("/")
# hello world!
def read_root():
return "Hello World!"
@app.get("/api/")
# test endpoint for homework #1
def read_item(prefix: Union[str, None] = None):
if (prefix == "0915"):
return "Globe"
elif (prefix == "0920"):
return "Smart"
elif (prefix == "0928"):
return "Smart"
return "Unknown"
@app.post("/api/loadPrefixData")
# upsert prefix and carrier data for one pair
# input: object(prefix, carrier)
# TODO: update data type
def read_item(prefix: Union[str, None] = None, carrier: Union[str, None] = None):
try:
current_date = datetime.datetime.now()
cursor.execute(
"INSERT INTO prefixes(prefix,carrier,created_at,updated_at) VALUES (%s,%s,%s,%s)", (prefix, carrier, current_date, current_date))
conn.commit()
except Exception as e:
print(e)
print("error loading prefix data")
return "OK!"
@app.post("/api/loadPrefixesData")
# upsert pairs of prefix and carrier data
# input: object(prefix, carrier)[]
# TODO: update data type
def read_item(carrier: Union[str, None] = None):
return "Hello World!"
@app.get("/api/getCarrierPrefixes")
# get the prefixes from given carrier
# input: carrier: string
# output: prefixes: string[]
def read_item(carrier: Union[str, None] = None):
return "Hello World!"
@app.get("/api/getCarrierFromPrefix")
# get the carrier from given prefix
# input: prefix: string
# output: carrier: string
def read_item(prefix: Union[str, None] = None):
return "Hello World!"