Metadata-Version: 2.1
Name: message-wrapper-4sure
Version: 0.2.0
Summary: Adds simple messaging for use with 4Sure micro services and DDyXPM
Home-page: https://www.4-sure.net
Author: Andrew Simpson
Author-email: adehe.d@gmail.com
Maintainer: Andrew Simpson
Maintainer-email: adehe.d@gmail.com
License: MIT
Keywords: django sns messaging
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: 4Sure backend developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: SNS :: Libraries :: Python Modules  :: Django Application
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aws-sns-message-validator
Requires-Dist: boto3
Requires-Dist: djangorestframework

# Message Wrapper 4Sure #
0.1.4

This is a Django application that can be added to a project to add an easy interface into sending and receiving
messages via various message brokers.

### Currently implemented ###
* AWS SNS

### What is this repository for? ###

* Easily register a message topic
* Publish a message to a topic
* Create an endpoint to receive messages that auto-confirms subscriptions
* Manage subscriptions through Django Admin
* Provide a place to record all outbound messages.

### How do I get set up? ###

Assuming you have a supported version of Python, you can first set up your virtual environment with:
```shell
$ python -m venv .venv
...
$ . .venv/bin/activate
```
Then, you can install message-wrapper-4sure with:
```shell
pip install --index-url https://pypi.4-sure.net/simple message-wrapper-4sure
```
### Configuring Boto3 ###
To use SNS you will require AWS credentials\
Set up credentials (in e.g. `~/.aws/credentials`):
```ini
[default]
aws_access_key_id = YOUR_KEY
aws_secret_access_key = YOUR_SECRET
```
Then, set up a default region (in e.g. `~/.aws/config`):
```ini
[default]
region=af-south-1
```
Alternatively you can also use environment variables:
```shell
AWS_ACCESS_KEY_ID - The access key for your AWS account.
AWS_SECRET_ACCESS_KEY - The secret key for your AWS account.
AWS_DEFAULT_REGION - The default AWS Region to use
```
### Configure your setting.py file ###
add messiging into INSTALLED_APPS: 
```python
INSTALLED_APPS = [
    ...,
    'messaging',
]
```
Migrate the database for Message Wrapper:
```shell
python manage.py migrate 
```
## Usage ##
### For outbound ###
To publish a message:
```python
from messaging.topic_providers import MessageWrapper

mw = MessageWrapper.get('sns')
mw.publish('topic', 'message')
```
It is preferred that the message is a json serializable.

### Inbound messaging ###
To receive messages from SNS is a 2-step process.

#### Step 1: Create an API endpoint to receive the message. ####
Create a ViewsetClass in your `views.py`:
```python
from messaging.views import SnsTopicActionViewset
    
class SharedDataMessageReceiver(SnsTopicActionViewset):
    
    @staticmethod
    def topic_to_receive(request, message, *args, **kwargs):
        """Write code here to do someting with the "payload" portion of the message"""
        ...
```
Don't forget to register it in your `urls.py`:
```python
urlpatterns = [
    ...
    re_path(
        '^api/v1/message_receiver/(?P<action>[^/.]+)', SharedDataMessageReceiver.as_view(),
        name='message_receiver'),
]
```


#### Step 2: Subscribe a endpoint to a topic. ####
Click on Topic Callback in Django Admin:\
![img.png](img.png)

Add a callback

![img_1.png](img_1.png)


AWS SNS will automatically attempt to send a confirmation message to the endpoint.\
If using the provided SNSActionAPI and decorators this proces will happen automatically.
