Skip to content

Commit

Permalink
Pretty Print and Localhost Insecure
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Aug 14, 2024
1 parent e8c98c2 commit e24cac2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ venv/
ENV/
env.bak/
venv.bak/
.secret

# Spyder project settings
.spyderproject
Expand Down
27 changes: 25 additions & 2 deletions envoy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"

Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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="",
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions envoy/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Records provide immutable access to data returned by the Envoy server.
"""

import json

from collections.abc import Mapping, Sequence


Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit e24cac2

Please sign in to comment.