-
Notifications
You must be signed in to change notification settings - Fork 1
/
short-sample-request.py
55 lines (47 loc) · 1.67 KB
/
short-sample-request.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
#!/usr/bin/env python3
import requests
import json, jsonpickle
import os
import sys
import base64
import glob
#
# Use localhost & port 5000 if not specified by environment variable REST
#
REST = os.getenv("REST") or "localhost:5000"
##
# 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, verbose=True):
print(f"Response to http://{REST}/{endpoint} request is {type(data)}")
jsonData = jsonpickle.encode(data)
if verbose and data != None:
print(f"Make request http://{REST}/{endpoint} with json {data.keys()}")
print(f"mp3 is of type {type(data['mp3'])} and length {len(data['mp3'])} ")
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
for mp3 in glob.glob("data/short*mp3"):
print(f"Separate data/{mp3}")
mkReq(requests.post, "apiv1/separate",
data={
"mp3": base64.b64encode( open(mp3, "rb").read() ).decode('utf-8'),
"callback": {
"url": "http://localhost:5000",
"data": {"mp3": mp3,
"data": "to be returned"}
}
},
verbose=True
)
print(f"Cache from server is")
mkReq(requests.get, "apiv1/queue", data=None)
sys.exit(0)