Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/db_web.py
447 views
1
"""
2
Web Database Adapter
3
"""
4
5
import json
6
import urllib
7
8
9
import tornado.httpclient
10
11
12
import db
13
14
15
class DB(db.DB):
16
"""
17
:arg URL str: the URL for the key-value store
18
"""
19
20
def __init__(self, url):
21
self.url = url
22
23
async def add(self, code, language, interacts):
24
"""
25
See :meth:`db.DB.add`
26
"""
27
body = urllib.parse.urlencode({
28
"code": code.encode("utf8"),
29
"language": language.encode("utf8"),
30
"interacts": interacts.encode("utf8")})
31
http_client = tornado.httpclient.AsyncHTTPClient()
32
response = await http_client.fetch(
33
self.url, method="POST", body=body,
34
headers={"Accept": "application/json"})
35
return json.loads(response.body)["query"]
36
37
async def get(self, key):
38
"""
39
See :meth:`db.DB.get`
40
"""
41
http_client = tornado.httpclient.AsyncHTTPClient()
42
response = await http_client.fetch(
43
"{}?q={}".format(self.url, key), method="GET",
44
headers={"Accept": "application/json"})
45
return json.loads(response.body)
46
47