-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.py
40 lines (28 loc) · 1.14 KB
/
authentication.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
""" CIP-API Authentication module """
import requests
from gel_report_config import *
class APIAuthentication:
""" Generate token and and create a requests 'session'
http://docs.python-requests.org/en/master/user/advanced/#session-objects
"""
def __init__(self):
# url to submit user info to generate token
self.token_url = "https://cipapi.genomicsengland.nhs.uk/api/get-token/"
# Call modules to generate token
self.token = self.get_token()
def get_token(self):
# read username from file (username is defined in gel_report_config)
with open(username,'r') as f:
user=f.readline()
#read password from file (pw is defined in gel_report_config)
with open(pw,'r') as f:
password=f.readline()
# use requests module to submit the credentials and return the token
# if proxy is set in the config file:
if proxy:
return requests.post(self.token_url, {"username": user, "password":password},proxies=proxy).json()["token"]
else:
return requests.post(self.token_url, {"username": user, "password":password}).json()["token"]
if __name__ == "__main__":
auth = APIAuthentication()
print auth.token