-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-client.py
64 lines (57 loc) · 1.76 KB
/
rest-client.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
#!/usr/bin/env python3
import requests
import json
import os
import sys
#
# Use localhost & port 5000 if not specified by environment variable REST
#
REST = os.getenv("REST") or "34.117.188.46"
##
# The following routine makes a JSON REST query of the specified type
# and if a successful JSON reply is made, it pretty-prints the reply
##
def mkReq(reqmethod, endpoint, data):
print(f"Response to http://{REST}/{endpoint} request is")
jsonData = json.dumps(data)
response = reqmethod(f"http://{REST}/{endpoint}", data=jsonData,
headers={'Content-type': 'application/json'})
if response.status_code == 200:
jsonResponse = json.dumps(response.json(), indent=4, sort_keys=True)
print(jsonResponse)
return
else:
print(
f"response code is {response.status_code}, raw response is {response.text}")
return response.text
loop = True
while loop:
print("Enter the option:")
print("a) addPlayer")
print("b) showPlayerData")
print("c) showGameData")
print("d) exit")
opt = input()
if opt in ['a', 'A']:
playerTag = input("Enter the player tag: ")
mkReq(requests.post, "apiv1/addPlayer",
data={
"playerTag" : playerTag
})
elif opt in ['b', 'B']:
playerTag = input("Enter the player tag: ")
mkReq(requests.get, "apiv1/showPlayerData",
data={
"playerTag" : playerTag
})
elif opt in ['c', 'C']:
gameId = input("Enter the game ID: ")
mkReq(requests.get, "apiv1/showGameData",
data={
"gameId" : gameId
})
elif opt in ['d', 'D']:
loop = False
else:
print("Invalid option")
sys.exit(0)