Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasekit-api-example.py
More file actions
Latest commit
108 lines (85 loc) · 2.61 KB
/
Copy pathbasekit-api-example.py
File metadata and controls
108 lines (85 loc) · 2.61 KB
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
#/usr/bin/env python
importoauth2
importhttplib
importjson
classOAuthClient():
'''
Very simple oauth client
'''
def__init__(self, url):
self.credentials({
'consumer_key': '',
'consumer_secret': '',
'access_token': '',
'access_secret': ''
})
self.request=oauth2.Request.from_consumer_and_token(
self.consumer,
token=self.token,
http_url=url
)
self.request.sign_request(
oauth2.SignatureMethod_HMAC_SHA1(),
self.consumer,
self.token
)
defcredentials(self, credentials):
self.token=oauth2.Token(
key=credentials['access_token'],
secret=credentials['access_secret']
)
self.consumer=oauth2.Consumer(
key=credentials['consumer_key'],
secret=credentials['consumer_secret']
)
classBaseKitApi():
'''
Returns a JSON responce to the API called
'''
def__init__(self, server='rest.example.com', port=80):
self.headers= {
'Content-Type': 'application/x-www-form-urlencoded'
}
self.server=server
self.port=port
ifself.server[-1] is'/':
self.server=self.server[:-1]
defsetHeaders(self, headers):
self.headers=headers
defcall(self, request_url, http_method='GET', parameters=[]):
ifself.servernotinrequest_url:
ifrequest_url[0] isnot'/':
url="{0}/{1}".format(self.server, request_url)
else:
url="{0}{1}".format(self.server, request_url)
else:
url=request_url
ifself.port==80:
url="http://{0}".format(url)
else:
url="https://{0}".format(url)
client=OAuthClient(url)
ifhttp_method.upper() =="POST":
encoded_post_data=client.request.to_postdata()
else:
encoded_post_data=None
url=client.request.to_url()
connection=httplib.HTTPConnection(
"%s:%d"% (self.server, self.port)
)
connection.request(
http_method.upper(),
url,
body=encoded_post_data,
headers=self.headers
)
response=connection.getresponse().read()
ifresponseisNone:
returnNone
else:
returnjson.loads(response)
http_method='GET'
parameters= []
api=BaseKitApi('api.example.com')
brand=api.call('/brands/1', http_method, parameters)
printbrand