Skip to content

Commit

Permalink
Merge pull request #12 from candango/develop
Browse files Browse the repository at this point in the history
build: merge develop to master
  • Loading branch information
piraz authored Apr 11, 2024
2 parents 20a453d + 2f5264a commit 6279b79
Show file tree
Hide file tree
Showing 19 changed files with 918 additions and 280 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ include LICENSE
include requirements/all.txt
include requirements/basic.txt
include requirements/tornado.txt
include requirements/requests.txt
2 changes: 1 addition & 1 deletion peasant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

__author__ = "Flavio Garcia <[email protected]>"
__version__ = (0, 6, 1)
__version__ = (0, 7, 0)
__licence__ = "Apache License V2.0"


Expand Down
208 changes: 0 additions & 208 deletions peasant/client.py

This file was deleted.

Empty file added peasant/client/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions peasant/client/protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2020-2024 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from peasant.client.transport import Transport
import logging

logger = logging.getLogger(__name__)


class Peasant(object):

_transport: Transport

def __init__(self, transport):
self._directory_cache = None
self._transport = transport
self._transport.peasant = self

@property
def directory_cache(self):
return self._directory_cache

@directory_cache.setter
def directory_cache(self, directory_cache):
self._directory_cache = directory_cache

@property
def transport(self):
return self._transport

def directory(self):
if self.directory_cache is None:
self.transport.set_directory()
return self.directory_cache

def new_nonce(self):
return self.transport.new_nonce()


class AsyncPeasant(Peasant):

def __init__(self, transport):
super(AsyncPeasant, self).__init__(transport)

async def directory(self):
if self._directory_cache is None:
future = self.transport.set_directory()
if future is not None:
logger.debug("Running transport set directory cache "
"asynchronously.")
await future
return self._directory_cache
108 changes: 108 additions & 0 deletions peasant/client/transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Copyright 2020-2024 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import logging
import typing
from urllib.parse import urlencode, urlparse

if typing.TYPE_CHECKING:
from peasant.client.protocol import Peasant

logger = logging.getLogger(__name__)


def concat_url(url: str, path: str = None, **kwargs: dict) -> str:
""" Concatenate a given url to a path, and query string if informed.
:param str url: Base url
:param str path: Path to be added to the returned url
:param dict kwargs:
:key path: Path to be added to the returned url
:key query_string: Query string to be added to the returned url
"""
query_string = kwargs.get("query_string", None)
if query_string:
if isinstance(query_string, dict):
query_string = urlencode(query_string)
if not isinstance(query_string, str):
err = (f"'query_string' parameter should be dict, or string. "
f"Not {type(query_string)}")
raise TypeError(err)
path = f"{path}?{query_string}"
if path is not None and path != "/":
if path.startswith("/"):
path = path[1:]
url = f"{url}/{path}"
return url


def fix_address(address):
parsed_address = urlparse(address)
if parsed_address.path.endswith("/"):
parsed_address = parsed_address._replace(path=parsed_address.path[:-1])
return parsed_address.geturl()


class Transport:

_peasant: Peasant

@property
def peasant(self) -> Peasant:
return self._peasant

@peasant.setter
def peasant(self, peasant: Peasant):
self._peasant = peasant

def get_url(self, path: str, **kwargs: dict):
if (path.lower().startswith("http://") or
path.lower().startswith("https://")):
return concat_url(path, "", **kwargs)
return concat_url(self._bastion_address, path, **kwargs)

def delete(self, path: str, **kwargs: dict):
raise NotImplementedError

def get(self, path: str, **kwargs: dict):
raise NotImplementedError

def head(self, path: str, **kwargs: dict):
raise NotImplementedError

def options(self, path: str, **kwargs: dict):
raise NotImplementedError

def patch(self, path: str, **kwargs: dict):
raise NotImplementedError

def post(self, path: str, **kwargs: dict):
raise NotImplementedError

def post_as_get(self, path: str, **kwargs: dict):
raise NotImplementedError

def put(self, path: str, **kwargs: dict):
raise NotImplementedError

def set_directory(self):
raise NotImplementedError

def new_nonce(self):
raise NotImplementedError

def is_registered(self):
raise NotImplementedError
Loading

0 comments on commit 6279b79

Please sign in to comment.