A package for handling responses with potential errors and generic data.
Project description
response_handler
A package for handling responses with potential errors and generic data.
Features
- Custom
Responseclass for handling responses with success status, errors, and data. - Custom
Errorclass for defining error codes and messages. - Custom
ResponseErrorexception for handling response-related errors. - Utilities for adding errors, checking success status, and retrieving error messages and codes.
Installation
You can install response_handler via pip:
pip install response_handler
Usage
Importing the Package
To use the Response class and related utilities, import them from the response_handler package:
from response_handler import Response, Error, ResponseError
Creating a Successful Response
Create a Response object with success set to True and provide any data:
response = Response(success=True, data="Some data")
if response.is_successful:
print("Response is successful")
print("Data:", response.data)
Creating a Failed Response
Create a Response object with success set to False:
response = Response(success=False)
if not response.is_successful:
print("Response failed")
Adding Errors to a Response
Add errors to a response using the add_error method:
response = Response(success=False)
response.add_error(404, "Not Found")
response.add_error(500, "Internal Server Error")
if response.has_errors:
print("Errors:")
for code, message in zip(response.error_codes, response.error_messages):
print(f"Error {code}: {message}")
Handling Missing Success Field
If the success field is not set, accessing is_successful raises a ResponseError:
response = Response()
try:
print(response.is_successful)
except ResponseError as e:
print("Caught an error:", str(e))
API Reference
Classes
Response
A generic class for handling responses.
Attributes:
success(Optional[bool]): Indicates whether the response is successful.errors(Optional[List[Error]]): A list of errors in the response.data(Optional[T]): The data in the response.
Methods:
is_successful(bool): Checks if the response is successful. RaisesResponseErrorifsuccessis not set.add_error(code: int, message: str): Adds an error to the response.has_errors(bool): Checks if the response has errors.error_messages(List[str]): Retrieves a list of error messages.error_codes(List[int]): Retrieves a list of error codes.
Error
A class for defining error codes and messages.
Attributes:
code(int): The error code.message(str): The error message.
ResponseError
A custom exception for response errors.
Attributes:
message(str): The error message.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
Author: Angel Kürten
Email: angel@angelkurten.com
GitHub: angelkurten
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file response_handler_lib-0.1.1.tar.gz.
File metadata
- Download URL: response_handler_lib-0.1.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c1164ec5afb4ab4db2868afd989ad2648782960e8facd1468eac01d1105c029
|
|
| MD5 |
1937da928742a5f25a690ee0f4382706
|
|
| BLAKE2b-256 |
df593c51cf54ed9a1e7b1de97cd093dd3840dad7248ddea302fd7176bc06e11e
|
File details
Details for the file response_handler_lib-0.1.1-py3-none-any.whl.
File metadata
- Download URL: response_handler_lib-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebb7adf56849800f21e28f3e3c9de31a079d376702e0ce4fcb902dc775a02ff1
|
|
| MD5 |
cd2fcd24331eb8cbaada648fdcdf8f6a
|
|
| BLAKE2b-256 |
34774a689ab02a5589c077fab225187e227db1db898ef79f3cba03a4a703caca
|