Skip to content

Commit 004ee9f

Browse files
committed
Final code from demos.
1 parent 4a10532 commit 004ee9f

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import uplink
2+
3+
from uplink_helpers import raise_for_status, response_to_data
4+
5+
6+
@response_to_data
7+
@raise_for_status
8+
@uplink.json
9+
class GameService(uplink.Consumer):
10+
def __init__(self):
11+
super().__init__(base_url='http://localhost:5000')
12+
13+
@uplink.get('/api/game/users/{username}')
14+
def find_user(self, username):
15+
pass
16+
17+
@uplink.put('/api/game/users')
18+
def create_user(self, **kwargs: uplink.Body):
19+
pass
20+
21+
@uplink.post('/api/game/games')
22+
def create_game(self, **kwargs: uplink.Body):
23+
pass
24+
25+
@uplink.get('/api/game/rolls')
26+
def all_rolls(self):
27+
pass
28+
29+
@uplink.get('/api/game/{game_id}/status')
30+
def game_status(self, game_id):
31+
pass
32+
33+
@uplink.get('/api/game/top_scores')
34+
def top_scores(self):
35+
pass
36+
37+
@uplink.post('/api/game/play_round')
38+
def play_round(self, **kwargs: uplink.Body):
39+
pass
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import random
2+
3+
from api import GameService
4+
5+
6+
def main():
7+
svc = GameService()
8+
9+
print("Game app! (client)")
10+
print()
11+
print()
12+
print("TOP SCORES")
13+
for s in svc.top_scores():
14+
print("{} scored {}".format(s.get('player').get('name'), s.get('score')))
15+
print()
16+
17+
game_id = svc.create_game().get('game_id')
18+
rolls = svc.all_rolls()
19+
player = svc.find_user("Charles")
20+
21+
is_over = False
22+
while not is_over:
23+
name = player.get('name')
24+
roll = random.choice(rolls)
25+
rnd = svc.play_round(game_id=game_id, user=name, roll=roll)
26+
is_over = rnd.get('is_final_round')
27+
print("Round {}".format(rnd.get('round_number')))
28+
print("{} rolls {}".format(name, roll))
29+
print("{} rolls {}".format(rnd.get('opponent').get('name'), rnd.get('computer_roll').get('name')))
30+
print("Resulting in {}".format(rnd.get('round_outcome')))
31+
print("")
32+
33+
game_status = svc.game_status(game_id)
34+
print("Game is over, outcome: Winner: {}".format(game_status.get('winner').get('name')))
35+
36+
37+
if __name__ == '__main__':
38+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uplink
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import uplink
2+
from requests import Response
3+
4+
5+
class FormatError(Exception):
6+
pass
7+
8+
9+
@uplink.response_handler
10+
def raise_for_status(response: Response):
11+
response.raise_for_status()
12+
return response
13+
14+
15+
@uplink.response_handler
16+
def response_to_data(response: Response):
17+
try:
18+
return response.json()
19+
except Exception as x:
20+
raise FormatError("Invalid format, could not parse JSON. Error: {}, status={}, text={}".format(
21+
x, response.status_code, response.text
22+
)) from x

0 commit comments

Comments
 (0)