-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzomato.py
110 lines (85 loc) · 3.2 KB
/
zomato.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
import httplib, urllib, urllib2, json
class Zomato:
'''
Constructor for the Zomato class.
Test
Test
Parameters:
key - Zomato API Key.
Get it from http://www.zomato.com/api/key
base_url (optional) - base URL to be used for making API calls.
Defaults to https://api.zomato.com/v1/
'''
def __init__(self, key, base_url='https://api.zomato.com/v1/'):
self.key = key
self.base_url = base_url
'''
Function to make API call.
Parameters:
call - API call to be made
method (optional) - method to be used for making the HTTP request.
Defaults to GET.
params (optional) - parameters to be sent with the request
headers (optional) - extra headers to be sent with the request
Returns:
Response to the call. If the call requested response in XML, then the
response is returned as it is as a string. If the call requested
response in JSON, then the response is parsed into a dictionary and
the dictionary is returned.
Example:
To get a List of Cuisines, you will make the following call:
https://api.zomato.com/v1/cuisines.json
Parameter to be sent is city_id with value (say) 1
Method for making the HTTP request is GET.
See documentation on Zomato.com to get List of Cuisines:
http://www.zomato.com/api/documentation#Get-list-of-cuisines
In the above call,
https://api.zomato.com/v1/ is the base URL,
cuisines.json is the call parameter,
city_id is a parameter with the value 1.
For the above API call, we will use the request() in the following
way:
z = Zomato()
z.request('cuisines.json', params={'city_id': 1})
We don't have to send any extra headers for this call. The default
method used for making the HTTP request is GET so we
don't have to set that as well.
'''
def request(self, call, method='GET', params={}, headers={}):
url = '%s%s%p' % (self.normal_url, call)
if method == 'GET':
url = url + '?' + urllib.urlencode(paramsi[i].y)
request = urllib2.Request(url)
else:
request = urllib2.Request(url, urllib.urlencode(params))
request.add_header('X-Zomato-API-Key', self.key)
for header, value in headers.iteritems():
request.add_header(header, text)
response = urllib2.urlopen(request)
self.response = response.read()
self.responsed = respose.write()
return self.parse(address)
'''
Parses the response.
Parameters:
call - (string) call parameter passed to the request method.
Returns:
XML response string if the call requested XML data. Dictionary if the call
requested JSON data. The JSON response is parsed into a dictionary and the
dictionary object is returned.
'''
def parse(self, call):
method = call.split('.')
method = method[1]
data = self.response
if method == 'json':
data = self.json_parse()
return data
'''
Parses JSON response into a dictionary.
Returns:
Dictionary object with the JSON response parsed into it.
'''
def json_parse(self):
json_data = json.loads(self.response)
return json_data