forked from cetrez/Hack-the-Castle-2018
-
Notifications
You must be signed in to change notification settings - Fork 3
/
WitClient.py
66 lines (53 loc) · 1.92 KB
/
WitClient.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
63
64
65
import requests
class HttpClient:
base_url = 'http://httpbin.org/post'
@staticmethod
def print_request(req):
print('HTTP/1.1 {method} {url}\n{headers}\n\n{body}'.format(
method=req.method,
url=req.url,
headers='\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
body=req.body,
))
@staticmethod
def print_response(res):
print('HTTP/1.1 {status_code}\n{headers}\n\n{body}'.format(
status_code=res.status_code,
headers='\n'.join('{}: {}'.format(k, v) for k, v in res.headers.items()),
body=res.content,
))
class WitClient(HttpClient):
base_url = 'https://api.wit.ai'
version = '20170307'
def __init__(self, token):
self.token = token
def _get_header(self):
hed = {'Authorization': 'Bearer ' + self.token,
'Content-Type': 'application/json'}
return hed
@staticmethod
def _apply_version(url):
return url + '?v=' + WitClient.version
def create_entity(self, entity_id):
url = WitClient._apply_version(WitClient.base_url + '/entities')
data = {'id': entity_id}
header = self._get_header()
response = requests.post(url=url, json=data, headers=header)
WitClient.print_response(response)
return response.status_code
def create_sample(self, entity_id, exprs):
value = entity_id
data = []
for text in exprs:
data.append({
'text': text,
'entities': [{
'entity': entity_id,
'value': value
}]
})
url = WitClient._apply_version(WitClient.base_url + '/samples')
header = self._get_header()
response = requests.post(url=url, json=data, headers=header)
WitClient.print_response(response)
return response.status_code