Skip to main content
https://github.com/crazytruth/iniesta/raw/master/docs/source/_static/iniesta.png

Iniesta

Build Status Documentation Status Codecov

PyPI pyversions PyPI version PyPI license Black

A messaging integration for the event driven pattern utilizing AWS SNS and AWS SQS for Insanic.

Why?

Iniesta is a messaging integration plugin for Insanic. This was initially created to easily apply the event driven pattern to services running Insanic.

For a bit of context, Andrés Iniesta is a Spanish professional soccer player who plays as a central midfielder. He is considered one the best soccer players and one of the greatest midfielders of all time. For those of you unfamiliar with soccer, a midfielder is responsible for playmaking, passing the soccer ball from the defense to the forwards.

Consequently, this project aims to be the messenger between services; a proxy, for sending messages(the soccer ball) from the producers(defenders) to the consumer(strikers) albeit the messages fan out and there is only one soccer ball.

Features

  • Asynchronous message handling.

  • Produce messages to a global SNS.

  • Filters for verification and subscribing SQS to SNS.

  • Polling for SQS and receiving messages.

  • Decorator for consuming messages with defined parameters.

  • Locks for idempotent message handling.

  • Checks for if proper subscriptions have been setup.

  • Checks for if proper permissions has been setup.

  • Decorators for emitting messages.

Installation

Prerequisites:

  • python >= 3.6

  • AWS Credentials

To install:

pip install insanic-iniesta

To setup, we need a couple settings.

  • INIESTA_INITIALIZATION_TYPE : (list[string]) List of initialization types defined by InitializationTypes enum.

    • Choices: “QUEUE_POLLING”, “EVENT_POLLING”, “SNS_PRODUCER”, “CUSTOM”

  • INIESTA_SQS_CONSUMER_FILTERS: (list) default:[] A list of filters for the message events your service will want to receive.

  • INIESTA_SNS_PRODUCER_GLOBAL_TOPIC_ARN: (string) default:None The global sns arn.

  • INIESTA_SNS_EVENT_KEY: (string) default:iniesta_pass The key the event will be published under. Will NOT want to change this.

  • INIESTA_LOCK_RETRY_COUNT: (int) default:1 Lock retry count when lock is unable to be required

  • INIESTA_LOCK_TIMEOUT: (int) default:10s Timeout for the lock when received

Basic Usage

There are several initialization types because not all applications need to produce, or receive messages at the same time. So you would need to set the initialization type catered towards your use case.

Initializing

First we need to decide on the type of initialization we need to use. For Iniesta to know the initialization type, we need to set INIESTA_INITIALIZATION_TYPE in our config. Until we do so, you will not be able to run Insanic.

# in your application config.py
INIESTA_INITIALIZATION_TYPE = ["SNS_PRODUCER"]

# and finally in app.py
from insanic import Insanic
from iniesta import Iniesta

app = Insanic('producer', version="0.0.1")
Iniesta.init_app(app)

For more documentation on initialization types, please refer to the Iniesta’s Documentation.

Publishing Messages to SNS

You would want to setup Iniesta if you ONLY need to produce messages to SNS.

from iniesta.sns import SNSClient

sns = SNSClient(topic_arn)
message = sns.create_message(event="EventHappened", message={"id": 1}, version=1)
await message.publish()

This will publish a message to SNS with the event specified in the parameters. The published event will be {event}.{service_name}. Even if you don’t specify the service_name in the event, it will automatically be appended.

Consuming Messages

To consume messages that other applications have produced, we setup Iniesta for EVENT_POLLING.

There are several checks Iniesta performs when initializing for EVENT_POLLING.

  • Checks if the AWS SQS has been created.

  • Checks if global arn is set (INIESTA_SNS_PRODUCER_GLOBAL_TOPIC_ARN)

  • Checks if filters have been defined (INIESTA_SQS_CONSUMER_FILTERS).

  • Checks if subscriptions has been made with service SQS and SNS.

  • Checks if necessary permissions have been put in place.

Initial setup for event polling:

# in your config.py
INIESTA_INITIALIZATION_TYPE = ['EVENT_POLLING']

# in service named receiver
from insanic import Insanic
from iniesta import Iniesta

app = Insanic('receiver')
Iniesta.init_app(app)

Since we have initialized for polling our queue, we need to create handlers for processing the messages.

For creating a handler for an event:

from iniesta.sqs import SQSClient

@SQSClient.handler('EventHappened.producer')
def event_happened_handler(message):
    # .. do some logic ..
    pass

The function must receive message as an argument. If the function successfully runs, the message will be deleted from SQS.

Other Use Cases

For other use cases for Iniesta, please refer to the Documentation.

Commands

Iniesta provides several commands to help testing during development.

Install

$ pip install iniesta[cli]

Basic Usage

$ iniesta --help
Usage: iniesta [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  filter-policies
  initialization-type
  publish
  send

Please refer to the Iniesta’s Commands Documentation for more information on each of the available commands.

Known Issues

  • If the module including the handlers are not imported, they do not properly register.

    To prevent this import the module somewhere(e.g. in your app.py) until a better solution is found.

Release History

View release history here

Contributing

For guidance on setting up a development environment and how to make a contribution to Iniesta, see the CONTRIBUTING.rst guidelines.

Meta

Distributed under the MIT license. See LICENSE for more information.

Thanks to all the people at my prior company that worked with me to make this possible.

Metadata

Release files for insanic-iniesta 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for insanic-iniesta 0.4.0
File Size Uploaded Provenance Actions
insanic-iniesta-0.4.0.tar.gz 151.1 kB Without provenance Details for insanic-iniesta-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for insanic-iniesta 0.4.0
File Interpreter ABI Platform Actions
insanic_iniesta-0.4.0-py3-none-any.whl Python 3 none any Details for insanic_iniesta-0.4.0-py3-none-any.whl

Total release size: 177.8 kB

Release files / insanic-iniesta-0.4.0.tar.gz

Download URL insanic-iniesta-0.4.0.tar.gz
Size 151.1 kB
Tags Source
SHA-256 checksum
How to use checksums
628f60e7a15e687f4540ccd477054916d11f63f9b09af16069a0b9f6ef8c4a36
BLAKE2b-256 checksum
How to use checksums
44801c329979440a4693f83fbdb0e83228865ed880c3fc8af6096b365960436e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.1

Release files / insanic_iniesta-0.4.0-py3-none-any.whl

Download URL insanic_iniesta-0.4.0-py3-none-any.whl
Size 26.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
47d0cfd1a10ad19d097949853652d1b6ae8a0acb9fbcfaa0422b10fd71ac8c8a
BLAKE2b-256 checksum
How to use checksums
892c1579197fe38127441f38c8b2cd812a313efb735e2f621a3d62bba2561b49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.1

Security for insanic-iniesta 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page