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
Built Distribution
Hashes for architect.sdk-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df507e4850d188dcb303c39a253bb64028db44c40e9ab8976a19b250d5d8a177 |
|
MD5 | 55bb6af4d7d1244f328d4232c9b91791 |
|
BLAKE2b-256 | d37de1c77609d75ab173840e02e814e1ea6db5eafb34c09c5051bdc08efaa994 |