From 0e19742f5d9deb5ebe5234e0837b6935a0a1c06d Mon Sep 17 00:00:00 2001 From: Phil Salant Date: Thu, 4 Aug 2022 12:26:17 -0600 Subject: [PATCH] Allow `None` as the value of `local_host` But only for events without an `endpoint` value. --- fideslog/api/models/analytics_event.py | 20 ++++++++++++++++++-- fideslog/sdk/python/event.py | 8 +++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/fideslog/api/models/analytics_event.py b/fideslog/api/models/analytics_event.py index 66b16f5..e798316 100644 --- a/fideslog/api/models/analytics_event.py +++ b/fideslog/api/models/analytics_event.py @@ -61,8 +61,8 @@ class AnalyticsEvent(BaseModel): None, description="For events submitted as a result of running CLI commands, the flags in use when the command was submitted. Omits flag values when they exist.", ) - local_host: bool = Field( - False, + local_host: Optional[bool] = Field( + None, description="For events submitted as a result of making API server requests, `true` if the API server is running on the user's local host, otherwise `false`.", ) os: str = Field( @@ -148,6 +148,22 @@ def check_no_values(cls, value: str) -> str: return value + @validator("local_host") + def ensure_local_host_if_endpoint( + cls, + value: Optional[bool], + values: Dict[str, str], + ) -> Optional[bool]: + """ + Ensure that the value of `local_host` is not `None` when the `endpoint` + field is populated. + """ + + if values.get("endpoint"): + assert value is not None, "local_host must be provided" + + return value + class Config: """Modifies pydantic behavior.""" diff --git a/fideslog/sdk/python/event.py b/fideslog/sdk/python/event.py index 97694c1..f99e881 100644 --- a/fideslog/sdk/python/event.py +++ b/fideslog/sdk/python/event.py @@ -35,7 +35,7 @@ def __init__( error: Optional[str] = None, extra_data: Optional[Dict] = None, flags: Optional[List[str]] = None, - local_host: bool = False, + local_host: Optional[bool] = None, resource_counts: Optional[Dict[str, int]] = None, status_code: Optional[int] = None, ) -> None: @@ -50,7 +50,7 @@ def __init__( :param error: For events submitted as a result of running CLI commands that exit with a non-0 status code, or events submitted as a result of API server requests that respond with a non-2xx status code, the error type, without specific error details. :param extra_data: Any additional key/value pairs that should be associated with this event. :param flags: For events submitted as a result of running CLI commands, the flags in use when the command was submitted. Omits flag values (when they exist) by persisting only the portion of each string in this list that come before `=` or `space` characters. - :param local_host: For events submitted as a result of making API server requests, `True` if the API server is running on the user's local host. Default: `False`. + :param local_host: For events submitted as a result of making API server requests, `True` if the API server is running on the user's local host, otherwise `False`. Default: `None` (acceptable only when `endpoint` is also `None`). :param resource_counts: Should contain the counts of dataset, policy, and system manifests in use when this event was submitted. Include all three keys, even if one or more of their values are `0`. Ex: `{ "datasets": 7, "policies": 26, "systems": 9 }`. :param status_code: For events submitted as a result of making API server requests, the HTTP status code included in the response. """ @@ -81,8 +81,11 @@ def __init__( self.resource_counts = resource_counts + self.local_host = local_host self.endpoint = None if endpoint is not None: + assert self.local_host is not None, "local_host must be provided" + endpoint_components = endpoint.split(":", maxsplit=1) assert ( len(endpoint_components) == 2 @@ -105,7 +108,6 @@ def __init__( self.error = error self.extra_data = extra_data or {} self.flags = flags - self.local_host = local_host self.status_code = status_code if self.command is not None or self.endpoint is not None: