From e24cac244fb1f3d25f5d05d6a695109fe2fab98a Mon Sep 17 00:00:00 2001 From: Benjamin Bengfort Date: Wed, 14 Aug 2024 17:55:46 -0500 Subject: [PATCH] Pretty Print and Localhost Insecure --- .gitignore | 1 + envoy/client.py | 27 +++++++++++++++++++++++++-- envoy/records.py | 8 ++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 82f9275..705ae2a 100644 --- a/.gitignore +++ b/.gitignore @@ -129,6 +129,7 @@ venv/ ENV/ env.bak/ venv.bak/ +.secret # Spyder project settings .spyderproject diff --git a/envoy/client.py b/envoy/client.py index 95037b1..4165617 100644 --- a/envoy/client.py +++ b/envoy/client.py @@ -100,6 +100,7 @@ def __init__( self._creds = None self._host = parse_url_host(url) + self._prefix = None user_agent = f"pyenvoy/{get_version(short=True)} python/{python_version()}" @@ -119,7 +120,7 @@ def __init__( pool_maxsize=pool_maxsize, max_retries=max_retries, ) - self.session.mount("https://", self.adapter) + self.session.mount(self.prefix+"://", self.adapter) # Configure REST resources on the client self.accounts = Accounts(self) @@ -140,6 +141,19 @@ def timeout(self, value): else: self._timeout = value + @property + def prefix(self): + if self._prefix is None: + if not self._host: + raise ValueError("cannot compute prefix without host") + + if self.is_localhost(): + self._prefix = "http" + else: + self._prefix = "https" + + return self._prefix + def status(self): return self.get("status", require_authentication=False) @@ -272,7 +286,7 @@ def _make_endpoint(self, *endpoint: tuple[str], params: dict = None) -> str: return urlunparse( URL( - scheme="https", + scheme=self.prefix, netloc=self._host, path=path, params="", @@ -329,6 +343,15 @@ def is_refreshable(self) -> bool: """ return self._creds is not None and self._creds.is_refreshable() + def is_localhost(self) -> bool: + """ + Returns true if the host is a local domain (e.g. localhost) + """ + host = self._host + if ":" in host: + host = host.split(":")[0] + return host == "localhost" or host.endswith(".local") + def parse_url_host(urlstr: str) -> str: parts = urlparse(urlstr, scheme="https", allow_fragments=False) diff --git a/envoy/records.py b/envoy/records.py index b20362b..f1ef875 100644 --- a/envoy/records.py +++ b/envoy/records.py @@ -2,6 +2,8 @@ Records provide immutable access to data returned by the Envoy server. """ +import json + from collections.abc import Mapping, Sequence @@ -100,6 +102,9 @@ def items(self): def asdict(self): return self.data.copy() + def pprint(self): + print(json.dumps(self.data, indent=2)) + class RecordList(Sequence): """ @@ -155,6 +160,9 @@ def __copy__(self): def copy(self): return self.__class__(self) + def pprint(self): + print(json.dumps(self.data, indent=2)) + class PaginatedRecords(RecordList):