Skip to main content

A set of utilities to build API gateway.

Project description

Usage: Define Command

Command represents an external operation. To define a command, you must inherit from blackgate.Command.

Example:

from blackgate import Command

class GetCurrentUser(Command):

    group_key = 'service.session'
    command_key = 'get_current_user'

    def __init__(self, cookie):
        self.cookie = cookie

    def run(self):
        resp = requests.get('http://session-service/session', headers={'Cookie': self.cookie}, timeout=1)
        return resp.json()['user_id']

    def fallback(self):
        return None

Usage: Synchronous Execution

You can contruct command with parameters and then execute it:

get_current_user = GetCurrentUser(cookie='session=78b29404-92c9-49db-b87b-531b5f9cfc56')
user_id = get_current_user.execute()

Usage: Asynchronous Execution

You can execute a Command asynchronously by using the queue() method, as in the following example:

@tornado.gen.coroutine
def some_function():
    get_current_user = GetCurrentUser(cookie='session=78b29404-92c9-49db-b87b-531b5f9cfc56')
    user_id = yield get_current_user.queue()
    print(user_id)

Usage: Fallback

Usage: Proxy

Configure Blackgate Component:

from blackgate import component
component.add('executor_pool', {'group_key': 'api.v1', 'max_workers': 10})
component.install()
app = tornado.web.Application([
  (r'/api/v1/(.*)', HTTPProxy, dict(command=APIV1Command)),
])
app.listen(8888)
tornado.ioloop.IOLoop.current().start()

Route User Request to Inner Service:

from blackgate import HTTPProxyCommand

class APIService(HTTPProxyCommand):

    group_key = 'api.v1'

    command_key = 'proxy_v1_traffic'

    def before_request(self):
        path = self.request['path'].replace('/api', '')
        self.request['url'] = 'http://intra.service.com' + path

    def fallback(self):
        return dict(
            status_code=500,
            reason='Internal Server Error',
            headers={},
            content=json.dumps({
                'code': 500,
                'message': 'internal server error',
                'error': {}
            })
        )

ThreadExecutorPool

Executor Pool is the place where you can run external network calls.

Each command defines its group_key. You must register an executor_pool for specific group_key:

from blackgate import component
component.add('executor_pool', {'group_key': 'service.session', 'max_workers', 10})
# ...
component.install()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blackgate-0.2.2.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

blackgate-0.2.2-py2.py3-none-any.whl (11.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file blackgate-0.2.2.tar.gz.

File metadata

  • Download URL: blackgate-0.2.2.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for blackgate-0.2.2.tar.gz
Algorithm Hash digest
SHA256 884b4044565737cc80e6ec52b17e57a17e2f12e38193a75020c3b07d27398059
MD5 e1b4156d4d110a19f9113eb17590db17
BLAKE2b-256 8576613248cc5a9b12c8d1b493c01451becdf8a3ebd4b6bbfaea01b21c03b431

See more details on using hashes here.

File details

Details for the file blackgate-0.2.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for blackgate-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 68f03126d5c415cd6ccbd7d9b61d93908abc39187fbd10b5f793b78092a6e0bd
MD5 38b1648875f4571dea396b01e90483e1
BLAKE2b-256 fe2563a365a8b65db08fcfe31c762aa5e7ea1b86ce60eb2ea3741d18decf0e0c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page