forked from commonsense/conceptnet5
-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.py
62 lines (47 loc) · 1.47 KB
/
client.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
56
57
58
59
60
61
62
"""Simple client to making RESTful requests to the server."""
import argparse
import json
import logging
import os
import requests
logging.basicConfig(
level=os.environ.get("LOGLEVEL", "INFO").upper(),
format="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def write_json(content: dict, fname: str) -> None:
"""Write json.
Args
----
content: content to write.
fname: filename to be saved.
"""
logging.debug(f"writing json {fname} ...")
with open(fname, "w") as stream:
json.dump(content, stream, indent=4, sort_keys=False)
def main(server_address: str, object_: str) -> None:
"""Make a HTTP get request to the server and print out the response.
Args
----
server_address: server address
object_: head of interest
"""
query = f"{server_address}/query?start=/c/en/{object_}&rel=/r/AtLocation"
response = requests.get(query).json()
print(response)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="simple client")
parser.add_argument(
"--server_address",
type=str,
default="http://api.conceptnet.io",
help="http://api.conceptnet.io" or "http://127.0.0.1:8084",
)
parser.add_argument(
"--object",
type=str,
default="laptop",
help="laptop, baseball, person, etc.",
)
args = parser.parse_args()
main(args.server_address, args.object)