Skip to main content

Python client SDK for Orb's API

Project description

Python SDK

The modern pricing platform to bill for seats, consumption, and everything in between.

SDK Installation

pip install orb-billing

SDK Example Usage

import orb
from orb.models import shared

s = orb.Orb(
    api_key="",
)


res = s.availability.ping()

if res.ping_response is not None:
    # handle response
    pass

Types

The SDK uses data classes for all generated models which provides in line hinting and documentation within your IDE. If you would like to see type errors in VS Code to help catch bugs earlier, set python.analysis.typeCheckingMode to basic.

Error Handling

There are several reasons SDK operations can fail in practice:

  • A particular resource cannot be found
  • You are not authorised to access certain resources
  • The request cannot be sent due to a network error

You can build robust code using the SDK with error handling. The example below demonstrates how to handle a subset of errors while falling back to re-raising others. Note that, in production environments, it may be beneficial to use a logging library instead of calling print().

import orb
import orb.models.errors

# Initialize the SDK.
s = orb.Orb(api_key_auth="my-orb-key")

customer_id = "sample_customer_id"

try:
  response = s.customer.fetch_by_external_id(external_customer_id=customer_id)
  customer = response.customer
  if customer is None:
    raise Exception("No customer data available")

  # Use the customer data we just received.
  id = customer
  provider = customer.payment_provider
  print(f"Customer '{id}' uses '{provider}' as payment provider.")
except orb.models.errors.FourHundredAndFourError as e:
  # Handle a 404 response for when a customer is not found
  print(f"Customer '{customer_id}' was not found.")
except orb.models.errors.FourHundredAndOneError as e:
  # Handle a 401 response when trying to access resources we are not authorized
  # to access.
  message = e.title or "Unauthorized request"
  message += f": {e.detail}" if e.detail else ""
  print(message)
except orb.models.errors.FiveHundredError as e:
  # Handle 5xx responses that occur when the server is experiencing issues.
  message = e.title or f"A server-side error ({e.status}) occured."
  message += f": {e.detail}" if e.detail else ""
  print(message)
except orb.models.errors.SDKError as e:
  # Handle any issues where we were unable to read the response from the server.
  print("Unexpected API error occurred.")
  print(f"Status code: {e.status_code}")
  print(f"Message: {e.message}")
except Exception as e:
  # We don't necessarily want to handle all possible errors so this final
  # catch-all block may be omitted entirely and the exception will be thrown
  # upwards in this program.
  print("Unexpected error occurred.")
  raise e

Pagination

Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the returned response object will have a Next method that can be called to pull down the next group of results. If the return value of Next is None, then there are no more pages to be fetched.

Here's an example of one such pagination call:

import orb

# Initialize the SDK.
s = orb.Orb(api_key="my-orb-key")

cursor = None
keep_fetching = True

# We start by attempting to fetch at least one page of results. 
while keep_fetching:
  # The SDK call takes the cursor and any additional arguments to filter the
  # coupon data.
  response = s.coupon.list(cursor=cursor, show_archived=False)
  if response.status_code != 200:
    raise Exception(f"Unexpected status code received from server: {response.status_code}")

  # Ensure the response from the server contains coupon data.
  if response.coupons is None:
    break

  # Check if we received an empty page of results. This is a signal that we
  # should stop requesting more data from the server.
  page = response.coupons.data
  if not page:
    break

  # Capture the cursor for the next loop. If there is no cursor then use that
  # as a signal to stop iteration.
  cursor = response.coupons.pagination_metadata.next_cursor
  keep_fetching = cursor is not None

  # At this point we have a page of coupon entries that we can collect or
  # iterate through as shown below.
  for coupon in page:
    code = coupon.redemption_code
    redeem_count = coupon.times_redeemed
    print(f"Coupon ${code} was redeemed ${redeem_count} times.")

Overriding the Default Client

By default, this SDK uses the Python requests library as the default HTTP client. However, you can use a custom HTTP client to enable using a proxy, enable custom telemetry, or use preconfigured global headers or additional configuration.

Here's an example of how to override with a custom client.

s = orb.Orb()

# Your custom HTTP client
client = requests.Session()

s.config_client(client)

Versions

This package follows SemVer conventions. Backwards-incompatible changes may be released as minor versions. Previous versions of the SDK can be found under the Releases tab of the repository.

Available Resources and Operations

availability

  • ping - Check availability

coupon

credit

credit_note

  • fetch - Fetch credit note
  • list - List credit notes

customer

event

invoice

item

metric

plan

price

price_interval

subscription

Dev Containers

Experience our SDK in an enhanced sandbox environment. Try it now in GitHub Codespaces!

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release !

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

orb-billing-1.26.0.tar.gz (142.2 kB view hashes)

Uploaded Source

Built Distribution

orb_billing-1.26.0-py3-none-any.whl (329.5 kB view hashes)

Uploaded Python 3

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