# 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.
```bash
pip install restutil
```
## Usage
- Decorest
```python
from restutil.decorators import Decorest
# create decorest object
deco = Decorest()
# wrap foo function
@deco.try_log
def foo():
return None
```
- ResData, ResOperationType
```python
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
```python
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
```python
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
```python
from restutil.encoder import make_response
from restutil.result import ResData
result: ResData = ResData()
make_response(result)
```
- Encoder
```python
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
- Adonis Gonzalez Godoy - [LinkedIn](https://www.linkedin.com/in/adonis-gonzalez) - [E-mail](adions025@gmail.com)
Raw data
{
"_id": null,
"home_page": "https://github.com/adions025/restutil",
"name": "restutil",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "Restfull, Rest, Util, Tools, Logger",
"author": "Adonis Gonzalez Godoy",
"author_email": "adions025@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/4d/63/979df3b798aefe17c5af0d85d615189330ac49f811d7a927ae45fa879334/restutil-1.0.15.tar.gz",
"platform": null,
"description": "# Rest tool\r\n> A curated list of awesome util functions.\r\n\r\nThis module contains interesting features for endpoint creation. It also includes some decorators.\r\n\r\n## Details\r\n\r\n- 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\r\n\r\n## Installation\r\n\r\nUse the package manager pip to install restutil.\r\n\r\n```bash\r\npip install restutil\r\n```\r\n\r\n## Usage\r\n\r\n - Decorest\r\n```python\r\nfrom restutil.decorators import Decorest\r\n\r\n# create decorest object\r\ndeco = Decorest()\r\n\r\n# wrap foo function\r\n\r\n@deco.try_log\r\ndef foo():\r\n return None\r\n```\r\n\r\n - ResData, ResOperationType\r\n```python\r\nfrom restutil.result import ResData\r\nfrom restutil.operations import ResOperationType as resType\r\n\r\n# create ResDaata object\r\nresult: ResData = ResData()\r\n\r\n# add data in its content\r\nresult.content = \"data\"\r\n\r\n# add flagged results\r\nresult.add_result(message=str(f'Error occurred'), res_type=resType.ERROR)\r\n\r\n# check flags\r\nbool(result.has_errors)\r\n```\r\n - Logger\r\n\r\n```python\r\nfrom restutil.logger import Log\r\nfrom restutil.logger import LoggerMinimumLevel as level\r\nimport os\r\n\r\n# Setting to file\r\nlocal_path = '%s/%s' % (os.path.dirname(__file__), 'Log')\r\nlg = Log().setting_to_file(logger_path=local_path, logger_file_name='Test', minimum_level=level.DEBUG)\r\nlg.info('Test Dummy')\r\n\r\n# Setting to logstash\r\nimport sys\r\n\r\nhostLogstash = '127.0.0.1'\r\nportLogstash = 5959\r\nlg = Log().settingToLogstash(host=hostLogstash, port=portLogstash, minimunLevel=level.DEBUG)\r\nextraData = {\r\n 'Field1': 'python version: ' + repr(sys.version_info),\r\n 'FieldCustom2': True,\r\n 'FieldCustom3': {'a': 1, 'b': 'c'}\r\n}\r\nlg.info(msg='Test Dummy', extra=extraData)\r\n\r\n# Do Extra Logger\r\n\"\"\"\r\nThis method was implemented for provide a easy system for create a extra logger information. \r\nWhen we declare logger with logstash configuration, we can use this method.\r\n\"\"\"\r\n\r\n\r\n\r\nfrom restutil.common import Common as cm\r\n\r\ncm().do_extra_logger(app_name='App Dummy', method_name='Method Dummy', class_name='Class Dummy',\r\n inherited_from='Principal App Name')\r\n\r\n# If you need add more information, you can look this other example:\r\n\r\ncm().do_extra_logger(app_name='App Dummy', method_name='Method Dummy', class_name='Class Dummy',\r\n inherited_from='Principal App Name', kwargs={'Result': 'Result Value Dummy'})\r\n\r\n# Remember, For use this configuration you need have correctly configured logstash.\r\n# logstash.conf\r\n# input {\r\n# udp {\r\n# port => <listenPort. It's same that you send in request parameters>\r\n# codec => json\r\n# }\r\n# }\r\n```\r\n\r\n - DictReader\r\n```python\r\nfrom restutil.dictionary import DictReader\r\nimport json\r\n\r\npath = \"path/to/json/file.json\"\r\nwith open(path, encoding='utf-8') as json_file:\r\n config_file = json.load(json_file)\r\n \r\n# create DictReader object\r\nconfig_dict = DictReader(config_file=config_file)\r\n```\r\n\r\n - make_response\r\n```python\r\nfrom restutil.encoder import make_response\r\nfrom restutil.result import ResData\r\n\r\nresult: ResData = ResData()\r\nmake_response(result)\r\n```\r\n\r\n - Encoder\r\n```python\r\nfrom restutil.encoder import Encoder\r\nfrom restutil.result import ResData\r\nimport json\r\n\r\nresult: ResData = ResData()\r\nresult.content = \"test dummy\"\r\nprint(json.dumps(result, cls=Encoder))\r\n```\r\n\r\n## Author\r\n\r\n- Adonis Gonzalez Godoy - [LinkedIn](https://www.linkedin.com/in/adonis-gonzalez) - [E-mail](adions025@gmail.com)\r\n",
"bugtrack_url": null,
"license": null,
"summary": "Rest util and common tools",
"version": "1.0.15",
"project_urls": {
"Download": "https://github.com/adions025/restutil",
"Homepage": "https://github.com/adions025/restutil"
},
"split_keywords": [
"restfull",
" rest",
" util",
" tools",
" logger"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "300822400d758a0d9ca703a47d10b261a5d8c2c2860d5f3c792f48334704b810",
"md5": "dd060b051f30c5d68b58a49987613f78",
"sha256": "fc03ddc40404b64c919b3f148ab7de59d283056b420ba1a85b48cb3a52669f1e"
},
"downloads": -1,
"filename": "restutil-1.0.15-py3-none-any.whl",
"has_sig": false,
"md5_digest": "dd060b051f30c5d68b58a49987613f78",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 13908,
"upload_time": "2024-09-27T14:05:58",
"upload_time_iso_8601": "2024-09-27T14:05:58.351254Z",
"url": "https://files.pythonhosted.org/packages/30/08/22400d758a0d9ca703a47d10b261a5d8c2c2860d5f3c792f48334704b810/restutil-1.0.15-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4d63979df3b798aefe17c5af0d85d615189330ac49f811d7a927ae45fa879334",
"md5": "cf1f12d3634fc7370f87cfd41ca76961",
"sha256": "d94218c64a7c28b33beecda81d0651dfe83b2ff4510df95c1dce911047f45792"
},
"downloads": -1,
"filename": "restutil-1.0.15.tar.gz",
"has_sig": false,
"md5_digest": "cf1f12d3634fc7370f87cfd41ca76961",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12282,
"upload_time": "2024-09-27T14:05:59",
"upload_time_iso_8601": "2024-09-27T14:05:59.405588Z",
"url": "https://files.pythonhosted.org/packages/4d/63/979df3b798aefe17c5af0d85d615189330ac49f811d7a927ae45fa879334/restutil-1.0.15.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-27 14:05:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "adions025",
"github_project": "restutil",
"github_not_found": true,
"lcname": "restutil"
}