Skip to main content

python-nest

Python library for nest style Microservices

Description

There is one nodejs framework named nestjs which is perfect to develop microservices. but we wanted to implement some microservices in python to work together with nestjs framework, I didn't find a solution, so I developed this package to do that, for now, it only implemented the MessagePattern so in nestjs, it is @MessagePattern and we can have same function in python with this lib/package.

How to use

Install latest version

pip install python-nestjs

The microservices server

in nestjs, you can do it with @MessagePattern, and the pattern can be string or object in nestjs, similarly in python the pattern can also be str or dict, you can find sample code test_server.py here is sample code, e.g.

in your nest_server.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from nest import NestMsServer, MessagePatternBaseHandler

app = NestMsServer()

to have handler function to process the pattern, you have two options

  • have a class which extends MessagePatternBaseHandler
  1. have the class
class TestDictHanlder(MessagePatternBaseHandler):
    def __init__(self):
        pass

    def get_message_pattern(self):
        '''return the message pattern

        it can be string or dict
        e.g. 'TEST' or {'cmd': 'test'}
        '''
        return {'cmd': 'TEST_PATTERN' }

    def handle(self, payload):
        '''handler function to process payload

        It should returns
        err - any error or None
        result - the processed result
        '''
        print(payload)
        return None, ['this is test dict', 'another test dict result']
  1. add/register the handler class
app.add_handler(TestDictHanlder)
  • have a function decorated with decorator message_pattern
  1. write a function and decorate it
@app.message_pattern({'cmd': 'test_decorator'})
def test_decorator(payload):
    '''test decorator function

    the function accepts one parameter payload
    '''
    print(payload)
    return None, payload

start/run it

    HOST ='localhost'
    PORT = 7086
    print(f'started to run and listed to port {PORT}')
    app.run(HOST, PORT)

The microservices client

if you want to call microservices which was implemented in nestjs, it is also simple, find in test_client.py or sample code here.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from nest import NestMsClient

if __name__ == '__main__':
    HOST ='localhost'
    PORT = 7086
    client = NestMsClient(HOST, PORT)
    pattern = 'TEST_PATTERN'
    res = client.send(pattern, None)
    print(res)
    pattern = { 'cmd' :'TEST_PATTERN' }
    res = client.send(pattern, None)
    print(res)
    pattern = { 'cmd' :'test_decorator' }
    res = client.send(pattern, 'this is ok')
    print(res)

Maintainers

@jerrywang1981

Contributors

License

MIT License

Metadata

Release files for python-nestjs 0.0.4

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

Source distribution (sdist)

Source distribution for python-nestjs 0.0.4
File Size Uploaded
python-nestjs-0.0.4.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-nestjs 0.0.4
File Interpreter ABI Platform
python_nestjs-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / python-nestjs-0.0.4.tar.gz

Download URL python-nestjs-0.0.4.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
799ebc82c43d52559e8371823aed06170a5eba68ed0159f5f002a4514c9048ba
BLAKE2b-256 checksum
How to use checksums
7de385210d46374e6a47d8597636bbe59b23ca1e01c16c42a200bffbfd50812b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

Release files / python_nestjs-0.0.4-py3-none-any.whl

Download URL python_nestjs-0.0.4-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5618af1abd664c12f03bae1515d2530396c3a4824e20a479e4cb5cf487cc0e8b
BLAKE2b-256 checksum
How to use checksums
1e061c15bc689bbc88eb0690956b5b07f3012b688f462d70578dba15e1d94098
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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