Skip to main content

Declarative HTTP client SDK for Python microservices

Project description

unitpulse-feign

Declarative HTTP client for Python microservices, inspired by Spring Cloud OpenFeign. Integrates with unitpulse-discovery to call services by name with automatic load balancing.

Installation

pip install "./unitpulse-feign"

Usage

from unitpulse_feign import setup_feign, get, post, put, delete, patch

# Must be called after setup_discovery
setup_feign(app)

@app.feign.client("target-service", lb_strategy="round_robin")
class TargetClient:
    @get("/api/users/{user_id}")
    def get_user(self, user_id: int) -> dict: pass

    @post("/api/users")
    def create_user(self, body: dict) -> dict: pass

# Create singleton and reuse — do NOT instantiate per request
client = TargetClient()
response = client.get_user(user_id=123)
response = client.create_user(body={"name": "Alice"})

Parameters

setup_feign(app)

Attaches app.feign to the app. Must be called after setup_discovery.

@app.feign.client(...)

Parameter Type Default Description
service_name str Target service name (discovery mode)
host str Direct host (direct mode, no discovery)
port int Direct port (direct mode)
scheme str "http" "http" or "https"
timeout int 5 Request timeout (seconds)
lb_strategy str "round_robin" Load balancing strategy

Modes:

  • Discovery mode: provide service_name — instances resolved via app.discovery
  • Direct mode: provide host + port — bypasses discovery

HTTP Method Decorators

Decorator Method
@get(path) GET
@post(path) POST
@put(path) PUT
@delete(path) DELETE
@patch(path) PATCH

Path supports {variable} placeholders matched to keyword arguments:

@get("/api/users/{user_id}/orders/{order_id}")
def get_order(self, user_id: int, order_id: int) -> dict: pass

client.get_order(user_id=1, order_id=42)

Load Balancing Strategies

Strategy Description
round_robin Cycles through instances in order
random Randomly selects an instance
weighted Weighted random based on weight in instance metadata
least_conn Selects instance with fewest active connections

Async Support

Each method exposes .async_call for use in async contexts:

result = await client.get_user.async_call(user_id=123)

License

MIT

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

unitpulse_feign-0.2.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

unitpulse_feign-0.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file unitpulse_feign-0.2.0.tar.gz.

File metadata

  • Download URL: unitpulse_feign-0.2.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for unitpulse_feign-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ccc37306103a87c7ae48c4d0eb9e33b5231103868ff7b076ae82c1b5baad4796
MD5 309d4280151eb247f0d40b98f2918a1b
BLAKE2b-256 9f5ea32783a2e30bde32bba1e1d9faa809fef072f0a2e8602a33357b7033ec57

See more details on using hashes here.

File details

Details for the file unitpulse_feign-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for unitpulse_feign-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9adb825224d5c7f6d10684021f37ac4c899593c9b19eed459914a691827d8de9
MD5 92980d8824b9614ef2adde9ec4cd1796
BLAKE2b-256 38cdd99bff631bbf5ecd89de8da65da7bc2602bdb128c7201781a13be452885a

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