/
request.py
119 lines (103 loc) · 3.52 KB
/
request.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- coding: utf-8 -*-
import urllib2
import urllib
import json
from config import config
from classes.exceptions.exceptions import WebAPIException
class BaseReq(object):
def __init__(self):
self.api_url = '%s/%s' % (config.wapi['url'], config.wapi['version'])
self.base_params = {
'key': config.wapi['key']
}
def request(self, url, params):
"""
:type url: str or unicode
:type params: dict
:rtype: dict
"""
data = urllib.urlencode(params)
req = urllib2.Request('%s?%s' % (url, data)) # GET-method
response = urllib2.urlopen(req).read()
try:
result = json.loads(response)
except ValueError:
raise WebAPIException('Response not in json')
else:
if not result['meta']['code'] == 200 and not result['meta']['code'] == 404:
raise WebAPIException('Response code is %s, API response:\n%s' % (result['meta']['code'], result))
return result['result']
class GeoSearch(BaseReq):
def __init__(self):
super(GeoSearch, self).__init__()
def get(self, coord, zoom):
"""
:param coord: diсt of lat and lng
:param zoom: int or str
:return: dict
"""
method = '/geo/search'
params = {
'key': self.base_params['key'],
'point': '%s,%s' % (coord['lng'], coord['lat']),
'zoom_level': str(zoom),
'fields': 'items.geometry.selection,items.links,items.adm_div,items.address,items.floors,items.description',
'type': 'adm_div.settlement,adm_div.city,adm_div.division,adm_div.district,'
'street,building,adm_div.place,poi,attraction'
}
url = self.api_url + method
return self.request(url, params)
class FirmList(BaseReq):
def __init__(self):
super(FirmList, self).__init__()
def get(self, build_id, page=1):
"""
:param build_id: int or str
:param page: int or str
:return: dict
"""
method = '/catalog/branch/list'
params = {
'key': self.base_params['key'],
'building_id': str(build_id),
'page': str(page)
}
url = "%s%s" % (self.api_url, method)
return self.request(url, params)
class FirmInfo(BaseReq):
def __init__(self):
super(FirmInfo, self).__init__()
def get(self, firm_id):
"""
:param firm_id: int or str
:return: dict
"""
method = '/catalog/branch/get'
params = {
'key': self.base_params['key'],
'id': str(firm_id),
'type': 'filial',
'fields': 'items.reviews,items.photos,items.links,items.external_content'
}
url = "%s%s" % (self.api_url, method)
return self.request(url, params)
class GalleryInfo(BaseReq):
def __init__(self):
super(GalleryInfo, self).__init__()
def get(self, id):
"""
:param id: int or str
:return: dict
"""
method = '/geo/get'
params = {
'key': self.base_params['key'],
'id': str(id),
'type': 'filial',
'fields': 'items.geometry.selection,items.links,items.adm_div,items.address,items.floors,items.description'
}
print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
print(params['fields'])
print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
url = "%s%s" % (self.api_url, method)
return self.request(url, params)