]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/zeiterfassung.py
6 import tornado
.httpclient
10 from .misc
import Object
12 class ZeiterfassungClient(Object
):
13 algorithm
= "Zeiterfassung-HMAC-SHA512"
16 self
.url
= self
.settings
.get("zeiterfassung_url")
19 self
.api_key
= self
.settings
.get("zeiterfassung_api_key")
20 self
.api_secret
= self
.settings
.get("zeiterfassung_api_secret")
22 # Check if all configuration values are set
23 if not all((self
.url
, self
.api_key
, self
.api_secret
)):
24 raise RuntimeError("%s is not configured" % self
.__class
__.__name
__)
27 self
.http_client
= tornado
.httpclient
.AsyncHTTPClient(
29 "User-Agent" : "IPFireWebApp",
33 def _make_signature(self
, method
, path
, body
):
34 # Empty since we only support POST
37 # Put everything together
38 string_to_sign
= "\n".join((
39 method
, path
, canonical_query
,
40 )).encode("utf-8") + body
43 h
= hmac
.new(self
.api_secret
.encode("utf-8"), string_to_sign
, hashlib
.sha512
)
47 @tornado.gen
.coroutine
48 def send_request(self
, path
, **kwargs
):
49 url
= urllib
.parse
.urljoin(self
.url
, path
)
51 # Query arguments are all keyword arguments
54 request
= tornado
.httpclient
.HTTPRequest(url
, method
="POST")
55 request
.body
= urllib
.parse
.urlencode(arguments
)
57 # Compose the signature
58 signature
= self
._make
_signature
("POST", path
, request
.body
)
60 # Add authorization header
61 request
.headers
["Authorization"] = " ".join(
62 (self
.algorithm
, self
.api_key
, signature
)
66 response
= yield self
.http_client
.fetch(request
)
68 # Decode the JSON response
69 d
= json
.loads(response
.body
.decode())
71 raise tornado
.gen
.Return(d
)