forked from mtbentley/indix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindix.py
166 lines (147 loc) · 6.36 KB
/
indix.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import requests
import json
import os
class indixException(Exception):
pass
class indixRestException(indixException):
""" This should do something with error codes from indix. Maybe.
:param int status: the error code
:param str uri: the url
:param str msg: the message, if one exists
"""
def __init__(self, status, uri, msg=""):
self.uri=uri
self.status=status
self.msg=msg
def __str__(self):
return "ERROR %s: %s (%s)" % (self.status, self.msg, self.uri)
def find_credentials():
"""Look in the current environment for indix account creds"""
try:
app_id = os.environ["INDIX_APP_ID"]
app_key = os.environ["INDIX_APP_KEY"]
return app_id, app_key
except:
return None, None
def make_request(base_uri, endpoint, **kwargs):
"""
Magic function that makes the actual HTTP request
Takes first two params to make the url, **kwargs get added as GET request
"""
full_url = "%s/%s/?" % (base_uri, endpoint)
for key, value in kwargs.items():
full_url = "%s%s=%s&" % (full_url, key, value)
response = requests.get(full_url)
return response
def pretty_print(json_to_print, sort_keys=True):
"""A little convenience function for pretty pringing of json"""
print(json.dumps(json_to_print, sort_keys=sort_keys, indent=4, separators=(',', ': ')))
class IndixRestClient(object):
"""
A client for accessing the indix REST API
:param str app_id: The app ID
:param str app_key: The app key
"""
def __init__(self, app_id=None, app_key=None, base="http://api.indix.com/api", version="beta"):
"""Create a indix REST API client."""
# Get account creds
if not app_id or not app_key:
app_id, app_key = find_credentials()
if not app_id or not app_key:
raise indixException("""You need app_id and app_key when you initialize stuff,
or add environment variables (see readme)""")
self.base = base
self.app_id, self.app_key = app_id, app_key
self.version_uri = "%s/%s" % (base, version)
def brands(self, query=None):
"""
:param query: the brand you want to search for
Returns: request object from requests.get
"""
response = make_request(self.version_uri, "brands", query=query,
app_id=self.app_id, app_key=self.app_key)
if response.ok:
return response
else:
raise indixRestException(response.status_code, response.url, response.reason)
def stores(self, query=None):
"""
:param query: the stores you want to search for
Returns: request object from requests.get
"""
response = make_request(self.version_uri, "stores", query=query,
app_id=self.app_id, app_key=self.app_key)
if response.ok:
return response
else:
raise indixRestException(response.status_code, response.url, response.reason)
def categories(self):
"""
Takes not params. Returns json for all possible catagories
Returns: request object from requests.get
"""
response = make_request(self.version_uri, "categories",
app_id=self.app_id, app_key=self.app_key)
if response.ok:
return response
else:
raise indixRestException(response.status_code, response.url, response.reason)
def products(self, pageNumber=1, query="", storeId="", brandId="", categoryId="",
startPrice="", endPrice="", sortBy="", priceHistoryAvail=False):
"""
:param int pageNumber: page number to get
:param query: product to search for
:param storeId: storeId, form indix.stores
:param brandId: brandId, from indix.brands
:param categoryId: categoryId, from indix.categories
:param startPrice: low price
:param endPrice: high price
:sortBy: must be one of: "RELEVANCE", "PRICE_LOW_TO_HIGH", "PRICE_HIGH_TO_LOW", "MOST_RECENT"
or blank
:priceHistoryAvail bool: if True, will only return products with price history available
Returns: request object from requests.get for 10 products
"""
response = make_request(self.version_uri, "products", pageNumber=pageNumber, query=query,
storeId=storeId, brandId=brandId, categoryId=categoryId,
startPrice=startPrice, endPrice=endPrice, sortBy=sortBy,
priceHisotryAvail=priceHistoryAvail,
app_id=self.app_id, app_key=self.app_key)
if response.ok:
return response
else:
raise indixRestException(response.status_code, response.url, response.reason)
def productById(self, id=None, pageNumber=1):
"""
:param id: productId from indix.products
:param pageNumber: page number
Returns: request object from requests.get
"""
response = make_request(self.version_uri, "products/%s" % id, pageNumber=pageNumber,
app_id=self.app_id, app_key=self.app_key)
if response.ok:
return response
else:
raise indixRestException(response.status_code, response.url, response.reason)
def pricesById(self, id=None):
"""
:param id: productId from indix.products
Returns: request object from requests.get
"""
response = make_request(self.version_uri, "products/%s/prices" % id,
app_id=self.app_id, app_key=self.app_key)
if response.ok:
return response
else:
raise indixRestException(response.status_code, response.url, response.reason)
def raw(self, endpoint=None, **kwargs):
"""
:param endpoint: the url to all to the end of api.indix.com/api/beta/
:param **kwargs: dictionary of extra things to add to url as a GET request
Returns: request object from requests.get
"""
response = make_request(self.version_uri, endpoint,
app_id=self.app_id, app_key=self.app_key, **kwargs)
if response.ok:
return response
else:
raise indixRestException(response.status_code, response.url, response.reason)