-
Notifications
You must be signed in to change notification settings - Fork 0
/
edqm_headers.py
61 lines (50 loc) · 1.86 KB
/
edqm_headers.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
from datetime import datetime
from typing import Dict
from enum import auto as enum_auto
from strenum import StrEnum
from edqm_hmac import edqm_hmac
import logging
class HttpMethod(StrEnum):
GET = enum_auto()
HEAD = enum_auto()
POST = enum_auto()
PUT = enum_auto()
DELETE = enum_auto()
CONNECT = enum_auto()
OPTIONS = enum_auto()
TRACE = enum_auto()
PATCH = enum_auto()
class HeaderBuilder:
username: str
api_key: str
host: str
def __init__(self, username, api_key):
self.username = username
self.api_key = api_key
@staticmethod
def __generate_date_header() -> str:
dt = datetime.utcnow()
formatted = dt.strftime("%a, %d %b %Y %H:%M:%S GMT")
logging.debug("Formatted date header: %s", formatted)
return formatted
def __generate_x_stapi_auth(self, request_url, verb, date, host):
string_to_sign = f"{verb}&{request_url}&{host}&{date}"
logging.debug("Signing: \"%s\"", string_to_sign)
signed = edqm_hmac(self.api_key, string_to_sign)
header = f"{self.username}|{signed}"
logging.debug("Generated header: \"%s\"", header)
return header
def generate_headers(self, request_url: str, verb: HttpMethod, host: str) -> Dict[str, str]:
date = self.__generate_date_header()
headers = {
"Date": date,
"X-STAPI-KEY": self.__generate_x_stapi_auth(request_url, verb, date, host),
"Accept": "application/json"
}
logging.debug("Generated headers: %s", headers)
return headers
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
builder = HeaderBuilder("[email protected]", "mysecret")
generated_headers = builder.generate_headers("/standardterms/api/v1/languages", HttpMethod.GET, "standardterms.edqm.eu")
print(generated_headers)