-
Notifications
You must be signed in to change notification settings - Fork 0
/
CertRequester.py
56 lines (45 loc) · 2.38 KB
/
CertRequester.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
import os
import requests
from requests import Response
class CertRequester(requests.Session):
def __init__(self, cert_path='', key_path='', first_part_url=''):
super().__init__()
self.cert_path = cert_path
self.key_path = key_path
self.first_part_url = first_part_url
if not os.path.isfile(cert_path):
raise FileNotFoundError(cert_path + " is not a valid path. Cert file does not exist.")
if not os.path.isfile(key_path):
raise FileNotFoundError(key_path + " is not a valid path. Key file does not exist.")
def get(self, url='', **kwargs) -> Response:
kwargs = self.modify_kwargs_for_bearer_token(kwargs)
return super().get(url=self.first_part_url + url, cert=(self.cert_path, self.key_path), **kwargs)
def post(self, url='', **kwargs) -> Response:
kwargs = self.modify_kwargs_for_bearer_token(kwargs)
return super().post(url=self.first_part_url + url, cert=(self.cert_path, self.key_path), **kwargs)
def put(self, url='', **kwargs) -> Response:
kwargs = self.modify_kwargs_for_bearer_token(kwargs)
return super().put(url=self.first_part_url + url, cert=(self.cert_path, self.key_path), **kwargs)
def patch(self, url='', **kwargs) -> Response:
kwargs = self.modify_kwargs_for_bearer_token(kwargs)
return super().patch(url=self.first_part_url + url, cert=(self.cert_path, self.key_path), **kwargs)
def delete(self, url='', **kwargs) -> Response:
kwargs = self.modify_kwargs_for_bearer_token(kwargs)
return super().delete(url=self.first_part_url + url, cert=(self.cert_path, self.key_path), **kwargs)
@staticmethod
def modify_kwargs_for_bearer_token(kwargs: dict) -> dict:
if 'headers' not in kwargs:
kwargs['headers'] = {}
for arg in kwargs:
if arg == 'headers':
headers = kwargs[arg]
if 'accept' not in headers:
headers['accept'] = ''
if headers["accept"] is not None:
if headers["accept"] != '':
headers["accept"] = headers["accept"] + ", application/json"
else:
headers["accept"] = "application/json"
headers['Content-Type'] = 'application/vnd.awv.eminfra.v1+json'
kwargs['headers'] = headers
return kwargs