Skip to main content

Python client library for Space - pricing-driven self-adaptation platform

Project description

Space Python Client

Python client library for Space, a pricing-driven self-adaptation platform for SaaS applications.

PyPI License: MIT

Table of Contents

What You Get

  • Simple API to connect to Space.
  • Contract lifecycle operations.
  • Feature evaluation with optional expected consumption.
  • Revert operation for optimistic usage updates.
  • Pricing token generation.
  • Built-in in-memory cache and optional Redis cache.
  • WebSocket events for real-time pricing updates.

Requirements

  • Python 3.10+

Installation

pip install space-python-client

Optional Redis support:

pip install "space-python-client[redis]"

Quick Start in 5 Minutes

from space_client import SpaceClientFactory

client = SpaceClientFactory.connect(
	"http://localhost:3000",
	"your-api-key",
	10_000,
)

healthy = client.is_connected_to_space()
print(f"Connected: {healthy}")

result = client.features.evaluate("user-123", "serviceA-featureX")
if result.error is not None:
	print(f"Evaluation error: {result.error.message}")
else:
	print(f"Feature enabled: {result.eval}")

pricing_token = client.features.generate_user_pricing_token("user-123")
print(f"Pricing token: {pricing_token}")

client.close()

Configuration

You can connect using either convenience factory arguments or SpaceConnectionOptions.

Option A: simple connection

from space_client import SpaceClientFactory

client = SpaceClientFactory.connect("http://localhost:3000", "your-api-key")

Option B: custom timeout

client = SpaceClientFactory.connect("http://localhost:3000", "your-api-key", 15000)

Option C: full options object

from space_client import SpaceClientFactory
from space_client.types import CacheOptions, SpaceConnectionOptions

options = SpaceConnectionOptions(
	url="http://localhost:3000",
	api_key="your-api-key",
	timeout=15000,
	cache=CacheOptions(enabled=True),
)

client = SpaceClientFactory.connect(options)

SpaceClientFactory.connect(...) validation rules:

  • URL is required and must start with http:// or https://.
  • API key is required.
  • Timeout must be a positive number.

API Overview

SpaceClientFactory

Method Description
connect(options) Creates a client from full options and validates required inputs.
connect(url, api_key) Convenience overload with default timeout (5000).
connect(url, api_key, timeout) Convenience overload with custom timeout.

SpaceClient

Main entry point. Exposes modules as public fields:

  • contracts
  • features
  • cache

Core methods:

Method Returns Details
is_connected_to_space() bool HTTP health check against /healthcheck.
on(event, callback) None Registers event callback for supported event names.
remove_listener(event) None Removes one callback by event name.
remove_all_listeners() None Removes all event callbacks.
connect() None Connects/reconnects WebSocket namespace if disconnected.
disconnect() None Disconnects WebSocket namespace.
close() None Closes sockets, cache provider, and HTTP resources.

Java-style aliases are also available (isConnectedToSpace, removeListener, removeAllListeners, etc.).

ContractModule

Method Returns
get_contract(user_id) `Contract
add_contract(contract_to_create) `Contract
update_contract_subscription(user_id, new_subscription) `Contract
update_contract_subscription_by_group_id(group_id, new_subscription) `list[Contract]
update_contract_usage_levels(user_id, service_name, usage_levels_novations) `Contract
remove_contract(user_id) None

Java-style aliases are also available (getContract, addContract, etc.).

FeatureModule

Method Returns
evaluate(user_id, feature_id, expected_consumption=None, details=False, server=False) FeatureEvaluationResult
revert_evaluation(user_id, feature_id, revert_to_latest=True) bool
generate_user_pricing_token(user_id) `str

Java-style aliases are also available (revertEvaluation, generateUserPricingToken).

Data Models

Main model classes are exported from space_client.types:

  • SpaceConnectionOptions
  • CacheOptions, CacheType, ExternalCacheConfig, RedisConfig
  • Contract, ContractToCreate, Subscription
  • FeatureEvaluationResult, EvaluationError
  • BillingPeriod, BillingPeriodToCreate, ContractHistoryEntry, UsageLevel, UserContact
  • SpaceEvent

Caching

Built-in cache is enabled with:

from space_client.types import CacheOptions, SpaceConnectionOptions

options = SpaceConnectionOptions(
	url="http://localhost:3000",
	api_key="your-api-key",
	cache=CacheOptions(enabled=True),
)

Redis cache example:

from space_client.types import (
	CacheOptions,
	CacheType,
	ExternalCacheConfig,
	RedisConfig,
	SpaceConnectionOptions,
)

options = SpaceConnectionOptions(
	url="http://localhost:3000",
	api_key="your-api-key",
	cache=CacheOptions(
		enabled=True,
		type=CacheType.REDIS,
		external=ExternalCacheConfig(
			redis=RedisConfig(host="localhost", port=6379),
		),
		ttl=300,
	),
)

WebSocket Events

Supported event names:

  • synchronized
  • pricing_created
  • pricing_archived
  • pricing_actived
  • service_disabled
  • error

Example:

def on_pricing_created(details):
	print("Pricing changed:", details)


client.on("pricing_created", on_pricing_created)
client.connect()

Error Handling

  • Factory validation raises ValueError for invalid input.
  • Runtime HTTP failures are converted to None/False return values where appropriate.
  • FeatureModule.evaluate(...) returns an object containing error when request handling fails.

Testing

Run tests locally:

pip install -e .[dev]
pytest

CI executes tests on every pull request for Python 3.10, 3.11, and 3.12.

Publishing to PyPI

The repository includes a release workflow that publishes automatically to PyPI when a GitHub Release is published.

Recommended setup:

  1. Configure PyPI Trusted Publishing for your GitHub repository.
  2. Create a new GitHub Release (tag like v0.1.0).
  3. The workflow builds the package and publishes it to PyPI.

Contributing

  1. Create a branch from main.
  2. Add or update tests for every behavior change.
  3. Open a pull request.

License

MIT License.

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

space_python_client-0.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

space_python_client-0.1.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file space_python_client-0.1.0.tar.gz.

File metadata

  • Download URL: space_python_client-0.1.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for space_python_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2f35dffe9f42f955f69bbbce35d90be24a8b45ec43182a754ad3124921d3da08
MD5 6aed35392e19ef5ff853c55450fd8d63
BLAKE2b-256 402d66e0641e3dcc1ea07b407810c8764a091dd7e3cc4fc7c2f144038cb6e13e

See more details on using hashes here.

Provenance

The following attestation bundles were made for space_python_client-0.1.0.tar.gz:

Publisher: publish.yml on Alex-GF/space-python-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file space_python_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for space_python_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 970370bc8bd4226d0e6ebf4e8fae8df19b57de779d7d672fd3d967d5d050a938
MD5 749ef23463bc57762dbae88254a476b1
BLAKE2b-256 a9882fe76d9fcb5025dc4bbf68b3aba9b829ed4ab399f65be1e755db511e2633

See more details on using hashes here.

Provenance

The following attestation bundles were made for space_python_client-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Alex-GF/space-python-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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