-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhighscore_api.py
49 lines (37 loc) · 1.15 KB
/
highscore_api.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
import dataclasses
import random
from dataclasses import dataclass
from random import randint
from typing import List
import requests
_BASE_URL = "https://bourgeoisie-birds.herokuapp.com"
@dataclass
class HighScore:
initials: str
score: int
def wake_up_server():
"""
Wakes up the heroku server
"""
url = _BASE_URL + '/wake-up'
requests.get(url)
def get_high_scores(cache) -> List[HighScore]:
"""
Gets highest high scores from the heroku server
"""
url = _BASE_URL + '/high-score?cache_burst=' + str(random.randint(1, 1000000))
data = requests.get(url).json()
return list(map(lambda d: HighScore(d['initials'], d['score']), data))
def put_high_score(high_score: HighScore) -> bool:
"""
Creates and entry for the new high score and returns if successful
"""
url = _BASE_URL + '/high-score'
data = requests.put(url, data=dataclasses.asdict(high_score)).json()
if "message" in data and data["message"] == "inserted":
return True
return False
if __name__ == "__main__":
print("testing")
print(put_high_score(HighScore("ADS", randint(1, 100))))
print(get_high_scores(209))