Skip to main content

Rest util and common tools

Project description

Rest tool

A curated list of awesome things related to microservices and computer vision.

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))

response = loads(dumps(result, cls=Encoder))

Author

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

restutil-1.0.13-py3-none-any.whl (13.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page