Skip to main content

Architect Python SDK

Project description

Architect Python SDK

This is the Python SDK used for brokering connections to microservice dependencies via Architect's deployment platform. If you're unfamiliar with the platform or our deploy tools, please check out Architect.io and our CLI to get started.

SDK Documentation

Connecting to dependencies

import architect.sdk as architect

my_dependency = architect.service('my_dependency_name')

# Client used to connect to service
client = my_dependency.client

# Dynamically imported models used for message formatting
# Used by protocols supporting code-gen like gRPC
defs = my_dependency.defs

REST

The Architect SDK uses the popular REST client, Requests, to broker communication to downstream REST microservices. The client that is provided is a requests session that is enriched with the proper service location meaning only URIs and HTTP actions need be provided:

res = my_dependency.client.get('/resource')
res = my_dependency.client.post('/resource', { data })
res = my_dependency.client.put('/resource/:resource_id', { data })
res = my_dependency.client.delete('/resource/:resource_id')

gRPC

Architect handles the code-gen for gRPC services for you. Every time you run $ architect install ..., you'll find relevant gRPC stubs generated and placed inside an architect_services folder in your service's root directory. This SDK handles the import of generated model objects for parsing input/output messages as well as the import and enrichment of client code for making calls to dependencies.

// service.proto
syntax = "proto3";

package example_service;

message PayRequest {
    int32 amount = 1;
}

message PayResponse {
    int32 output = 1;
}

service Example {
  rpc Pay (PayRequest) returns (PayResponse) {}
}
# Model definitions will match message names in the .proto file for the service
pay_req = my_dependency.defs.PayRequest(amount=10)

# Client will automatically have methods matching the service definition from 
# the dependency's .proto file. Response handling matches gRPC documentation.
pay_res = my_dependency.client.Pay(pay_req)
output = pay_res.output

Connecting to data stores

Since there are so many DB clients available, we don't want to choose a default for developers. Instead, the Architect SDK provides easy mechanics to parse credentials provided by the platform:

import psycopg2
import architect.sdk as architect

# Key used to cite the datastore must match the key used in your 
# architect service config
db_config = architect.datastore('primary_db')
connection = psycopg2.connect(dbname=db_config.name, 
                              user=db_config.username,
                              password=db_config.password, 
                              host=db_config.host,
                              port=db_config.port)

Events and messaging

The Architect platform also supports pub/sub based communication between services. The primary use-case for this flow would be to allow services to broadcast events for other services to subscribe to without needing to know who the subscribers are.

NOTE: As of v0.2.x of the SDK, the only method for publishing/subscribing to events using Architect is via REST (mirroring the function of traditional webhooks). We're actively working on means of supporting queuing systems like RabbitMQ, AWS SQS, and more.

Subscribing

// architect.json
{
  "subscriptions": {
    "<service_name>": {
      "<event_name>": {
        "uri": "/event/callback",
        "headers": {
          "x-custom-header": "example"
        }
      }
    }
  }
}

The URI used for registration must match a URI on your service:

from flask import Flask, request
import architect.sdk as architect

app = Flask(__name__)

@app.route("/event/callback")
def callback():
    # Custom event handling here

if __name__ == '__main__':
    app.run(host=os.environ['HOST'], port=os.environ['PORT'])

Publishing

NOTE: simple publication methods coming soon. For now, you can iterate through subscribers to submit events.

// architect.json
{
  "notifications": ["<event_name>"]
}
from requests import Request, Session
import architect.sdk as architect

# Iterate through notification subscribers to POST payload
for sub in architect.notification('<event_name>').subscriptions:
    r = requests.post('{}:{}{}'.format(sub.host, sub.port, sub.uri), data = {})

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

architect.sdk-0.2.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

architect.sdk-0.2.1-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file architect.sdk-0.2.1.tar.gz.

File metadata

  • Download URL: architect.sdk-0.2.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for architect.sdk-0.2.1.tar.gz
Algorithm Hash digest
SHA256 62af52eb6f21cf10a39a8ff5b997492183403544a18672f76758f8431adab1c5
MD5 aaf0b6c144e53909664d2d9db092c03a
BLAKE2b-256 43257333a860ba39be2b491b7a18fa922ac88ac4a7e1719f63617eea0270fc1e

See more details on using hashes here.

File details

Details for the file architect.sdk-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: architect.sdk-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for architect.sdk-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df507e4850d188dcb303c39a253bb64028db44c40e9ab8976a19b250d5d8a177
MD5 55bb6af4d7d1244f328d4232c9b91791
BLAKE2b-256 d37de1c77609d75ab173840e02e814e1ea6db5eafb34c09c5051bdc08efaa994

See more details on using hashes here.

Supported by

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