-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI_lib.py
46 lines (42 loc) · 1.52 KB
/
API_lib.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
import os, sys
import json
import urllib
server_url = 'https://aping-pong.herokuapp.com'
def call(path, id_code='', show_cmd=False, is_post=False, **kwargs):
"""Execute curl commands to access game API endpoints.
Inputs
------
path: API endpoint leading slash [string]
id_code: Unique private id [string]
show_cmd: Print the full curl command executed to stdout [bool, default False]
is_post: Not currently used for this game, but controls how parameters are passed
**kwargs: Other keyword arguments to pass either as regular parameters (is_post=False) or
as form parameters (is_post=True)
"""
params = []
if id_code != '':
params.append(('private_id', id_code))
path = urllib.parse.quote(path)
# silent mode for curl
cmd = f'curl -s "{server_url}/{path}'
if is_post:
# add trailing double quote prior to form params
cmd += '?' + urllib.parse.urlencode(params) + '"'
for k, v in kwargs.items():
cmd += f" --form {k}={v}"
else:
params.extend(list(kwargs.items()))
cmd += '?' + urllib.parse.urlencode(params) + '"'
if show_cmd:
print(cmd)
res = os.popen(cmd).read()
try:
return json.loads(res)
except:
return res
def declare_player(level, pID=None, show_cmd=False):
if pID is None:
info = call('request_game/{}'.format(level), show_cmd=show_cmd)
else:
info = call('request_game/{}/{}'.format(level, pID), show_cmd=show_cmd)
return info