forked from fabieu/sureflap-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various adjustments to improve performance and maintainability
- Changed validation of config.ini entries - Added logging module - Updated documentation (README.md) - Updated dependencies and migrated to pipenv - Implemented production webserver (cherrypy) - Code formatting
- Loading branch information
Showing
17 changed files
with
598 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# Custom | ||
config.ini | ||
*.token | ||
.vscode/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
flask = "*" | ||
requests = "*" | ||
cachetools = "*" | ||
jinja2 = "*" | ||
pyyaml = "*" | ||
python-dateutil = "*" | ||
flask-cors = "*" | ||
cherrypy = "*" | ||
|
||
[dev-packages] | ||
autopep8 = "*" | ||
|
||
[requires] | ||
python_version = "3" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[api] | ||
endpoint = "https://app.api.surehub.io" | ||
port = 2311 | ||
|
||
[user] | ||
email = "SureFlapEmail" | ||
password = "SureFlapPassword" | ||
email = "SureFlap E-Mail" | ||
password = "SureFlap Password" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: 1 | ||
formatters: | ||
simple: | ||
datefmt: '%Y-%m-%d %H:%M:%S' | ||
format: '%(asctime)s %(levelname)s %(message)s' | ||
handlers: | ||
console: | ||
class: logging.StreamHandler | ||
level: DEBUG | ||
formatter: simple | ||
stream: ext://sys.stdout | ||
loggers: | ||
sureflap: | ||
level: INFO | ||
handlers: [console] | ||
propagate: no | ||
root: | ||
level: DEBUG | ||
handlers: [console] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,80 @@ | ||
import configparser | ||
from configparser import ConfigParser | ||
import os | ||
import validators | ||
|
||
try: | ||
config = configparser.ConfigParser() | ||
config.read(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'config.ini'))) | ||
|
||
endpoint = config['api']['endpoint'].replace('"', '') | ||
email = config['user']['email'].replace('"', '') | ||
password = config['user']['password'].replace('"', '') | ||
except: | ||
print('No valid config.ini found! Please make sure you rename "config.ini.sample" to "config.ini" and edit the settings correctly.') | ||
os._exit(0) | ||
|
||
|
||
def validate(): | ||
if email != "SureFlapEmail" and password != "SureFlapPassword": | ||
if validators.url(endpoint): | ||
return True | ||
else: | ||
print("Invalid endpoint provided. Please check the syntax!") | ||
return False | ||
else: | ||
print("Please edit the config.ini first!") | ||
return False | ||
|
||
|
||
import logging | ||
import logging.config | ||
from requests.models import ProtocolError | ||
import yaml | ||
|
||
# Initialize logging | ||
with open('logging.conf', 'r') as f: | ||
config = yaml.safe_load(f.read()) | ||
logging.config.dictConfig(config) | ||
|
||
logger = logging.getLogger("sureflap") | ||
|
||
|
||
# Global configuration variables | ||
ENDPOINT = "https://app.api.surehub.io" | ||
|
||
|
||
class InvalidConfig(Exception): | ||
pass | ||
|
||
|
||
class ConfigValidator(ConfigParser): | ||
def __init__(self, config_file): | ||
super(ConfigValidator, self).__init__() | ||
|
||
self.config_file = config_file | ||
open(config_file) | ||
self.read(config_file) | ||
self.validate_config() | ||
|
||
def validate_config(self): | ||
required_values = { | ||
'api': { | ||
'port': None, | ||
}, | ||
'user': { | ||
'email': None, | ||
'password': None, | ||
}, | ||
} | ||
|
||
for section, keys in required_values.items(): | ||
if section not in self: | ||
raise InvalidConfig( | ||
f'{self.__class__.__name__}: Missing section "{section}" in {self.config_file}') | ||
|
||
for key, values in keys.items(): | ||
if key not in self[section] or self[section][key] in ('', 'YOUR_PERSONAL_ACCESS_TOKEN'): | ||
raise InvalidConfig( | ||
f'{self.__class__.__name__}: Missing value for "{key}" in section "{section}" in {self.config_file}') | ||
|
||
if values: | ||
if self[section][key] not in values: | ||
allowed_values = f"[{(', '.join(map(str, values)))}]" | ||
raise InvalidConfig( | ||
f'{self.__class__.__name__}: Invalid value for "{key}" under section "{section}" in {self.config_file} - allowed values are {allowed_values}') | ||
|
||
|
||
def init_config(): | ||
try: | ||
config = ConfigValidator(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'config.ini'))) | ||
|
||
global EMAIL, PASSWORD, PORT | ||
PORT = config['api']['port'] | ||
EMAIL = config['user']['email'].replace('"', '') | ||
PASSWORD = config['user']['password'].replace('"', '') | ||
return True | ||
|
||
except FileNotFoundError: | ||
logger.error( | ||
'No config.ini found! Please make sure you rename "config.ini.sample" to "config.ini" and edit the settings correctly.') | ||
|
||
except InvalidConfig as e: | ||
logger.error(e) | ||
raise SystemExit(2) | ||
|
||
except: | ||
logger.error("Configuration was not successfull!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.