File: telephony.py

package info (click to toggle)
python-duo-client 5.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 784 kB
  • sloc: python: 7,231; makefile: 7; sh: 6
file content (65 lines) | stat: -rw-r--r-- 1,892 bytes parent folder | download | duplicates (2)
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
62
63
64
65
from typing import Callable
from duo_client.util import (
    get_params_from_kwargs,
    get_log_uri,
    get_default_request_times,
)

VALID_TELEPHONY_V2_REQUEST_PARAMS = [
    "filters",
    "mintime",
    "maxtime",
    "limit",
    "sort",
    "next_offset",
    "account_id",
]

LOG_TYPE = "telephony"


class Telephony:
    @staticmethod
    def get_telephony_logs_v1(json_api_call: Callable, host: str, mintime=0):
        # Sanity check mintime as unix timestamp, then transform to string
        mintime = f"{int(mintime)}"
        params = {
            "mintime": mintime,
        }
        response = json_api_call(
            "GET",
            get_log_uri(LOG_TYPE, 1),
            params,
        )
        for row in response:
            row["eventtype"] = LOG_TYPE
            row["host"] = host
        return response

    @staticmethod
    def get_telephony_logs_v2(json_api_call: Callable, host: str, **kwargs):
        params = {}
        default_mintime, default_maxtime = get_default_request_times()

        params = get_params_from_kwargs(VALID_TELEPHONY_V2_REQUEST_PARAMS, **kwargs)

        if "mintime" not in params:
            # If mintime is not provided, the script defaults it to 180 days in past
            params["mintime"] = default_mintime
        params["mintime"] = f"{int(params['mintime'])}"
        if "maxtime" not in params:
            # if maxtime is not provided, the script defaults it to now
            params["maxtime"] = default_maxtime
        params["maxtime"] = f"{int(params['maxtime'])}"
        if "limit" in params:
            params["limit"] = f"{int(params['limit'])}"

        response = json_api_call(
            "GET",
            get_log_uri(LOG_TYPE, 2),
            params,
        )
        for row in response["items"]:
            row["eventtype"] = LOG_TYPE
            row["host"] = host
        return response