Skip to content

E-Log Service

elog_client.py

Elog API client for posting entries and fetching user and logbook information.

get_user()

Fetches the user information from the ELOG API. Also used to verify the API key. :return: A tuple containing the status code and the user data or exception.

Source code in trace/services/elog_client.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def get_user() -> tuple[int, dict | Exception]:
    """
    Fetches the user information from the ELOG API. Also used to verify the API key.
    :return: A tuple containing the status code and the user data or exception.
    """
    url = f"{ELOG_API_URL}/v1/users/me"
    headers = {"x-vouch-idp-accesstoken": ELOG_API_KEY}
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        return response.status_code, response.json()
    except requests.exceptions.RequestException as e:
        logger.error(e)
        return response.status_code, e

post_entry(title, body, logbooks, image_bytes, config_file_path=None)

Posts a new entry with image to the ELOG API.

:param title: The title of the entry. :param body: The body of the entry. :param logbooks: A list of logbook names to post the entry to. :param image_bytes: Bytes of the image to be attached to the entry. :param config_file: Optional, path of config file to attach. :return: A tuple containing the status code and the response data or exception.

Source code in trace/services/elog_client.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
def post_entry(
    title: str, body: str, logbooks: list[str], image_bytes, config_file_path: Path | None = None
) -> tuple[int, dict | Exception]:
    """
    Posts a new entry with image to the ELOG API.

    :param title: The title of the entry.
    :param body: The body of the entry.
    :param logbooks: A list of logbook names to post the entry to.
    :param image_bytes: Bytes of the image to be attached to the entry.
    :param config_file: Optional, path of config file to attach.
    :return: A tuple containing the status code and the response data or exception.
    """
    url = f"{ELOG_API_URL}/v2/entries"
    headers = {"x-vouch-idp-accesstoken": ELOG_API_KEY}

    entry_data = {"title": title, "text": body, "logbooks": logbooks}
    entry_json = json.dumps(entry_data).encode("utf-8")

    files = [
        ("entry", ("entry.json", entry_json, "application/json")),
        ("files", ("trace_plot.png", image_bytes, "image/png")),
    ]
    if config_file_path is not None:
        with open(config_file_path, "rb") as f:
            config_bytes = f.read()
        files.append(("files", (config_file_path.name, config_bytes, "application/octet-stream")))
    try:
        response = requests.post(
            url,
            headers=headers,
            files=files,
        )
        response.raise_for_status()
        return response.status_code, response.json()
    except requests.exceptions.RequestException as e:
        logger.error(e)
        return response.status_code, e

get_logbooks()

Fetches the list of logbooks from the ELOG API.

:return: A tuple containing the status code and a list of logbook names or an exception.

Source code in trace/services/elog_client.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def get_logbooks() -> tuple[int, list[str] | Exception]:
    """
    Fetches the list of logbooks from the ELOG API.

    :return: A tuple containing the status code and a list of logbook names or an exception.
    """
    url = f"{ELOG_API_URL}/v1/logbooks"
    headers = {"x-vouch-idp-accesstoken": ELOG_API_KEY}

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        return response.status_code, [logbook["name"] for logbook in response.json()["payload"]]
    except requests.exceptions.RequestException as e:
        logger.error(e)
        return response.status_code, e