-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
164 lines (135 loc) · 5.41 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from fastapi.middleware.cors import CORSMiddleware
from fastapi import Header
from env import env
import requests
import database
import fastapi
import uvicorn
import classes
import json
DEVELOPMENT = False
API_ENDPOINT = 'https://discord.com/api/v10'
CLIENT_ID = env.CLIENT_ID
CLIENT_SECRET = env.CLIENT_SECRET
if DEVELOPMENT:
REDIRECT_URI = 'http://localhost:8000/auth/discord/login' # Redirect to the in app login URL
print(f"Testing URL: https://discord.com/oauth2/authorize?client_id={CLIENT_ID}&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fauth%2Fdiscord%2Flogin&scope=identify")
else:
REDIRECT_URI = 'http://localhost:37520/auth/discord/login' # Redirect to the in app login URL
app = fastapi.FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# MARK: Auth
def verify_token(token):
headers = {
'Authorization': f'Bearer {token}'
}
r = requests.get('%s/users/@me' % API_ENDPOINT, headers=headers)
return r.status_code == 200
def get_user_id(token):
headers = {
'Authorization': f'Bearer {token}'
}
r = requests.get('%s/users/@me' % API_ENDPOINT, headers=headers)
r.raise_for_status()
return r.json()
@app.get('/auth/discord')
def discord_url():
return f'https://discord.com/oauth2/authorize?client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}&scope=identify'
@app.get('/auth/discord/login')
def exchange_code(code):
data = {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI,
'scope': 'identify'
}
headers = {
'content-type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers, auth=(CLIENT_ID, CLIENT_SECRET))
r.raise_for_status()
user = get_user_id(r.json()['access_token'])
# Check if the user exists
database_response = database.get_new_token(user['id'])
if database_response.status != 200:
database_response = database.create_user(user['id'], user['username'])
if database_response.status != 200:
return {'error': 'Failed to create user.'}
return database_response.data
# MARK: Tracking
@app.get("/tracking/ping/{user_id}")
def ping(user_id: str):
return database.ping(user_id)
@app.get("/tracking/time/{user_id}")
def get_user_time(user_id: str):
return database.get_time_used(user_id)
@app.get("/tracking/users")
def get_online_users():
return database.get_online_user_count()
# MARK: User
@app.get('/user/{user_id}')
def get_user(user_id: str, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.get_user(user_id, authorization)
@app.get('/delete/{user_id}')
def delete_user(user_id: str, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.delete_user(user_id, authorization)
# MARK: Jobs
@app.post('/user/{user_id}/job/started')
def job_started(user_id: str, job: classes.Job, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.job_started(user_id, authorization, job)
@app.post('/user/{user_id}/job/finished')
def job_finished(user_id: str, job: classes.FinishedJob, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.job_finished(user_id, authorization, job)
@app.post('/user/{user_id}/job/cancelled')
def job_cancelled(user_id: str, job: classes.CancelledJob, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.job_cancelled(user_id, authorization, job)
@app.get('/user/{user_id}/jobs')
def get_jobs(user_id: str, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.get_jobs(user_id, authorization)
# MARK: Commits
@app.get('/commits/{commit_id}')
def get_commit_info(commit_id: str):
return database.get_commit_info(commit_id)
@app.post('/commits/{commit_id}/updated')
def mark_commit_updated(user_id: str, commits: classes.UpdatedCommits, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.mark_commit_updated(user_id, authorization, commits)
@app.post('/commits/{commit_id}/emote/add')
def add_emote_to_commit(user_id: str, commit_id: str, emote: str, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.add_emote_to_commit(user_id, authorization, commit_id, emote)
@app.post('/commits/{commit_id}/emote/remove')
def remove_emote_from_commit(user_id: str, commit_id: str, emote: str, authorization: str = Header(None)):
if not authorization:
return {'error': 'No authorization header.'}
return database.remove_emote_from_commit(user_id, authorization, commit_id, emote)
# MARK: Heartbeat
@app.get('/heartbeat')
def heartbeat():
return {'status': 'ok'}
if __name__ == '__main__':
if DEVELOPMENT:
print("WARNING: Running on localhost")
uvicorn.run(app, host='localhost', port=8000)
else:
uvicorn.run(app, host='0.0.0.0', port=8000)