-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
db_web.py
45 lines (35 loc) · 1.09 KB
/
db_web.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
"""
Web Database Adapter
"""
import json
import urllib
import tornado.httpclient
import db
class DB(db.DB):
"""
:arg URL str: the URL for the key-value store
"""
def __init__(self, url):
self.url = url
async def add(self, code, language, interacts):
"""
See :meth:`db.DB.add`
"""
body = urllib.parse.urlencode({
"code": code.encode("utf8"),
"language": language.encode("utf8"),
"interacts": interacts.encode("utf8")})
http_client = tornado.httpclient.AsyncHTTPClient()
response = await http_client.fetch(
self.url, method="POST", body=body,
headers={"Accept": "application/json"})
return json.loads(response.body)["query"]
async def get(self, key):
"""
See :meth:`db.DB.get`
"""
http_client = tornado.httpclient.AsyncHTTPClient()
response = await http_client.fetch(
"{}?q={}".format(self.url, key), method="GET",
headers={"Accept": "application/json"})
return json.loads(response.body)