Metadata-Version: 2.1
Name: mrkutil
Version: 2.0.1
Summary: Python package containing common functions for python service based architecture.
Author-email: Nebojsa Mrkic <mrkic.nebojsa@gmail.com>
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Classifier: License :: OSI Approved :: Apache Software License
Requires-Dist: RabbitMQPubSub>=1.3.0
Requires-Dist: redis>=4.3 ; extra == "extras"
Requires-Dist: sqlalchemy>=1.4 ; extra == "extras"
Requires-Dist: pytest ; extra == "tests"
Requires-Dist: mock ; extra == "tests"
Requires-Dist: requests ; extra == "tests"
Requires-Dist: pytest-runner ; extra == "tests"
Requires-Dist: pre-commit ; extra == "tests"
Requires-Dist: pytest-mock ; extra == "tests"
Requires-Dist: coverage ; extra == "tests"
Requires-Dist: coverage-badge ; extra == "tests"
Project-URL: Home, https://github.com/ivke-99/mrkutil
Provides-Extra: extras
Provides-Extra: tests

## mrkutil

This is a Python package containing common functions for Python service-based architecture.

### Base Handler

BaseHandler is an abstract class used for implementing function/class level factory pattern.

```python
data = ["method"] = "my_defined_method"
handler = BaseHandler.process_data(data)
```

Where my_defined_method is the name of the child handler name static method.

### Base Redis

Simple class with utility functions for working with redis.
Key is the prefix for all redis objects, and timeout is how long redis keeps the object in cache.
Example usage:

```python
cache = RedisBase(key="key", timeout=300)
res = cache.get("this_key")
```

### Communication

Communication package is a wrapper arround RabbitMQPubSub library that provides a simple interface for publishing and subscribing to messages.

call_service is a function that can be used to call a service with a message and await its response.

```python
call_service(request_data=data, destination="some_exchange", source="self_exchange")
```

listen is a function that can be used to listen to a queue and call a function when a message is received.
Commonly implemented in microservices in main.py as a long living process.

```python
listen(exchange="some_exchange", exchange_type="direct", queue="some_queue")
```

trigger_service triggers a service with a message but does not wait for a response. Useful for fire and forget scenarios.

```python
trigger_service(request_data=data, destination="some_exchange", source="self_exchange")
```

### Logging Configuration

The `get_logging_config` function in `mrkutil/logging/logging_config.py` generates a logging configuration dictionary based on the provided parameters. It supports both JSON and default formatters.

```python
config = get_logging_config(log_level="DEBUG", json_format=False, api=True)
```

### Pagination

The `paginate` function in mongoengine.py and sqlalchemy.py is a utility function that paginates a list of items based on the provided page and page size.
It is useful in microservices where there is no accessible web framework pagination method, and to avoid code duplication.

There is also a dependency.py which contains pagination_params function that are usually used in fastapi dependency injection.

```python
paginate(items, page_number=1, page_size=10, direction="asc", sort_by="id")
```

### Responses

ServiceResponse class is a utility class for creating a standard response object that can be used in all services.
It is useful for standardizing the response format across all services.

```python
ServiceResponse(code=400, message="Validation Error", errors=[{"field": "name", "message": "Name is required"}])
```

## Authors

- [@nmrkic](https://github.com/nmrkic)
- [@ivke-99](https://github.com/ivke-99)

## Deployment to PyPI

To deploy this package to PyPI, use the following commands:

```bash
flit build
flit publish
```

## Contributing

Contributions are always welcome! If you have any suggestions or improvements, feel free to submit a pull request or open an issue.

