Skip to content

Commit

Permalink
refactor(client): break client in modules
Browse files Browse the repository at this point in the history
Fixes: #8
  • Loading branch information
piraz committed Mar 29, 2024
1 parent bc4d1a8 commit db8654b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 83 deletions.
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
83 changes: 2 additions & 81 deletions peasant/client.py → peasant/client/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,90 +15,11 @@
import copy
import logging
from peasant import get_version
from peasant.client.transport import Transport
from urllib.parse import urlencode

logger = logging.getLogger(__name__)


class PeasantTransport:

_peasant: "Peasant"

@property
def peasant(self):
return self._peasant

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

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

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

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

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

def set_directory(self):
raise NotImplementedError

def new_nonce(self):
raise NotImplementedError

def is_registered(self):
raise NotImplementedError


class Peasant(object):

_transport: PeasantTransport

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


tornado_installed = False
try:
from tornado.httpclient import HTTPRequest
Expand Down Expand Up @@ -139,7 +60,7 @@ def get_tornado_request(url, **kwargs):
pass


class TornadoTransport(PeasantTransport):
class TornadoTransport(Transport):

def __init__(self, bastion_address):
super().__init__()
Expand Down
57 changes: 57 additions & 0 deletions peasant/client/transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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

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

logger = logging.getLogger(__name__)


class Transport:

_peasant: Peasant

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

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

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

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

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

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

def set_directory(self):
raise NotImplementedError

def new_nonce(self):
raise NotImplementedError

def is_registered(self):
raise NotImplementedError
1 change: 0 additions & 1 deletion tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# print current directory
import unittest
from tests import tornado_test

Expand Down
2 changes: 1 addition & 1 deletion tests/tornado_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from firenado.testing import TornadoAsyncTestCase
from firenado.launcher import ProcessLauncher
from peasant.client import TornadoTransport
from peasant.client.tornado import TornadoTransport
from tests import chdir_fixture_app, PROJECT_ROOT
from tornado.testing import gen_test

Expand Down

0 comments on commit db8654b

Please sign in to comment.