-
Notifications
You must be signed in to change notification settings - Fork 1
/
visa_api.py
83 lines (69 loc) · 2.76 KB
/
visa_api.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
import json
import requests
import os
MAX_OFFERS = 100
MERCHANT_API_URL = f'https://sandbox.api.visa.com/vmorc/offers/v1/all?&&max_offers={MAX_OFFERS}'
def get_merchant_offers_by_country(country_code):
url = MERCHANT_API_URL + f'&&redemption_country={country_code}'
print(f'sending api request to {url}')
headers = {
'Authorization': 'Basic RkdKSURLRFpGSFFDQ0VTSTJCQTUyMTV1T19qYnlXU3k1RHFRd2JjbVFVNmR0dTQzbzpCQjJTa0tyM09jNHU=',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
curr_dir = os.path.dirname(os.path.abspath(__file__))
certs = (
str(curr_dir) + '/certs/cert.pem',
str(curr_dir) + '/certs/key_b391edad-e45c-4f85-8025-9ed6ed358427.pem'
)
r = requests.get(url, headers=headers, cert=certs)
r_dict = r.json()
response = []
for offer in r_dict['Offers']:
merchant_list = [{
'merchantId': merchant['merchantId'],
'merchant': merchant['merchant']
} for merchant in offer['merchantList']]
curr = {
'offerId': offer['offerId'],
'offerShortDescription': offer['offerShortDescription']['text'],
'offerTitle': offer['offerTitle'],
'visaTerms': offer['visaTerms']['text'],
'redemptionChannelList': offer['redemptionChannelList'],
'merchantList': merchant_list,
'redemptionUrl': offer['redemptionUrl']
}
response.append(curr)
return response
def get_merchant_offers_by_offerid(offer_id):
url = MERCHANT_API_URL + f'&&offerid={offer_id}'
print(f'sending api request to {url}')
headers = {
'Authorization': 'Basic RkdKSURLRFpGSFFDQ0VTSTJCQTUyMTV1T19qYnlXU3k1RHFRd2JjbVFVNmR0dTQzbzpCQjJTa0tyM09jNHU=',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
curr_dir = os.path.dirname(os.path.abspath(__file__))
certs = (
str(curr_dir) + '/certs/cert.pem',
str(curr_dir) + '/certs/key_b391edad-e45c-4f85-8025-9ed6ed358427.pem'
)
r = requests.get(url, headers=headers, cert=certs)
r_dict = r.json()
response = []
for offer in r_dict['Offers']:
merchant_list = [{
'merchantId': merchant['merchantId'],
'merchant': merchant['merchant']
} for merchant in offer['merchantList']]
curr = {
'offerId': offer['offerId'],
'offerShortDescription': offer['offerShortDescription']['text'],
'offerTitle': offer['offerTitle'],
'visaTerms': offer['visaTerms']['text'],
'redemptionChannelList': offer['redemptionChannelList'],
'merchantList': merchant_list,
'redemptionUrl': offer['redemptionUrl']
}
response.append(curr)
return response