-
Notifications
You must be signed in to change notification settings - Fork 1
/
datatracker.py
46 lines (37 loc) · 1.55 KB
/
datatracker.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
# Copyright The IETF Trust 2024, All Rights Reserved
import api
from base64 import b64encode
import json
import logging
from urllib import parse
log = logging.getLogger("api")
class DatatrackerApi(api.Api):
"""API for posting messages to the datatracker"""
default_base_url = "https://datatracker.ietf.org"
default_mail_subdomain = "datatracker"
post_message_endpoint = "api/email/"
def post_message(self, dest: str, message: bytes) -> None:
payload = {
"dest": dest,
"message": b64encode(message).decode(),
}
url = parse.urljoin(self.base_url, self.post_message_endpoint)
response = self.post_with_auth(url, payload)
log.debug(f"API responded with status {response.status}")
if response.status != 200:
raise api.ApiError()
log.debug(f"Response Content-Type was {response.headers['Content-Type']}")
if response.headers["Content-Type"] != "application/json":
raise api.ApiError()
api_response = json.loads(response.read())
log.debug(f"API result was {api_response['result']}")
if api_response["result"] == "bad_dest":
raise api.BadDestinationError()
if api_response["result"] == "bad_msg":
raise api.BadMessageError()
if api_response["result"] != "ok":
raise api.UnknownError()
# if we got here, that means success!
class DatatrackerTestApi(DatatrackerApi):
"""API for testing message delivery to the datatracker"""
post_message_endpoint = "api/email/test/"