Rest util and common tools
Project description
Rest tool
A curated list of awesome util functions.
This module contains interesting features for endpoint creation. It also includes some decorators.
Details
- Decorest - class decorator that allows implementing a general try_log function in the entire function, but it is necessary to use the ResData data model
Installation
Use the package manager pip to install restutil.
pip install restutil
Usage
- Decorest
from restutil.decorators import Decorest
# create decorest object
deco = Decorest()
# wrap foo function
@deco.try_log
def foo():
return None
- ResData, ResOperationType
from restutil.result import ResData
from restutil.operations import ResOperationType as resType
# create ResDaata object
result: ResData = ResData()
# add data in its content
result.content = "data"
# add flagged results
result.add_result(message=str(f'Error occurred'), res_type=resType.ERROR)
# check flags
bool(result.has_errors)
- Logger
from restutil.logger import Log
from restutil.logger import LoggerMinimumLevel as level
import os
# Setting to file
local_path = '%s/%s' % (os.path.dirname(__file__), 'Log')
lg = Log().setting_to_file(logger_path=local_path, logger_file_name='Test', minimum_level=level.DEBUG)
lg.info('Test Dummy')
# Setting to logstash
import sys
hostLogstash = '127.0.0.1'
portLogstash = 5959
lg = Log().settingToLogstash(host=hostLogstash, port=portLogstash, minimunLevel=level.DEBUG)
extraData = {
'Field1': 'python version: ' + repr(sys.version_info),
'FieldCustom2': True,
'FieldCustom3': {'a': 1, 'b': 'c'}
}
lg.info(msg='Test Dummy', extra=extraData)
# Do Extra Logger
"""
This method was implemented for provide a easy system for create a extra logger information.
When we declare logger with logstash configuration, we can use this method.
"""
from restutil.common import Common as cm
cm().do_extra_logger(app_name='App Dummy', method_name='Method Dummy', class_name='Class Dummy',
inherited_from='Principal App Name')
# If you need add more information, you can look this other example:
cm().do_extra_logger(app_name='App Dummy', method_name='Method Dummy', class_name='Class Dummy',
inherited_from='Principal App Name', kwargs={'Result': 'Result Value Dummy'})
# Remember, For use this configuration you need have correctly configured logstash.
# logstash.conf
# input {
# udp {
# port => <listenPort. It's same that you send in request parameters>
# codec => json
# }
# }
- DictReader
from restutil.dictionary import DictReader
import json
path = "path/to/json/file.json"
with open(path, encoding='utf-8') as json_file:
config_file = json.load(json_file)
# create DictReader object
config_dict = DictReader(config_file=config_file)
- make_response
from restutil.encoder import make_response
from restutil.result import ResData
result: ResData = ResData()
make_response(result)
- Encoder
from restutil.encoder import Encoder
from restutil.result import ResData
import json
result: ResData = ResData()
result.content = "test dummy"
print(json.dumps(result, cls=Encoder))
Author
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
restutil-1.0.15.tar.gz
(12.3 kB
view details)
Built Distribution
restutil-1.0.15-py3-none-any.whl
(13.9 kB
view details)
File details
Details for the file restutil-1.0.15.tar.gz
.
File metadata
- Download URL: restutil-1.0.15.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d94218c64a7c28b33beecda81d0651dfe83b2ff4510df95c1dce911047f45792 |
|
MD5 | cf1f12d3634fc7370f87cfd41ca76961 |
|
BLAKE2b-256 | 4d63979df3b798aefe17c5af0d85d615189330ac49f811d7a927ae45fa879334 |
File details
Details for the file restutil-1.0.15-py3-none-any.whl
.
File metadata
- Download URL: restutil-1.0.15-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc03ddc40404b64c919b3f148ab7de59d283056b420ba1a85b48cb3a52669f1e |
|
MD5 | dd060b051f30c5d68b58a49987613f78 |
|
BLAKE2b-256 | 300822400d758a0d9ca703a47d10b261a5d8c2c2860d5f3c792f48334704b810 |