-
Notifications
You must be signed in to change notification settings - Fork 13
/
call.py
executable file
·57 lines (42 loc) · 1.62 KB
/
call.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
# TME API - example usage with Python
# More info and create application at: https://developers.tme.eu
import collections, urllib, base64, hmac, hashlib, urllib2, json
def product_import_tme():
# /product/product_import_tme/
token = '<TOKEN>'
app_secret = '<APP SECRET>'
params = {
'SymbolList[0]' : 'NE555D',
'SymbolList[1]' : '1N4007-DC',
'Country': 'PL',
'Currency': 'PLN',
'Language': 'PL',
}
response = api_call('Products/GetPrices', params, token, app_secret, True);
response = json.loads(response);
print response;
def api_call(action, params, token, app_secret, show_header=False):
api_url = 'https://api.tme.eu/' + action + '.json';
params['Token'] = token;
params = collections.OrderedDict(sorted(params.items()));
encoded_params = urllib.urlencode(params, '')
signature_base = 'POST' + '&' + urllib.quote(api_url, '') + '&' + urllib.quote(encoded_params, '')
api_signature = base64.encodestring(hmac.new(app_secret, signature_base, hashlib.sha1).digest()).rstrip();
params['ApiSignature'] = api_signature;
opts = {
'http': {
'method' : 'POST',
'header' : 'Content-type: application/x-www-form-urlencoded',
'content' : urllib.urlencode(params)
}
};
http_header = {
"Content-type": "application/x-www-form-urlencoded",
};
# create your HTTP request
req = urllib2.Request(api_url, urllib.urlencode(params), http_header);
# submit your request
res = urllib2.urlopen(req);
html = res.read();
return html;
print product_import_tme();