gdp / apps / rest-test / restful_demo_py.html @ master
History | View | Annotate | Download (3.33 KB)
1 |
<html>
|
---|---|
2 |
<head><title>RESTful Demo (Mac, Python Client)</title></head> |
3 |
<body>
|
4 |
<pre>
|
5 |
#!/opt/local/bin/python2.7 |
6 |
|
7 |
# Warning: |
8 |
# |
9 |
# The MacOS 10.12 default python suffers from an openssl bug, |
10 |
# so an alternate python2.7 package was installed for this demo... |
11 |
# |
12 |
|
13 |
import json |
14 |
import requests |
15 |
|
16 |
def page_display(p): |
17 |
if p != None: |
18 |
print "STATUS:" |
19 |
print p.status_code |
20 |
print "HEADERS:" |
21 |
print p.headers |
22 |
print "CONTENT:" |
23 |
print p.content |
24 |
else: |
25 |
print "Error: no page" |
26 |
# |
27 |
|
28 |
# shared |
29 |
test_auth = ("PLEASE_EDIT_username", "PLEASE_EDIT_password") |
30 |
json_header = { 'Content-type': 'application/json' } |
31 |
|
32 |
def demo_write(demo_value, gcl_name): |
33 |
json_body = { |
34 |
"demo" : demo_value, |
35 |
"gcl_name" : gcl_name |
36 |
} |
37 |
wrec = requests.post("https://gdp-rest-01.eecs.berkeley.edu/gdp/v1/gcl/" + |
38 |
gcl_name, |
39 |
auth = test_auth, |
40 |
timeout = 60, |
41 |
headers = json_header, |
42 |
data = json.dumps(json_body)) # serialize json_body |
43 |
print "\n==== PUT REC Response Page:" |
44 |
page_display(wrec) |
45 |
print "==== PUT GET REC Response Page\n" |
46 |
|
47 |
if wrec.status_code == 200: |
48 |
print "Info: wrote {}".format(demo_value) |
49 |
return True |
50 |
else: |
51 |
print "Error: write to GCL {}".format(gcl_name) |
52 |
return False |
53 |
|
54 |
# |
55 |
|
56 |
def demo_read(demo_value, gcl_name): |
57 |
# In a get request, recno= may be positive or negative, where 1 obtains |
58 |
# the first record and -1 obtains the last record by counting backwards |
59 |
# one record from the log end. |
60 |
rrec = requests.get("https://gdp-rest-01.eecs.berkeley.edu/gdp/v1/gcl/" + |
61 |
gcl_name + "?recno=last", |
62 |
auth = test_auth, |
63 |
timeout = 60) |
64 |
|
65 |
print "\n==== GET REC Response Page:" |
66 |
page_display(rrec) |
67 |
print "==== END GET REC Response Page\n" |
68 |
|
69 |
rrec_j = rrec.json() |
70 |
if rrec.status_code == 200 and rrec_j["demo"] == demo_value: |
71 |
print "Info: read {}".format(rrec_j["demo"]) |
72 |
return True |
73 |
else: |
74 |
print "Error: read from GCL {}".format(gcl_name) |
75 |
return False |
76 |
# |
77 |
|
78 |
def demo_restful_use(): |
79 |
|
80 |
json_body = { |
81 |
# log name convention: "reverse_org_id.device.device_id", |
82 |
# for example "edu.berkeley.eecs.swarmlab.device.01aa02bb03cc" |
83 |
"external-name" : "PLEASE_EDIT_external_name_of_log", |
84 |
} |
85 |
|
86 |
pgcl = requests.put("https://gdp-rest-01.eecs.berkeley.edu/gdp/v1/gcl", |
87 |
auth = test_auth, |
88 |
timeout = 60, |
89 |
headers = json_header, |
90 |
data = json.dumps(json_body)) # serialize json_body |
91 |
|
92 |
print "\n==== PUT GCL Response Page:" |
93 |
page_display(pgcl) |
94 |
print "==== END PUT GCL Response Page\n" |
95 |
|
96 |
if pgcl.status_code == 201: |
97 |
pgcl_j = pgcl.json() |
98 |
gcl_name = pgcl_j["gcl_name"] |
99 |
gdplogd_name = pgcl_j["gdplogd_name"] |
100 |
if gcl_name != None and gdplogd_name != None: |
101 |
print "Info: created GCL {}".format(gcl_name) |
102 |
if demo_write("20170709 EC Workshop", gcl_name): |
103 |
demo_read("20170709 EC Workshop", gcl_name) |
104 |
else: |
105 |
print "Error: demo_write failed" |
106 |
else: |
107 |
print "Warning: response status code {}".format(pgcl.status_code) |
108 |
# |
109 |
|
110 |
demo_restful_use() |
111 |
|
112 |
</pre>
|
113 |
</body>
|
114 |
</html>
|