-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add v2 endpoint for telephony log (#208)
* Add v2 endpoint for telephony log * Fix test by correcting expected output
- Loading branch information
1 parent
3ed320d
commit dd195b1
Showing
12 changed files
with
370 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ dist | |
.idea | ||
env/ | ||
py3env/ | ||
.venv | ||
duo_client.egg-info | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from __future__ import absolute_import | ||
from .telephony import Telephony | ||
|
||
__all__ = [ | ||
'Telephony' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Dict, Sequence, Tuple | ||
from datetime import datetime, timedelta, timezone | ||
|
||
|
||
def get_params_from_kwargs(valid_params: Sequence[str], **kwargs) -> Dict: | ||
params = {} | ||
for k in kwargs: | ||
if kwargs[k] is not None and k in valid_params: | ||
params[k] = kwargs[k] | ||
return params | ||
|
||
|
||
def get_log_uri(log_type: str, version: int = 1) -> str: | ||
return f"/admin/v{version}/logs/{log_type}" | ||
|
||
|
||
def get_default_request_times() -> Tuple[int, int]: | ||
today = datetime.now(tz=timezone.utc) | ||
mintime = int((today - timedelta(days=180)).timestamp() * 1000) | ||
maxtime = int(today.timestamp() * 1000) - 120 | ||
return mintime, maxtime |
Oops, something went wrong.