Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

brewblox-service

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brewblox-service

Scaffolding for Brewblox backend services

  • 3.3.2
  • PyPI
  • Socket score

Maintainers
1

Scaffolding for Brewblox service applications

In order to reduce code duplication between services, generic functionality is implemented here.

For an example on how to implement your own service based on brewblox-service, see https://github.com/brewblox/brewblox-boilerplate.

brewblox_service

Small generic tools are defined here.

brewblox_logger can be used for creating module-specific loggers. It is not required, but will play a bit nicer with default formatting of the log.

Example:

from brewblox_service import brewblox_logger

LOGGER = brewblox_logger(__name__)
LOGGER.info('hello')

service.py

Parses commandline arguments, creates an aiohttp application object, and runs it.

The shortest implementation is:

parser = service.create_parser('my_service')
config = service.create_config(parser)
app = service.create_app(config)
service.run_app(app)

This will get you a working web application, but it will not have any endpoints.

Applications can configure their own features, and add new commandline arguments.

Example:

class ExtendedConfig(models.BaseServiceConfig):
    my_arg: str

# Separately creating the parser allows adding arguments to the default set
parser = service.create_parser('my_service')
parser.add_argument('--my-arg')

# Get a config object from the extended args
config = service.create_config(parser, model=ExtendedConfig)

# Now create the app
app = service.create_app(config)

async def setup():
    # Add features for this service
    # You can call async functions here
    device.setup(app)
    api.setup(app)

# Run the app. It will await the setup() coroutine
service.run_app(app, setup())

features.py

Many service features are application-scoped. Their lifecycle should span multiple requests, either because they are not request-driven, or because they manage asynchronous I/O operations (such as listening to AMQP messages).

The ServiceFeature class offers an abstract base class for this behavior. Implementing classes should define startup(app) and shutdown(app) functions, and those will be automatically called when the application starts up and shuts down.

Both startup() and shutdown() are called in an async context, making them the async counterparts of __init__() and __del__() functions.

Features must be constructed after the app is created, but before it starts running. (service.create_app() and service.run_app(app)). It is recommended but not required to add features in an async setup function.

The add() and get() functions make it easy to centrally declare a feature, and then use it in any function that has a reference to the aiohttp app.

repeater.py

A very common use case for features is to have the equivalent of a while True loop. The RepeaterFeature class handles the boilerplate involved in setting this up in an async context.

mqtt.py

Basic MQTT messaging: publish and listen to events. To publish data, all you need is the publish(topic, message) function.

To listen to incoming messages, you can combine subscribe(topic) with one or more calls to listen(topic, callback). The subscribe/listen functions allow for + and # wildcards to be used.

For a detailed explanation of how to use MQTT topics, see http://www.steves-internet-guide.com/understanding-mqtt-topics/.

For the Brewblox spec on how and where to publish data, see https://brewblox.com/dev/reference/events.html.

Includes top-level convenience functions for publish(topic, message), listen(topic, callback) and subscribe(topic).

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc