]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/zeiterfassung.py
6 import tornado
.httpclient
11 from misc
import Object
13 class ZeiterfassungClient(Object
):
14 algorithm
= "Zeiterfassung-HMAC-SHA512"
17 self
.url
= self
.settings
.get("zeiterfassung_url")
20 self
.api_key
= self
.settings
.get("zeiterfassung_api_key")
21 self
.api_secret
= self
.settings
.get("zeiterfassung_api_secret")
23 # Check if all configuration values are set
24 if not all((self
.url
, self
.api_key
, self
.api_secret
)):
25 raise RuntimeError("%s is not configured" % self
.__class
__.__name
__)
28 self
.http_client
= tornado
.httpclient
.AsyncHTTPClient(
30 "User-Agent" : "IPFireWebApp",
34 def _make_signature(self
, method
, path
, body
):
35 # Empty since we only support POST
38 # Put everything together
39 string_to_sign
= "\n".join((
40 method
, path
, canonical_query
,
41 )).encode("utf-8") + body
44 h
= hmac
.new(self
.api_secret
.encode("utf-8"), string_to_sign
, hashlib
.sha512
)
48 @tornado.gen
.coroutine
49 def send_request(self
, path
, **kwargs
):
50 url
= urlparse
.urljoin(self
.url
, path
)
52 # Query arguments are all keyword arguments
55 request
= tornado
.httpclient
.HTTPRequest(url
, method
="POST")
56 request
.body
= urllib
.urlencode(arguments
)
58 # Compose the signature
59 signature
= self
._make
_signature
("POST", path
, request
.body
)
61 # Add authorization header
62 request
.headers
["Authorization"] = " ".join(
63 (self
.algorithm
, self
.api_key
, signature
)
67 response
= yield self
.http_client
.fetch(request
)
69 # Decode the JSON response
70 d
= json
.loads(response
.body
.decode())
72 raise tornado
.gen
.Return(d
)