nonoservice 0.0.3
A set of classes to build nonoservices with a Redis Stream commuication bus
Requires Python: >=3.9

# Nonoservice Overview

This project establishes a comprehensive framework of classes and functions designed to represent and manage diverse payloads, headers, and telegrams systematically. The primary objective is to streamline the creation, manipulation, and validation of these objects, ensuring strict adherence to predefined formats and constraints.

Additionally, the project incorporates the Nonoservice architecture, a microservices-based system that communicates via a Redis bus. The main features of this architecture include generating telegrams, adding them to the bus, and reading them from the bus.

## Installation

To install the necessary dependencies, run:

```bash
pip install nonoservice
```

## Usage

Below is an example of how to create and use the classes defined in this project.

### Class Telegram

```python
from datetime import date
from your_module import Source, Step, Journey, Debug, ResponsePayload, Ressource, Action, ActionPayload, Trace, LogPayload, Payload, Header, Telegram, EOM

# Create Source
source = Source(application="App1", microservice="Service1")

# Create Step
step = Step(channel="Channel1", source=source)

# Create Journey and add Step
journey = Journey()
journey.add_step(step)

# Create Debug
debug = Debug(file_name="file.py", line=10, expected="expected_value", actual="actual_value", message="Debug message")

# Create ResponsePayload
response_payload = ResponsePayload(success=True, data={"key": "value", "number": 123, "date": date.today()})

# Create ActionPayload
action_payload = ActionPayload(action=Action.CREATE, ressource=Ressource.COURSE, data={"key": "value"})

# Create Trace
trace = Trace(action_telegram={"key": "value"}, message="Trace message")

# Create LogPayload
log_payload = LogPayload(type="Debug", payload=debug)

# Create Payload
payload = Payload(payload=action_payload)

# Create Header
header = Header(token="token123", source=source, channel="Channel1", journey=journey)

# Create Telegram
telegram = Telegram(header=header, payload=payload, checksum="checksum123", end_of_message=EOM)

# Convert to dictionary
telegram_dict = telegram.to_dict()

# Convert to JSON
telegram_json = telegram.to_json()

print(telegram_dict)
print(telegram_json)
```
### Class Postilion

To initialize a Postilion instance, you need to provide a Source object:

```python
# Initialize
from module.Telegram import Source, Postilion

source = Source(application="MyApp", microservice="MyService")
postilion = Postilion(source=source)
```
### Resetting Source

You can reset the source information of the telegram:

```python
new_source = Source(application="NewApp", microservice="NewService")
postilion.reset_source(new_source)
```
### Preparing Header
Prepare the header for the telegram with the specified token, channel, and optional journey:

```python
from module.Telegram import Journey
journey = Journey()
postilion.prepare_header(token="my_token", channel="my_channel", journey=journey)
```
### Preparing Payload

Prepare the payload for the telegram:

```python
from module.Telegram import ActionPayload, Payload

action_payload = ActionPayload(action="my_action")
payload = Payload(payload=action_payload)
postilion.prepare_payload(payload)
```

### Generating Checksum

Generate a checksum for the given header and payload:

```python
checksum = postilion.checksum()
print(checksum)
```

### Verifying Checksum

Verify the checksum of a given telegram:

```python
from module.Telegram import Telegram

telegram = postilion.make_telegram()
is_valid = Postilion.verify_checksum(telegram)
print(is_valid)
```

### Making Telegram

Prepare the telegram with the given payload, including the header and checksum:

```python
telegram = postilion.make_telegram()
print(telegram)
```