forked from jakmanne/api-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleapp.py
72 lines (54 loc) · 2.24 KB
/
sampleapp.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
from oauthlib.oauth2 import BackendApplicationClient
import requests
from requests_oauthlib import OAuth2Session
import urllib.parse
def enable_debug_logging():
import logging
import http.client
http.client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def create_authenticated_http_session(client_id, client_secret) -> requests.Session:
oauth2_client = BackendApplicationClient(client_id=urllib.parse.quote(client_id))
session = OAuth2Session(client=oauth2_client)
session.fetch_token(
token_url='https://api.sbanken.no/identityserver/connect/token',
client_id=urllib.parse.quote(client_id),
client_secret=urllib.parse.quote(client_secret)
)
return session
def get_customer_information(http_session: requests.Session, customerid):
response_object = http_session.get(
"https://api.sbanken.no/customers/api/v1/Customers",
headers={'customerId': customerid}
)
print(response_object)
print(response_object.text)
response = response_object.json()
if not response["isError"]:
return response["item"]
else:
raise RuntimeError("{} {}".format(response["errorType"], response["errorMessage"]))
def get_accounts(http_session: requests.Session, customerid):
response = http_session.get(
"https://api.sbanken.no/bank/api/v1/Accounts",
headers={'customerId': customerid}
).json()
if not response["isError"]:
return response["items"]
else:
raise RuntimeError("{} {}".format(response["errorType"], response["errorMessage"]))
def main():
# enable_debug_logging()
import api_settings
import pprint
http_session = create_authenticated_http_session(api_settings.CLIENTID, api_settings.SECRET)
customer_info = get_customer_information(http_session, api_settings.CUSTOMERID)
pprint.pprint(customer_info)
accounts = get_accounts(http_session, api_settings.CUSTOMERID)
pprint.pprint(accounts)
if __name__ == "__main__":
main()