Skip to content

Commit

Permalink
Add BaseFacade static class to allow access to API from anywhere (#15)
Browse files Browse the repository at this point in the history
* Add Facade to allow for access to API and options

* Set version to 0.8.0
  • Loading branch information
dkarchmer authored Aug 20, 2023
1 parent 8b2af31 commit 519b2f5
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### v0.9.0 (2023-08-20)

* Add isort and Black as formatter
* Add static BaseFacade class to allow access to API class and BaseMain options

### v0.8.0 (2023-07-04)

* Remove support for Python 3.8.
Expand Down
25 changes: 25 additions & 0 deletions drf_client/helpers/base_facade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Hold static information that can be accessed by any part of the package.
A facade is an object that serves as a front-facing interface masking more complex
underlying or structural code.
"""
from argparse import Namespace

from drf_client.connection import Api as RestApi


class BaseFacade:
"""Stores key static information used across the package."""

api: RestApi or None = None
api_options: dict or None = None
cmd_args: Namespace or None = None

@staticmethod
def initialize_api(api_options: dict, cmd_args: Namespace = None):
"""Initialize API with the given options."""
if BaseFacade.api is None:
# Only initialize ones
BaseFacade.api_options = api_options.copy()
BaseFacade.api = RestApi(api_options)
BaseFacade.cmd_args = cmd_args
59 changes: 34 additions & 25 deletions drf_client/helpers/base_main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import sys
import logging
import argparse
import getpass

import logging
import sys
from urllib.parse import urlparse

from drf_client.connection import Api as RestApi, DEFAULT_HEADERS
from drf_client.exceptions import HttpClientError
from .facade import Facade

LOG = logging.getLogger(__name__)

Expand All @@ -16,14 +14,14 @@ class BaseMain:
args = None
api = None
options = {
'DOMAIN': None,
'API_PREFIX': 'api/v1',
'TOKEN_TYPE': 'jwt',
'TOKEN_FORMAT': 'JWT {token}',
'USERNAME_KEY': 'username',
'LOGIN': 'auth/login/',
'LOGOUT': 'auth/logout/',
'USE_DASHES': False,
"DOMAIN": None,
"API_PREFIX": "api/v1",
"TOKEN_TYPE": "jwt",
"TOKEN_FORMAT": "JWT {token}",
"USERNAME_KEY": "username",
"LOGIN": "auth/login/",
"LOGOUT": "auth/logout/",
"USE_DASHES": False,
}
logging_level = logging.INFO

Expand All @@ -38,12 +36,19 @@ def __init__(self):
"""
self.parser = argparse.ArgumentParser(description=__doc__)
self.parser.add_argument(
'-u', '--user', dest='username', type=str, required=True,
help='Username used for login'
"-u",
"--user",
dest="username",
type=str,
required=True,
help="Username used for login",
)
self.parser.add_argument(
'--server', dest='server', type=str, required=True,
help='Server Domain Name to use'
"--server",
dest="server",
type=str,
required=True,
help="Server Domain Name to use",
)

self.add_extra_args()
Expand All @@ -64,7 +69,9 @@ def main(self):
4. Call after_loging to do actual work with server data
"""
self.domain = self.get_domain()
self.api = RestApi(self.get_options())
# Create a static pointer to the API for global access
Facade.initialize_api(options=self.get_options(), args=self.args)
self.api = Facade.api
self.before_login()
ok = self.login()
if ok:
Expand All @@ -75,17 +82,19 @@ def main(self):

def get_options(self):
options = self.options
options['DOMAIN'] = self.domain
options["DOMAIN"] = self.domain
return options

def config_logging(self):
"""
Overwrite to change the way the logging package is configured
:return: Nothing
"""
logging.basicConfig(level=self.logging_level,
format='[%(asctime)-15s] %(levelname)-6s %(message)s',
datefmt='%d/%b/%Y %H:%M:%S')
logging.basicConfig(
level=self.logging_level,
format="[%(asctime)-15s] %(levelname)-6s %(message)s",
datefmt="%d/%b/%Y %H:%M:%S",
)

def add_extra_args(self):
"""
Expand All @@ -99,7 +108,7 @@ def get_domain(self) -> str:
Figure out server domain URL based on --server and --customer args
"""
if not urlparse(self.args.server).scheme:
return f'https://{self.args.server}'
return f"https://{self.args.server}"
return self.args.server

def login(self) -> bool:
Expand All @@ -109,7 +118,7 @@ def login(self) -> bool:
password = getpass.getpass()
ok = self.api.login(username=self.args.username, password=password)
if ok:
LOG.info('Welcome {0}'.format(self.args.username))
LOG.info("Welcome {0}".format(self.args.username))
return ok

def before_login(self):
Expand All @@ -125,4 +134,4 @@ def after_login(self):
This function MUST be overwritten to do actual work after logging into the Server
:return: Nothing
"""
LOG.warning('No actual work done')
LOG.warning("No actual work done")
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pathlib

from setuptools import find_packages, setup
from setuptools import setup

# The directory containing this file
HERE = pathlib.Path(__file__).parent
Expand All @@ -10,7 +10,7 @@

setup(
name="django-rest-framework-client",
version="0.8.0",
version="0.9.0",
description="Python client for a DjangoRestFramework based web site",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
15 changes: 15 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""test Resource class."""
import argparse
import unittest

from drf_client.helpers.base_facade import BaseFacade


class FacadeTestCase(unittest.TestCase):
"""Test static facade class."""

def test_initialize_facade(self):
"""Test Initializer."""
BaseFacade.initialize_api({"DOMAIN": "https://example.com"})
assert BaseFacade.api_options["DOMAIN"] == "https://example.com"
assert BaseFacade.api is not None
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = py39, py310
envlist = py310, py311

[testenv]
deps =
Expand Down

0 comments on commit 519b2f5

Please sign in to comment.