lazr-restfulclient

0.14.6 · active · verified Thu Apr 16

lazr.restfulclient is a programmable client library that leverages commonalities among lazr.restful web services to provide extended functionality built upon wadllib. It is actively maintained, with the current version 0.14.6 released in January 2024, including updates for Python 3.12 compatibility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize a `ServiceRoot` client and access a top-level collection. This example uses a placeholder URL and assumes a basic RESTful service structure.

import os
from lazr.restfulclient.client import ServiceRoot
from lazr.restfulclient.errors import HTTPError

# Example: Connect to a mock service root (replace with your actual service URL)
# For Launchpad services, the root URL would be like 'https://api.launchpad.net/1.0/'
SERVICE_ROOT_URL = os.environ.get('LAZR_SERVICE_URL', 'https://api.example.com/')

try:
    # Initialize a ServiceRoot. The first argument is typically an authorizer, or None for unauthenticated access.
    service = ServiceRoot(None, SERVICE_ROOT_URL)

    # Attempt to access a top-level collection, e.g., 'users'
    # This will trigger an HTTP GET request to SERVICE_ROOT_URL/users
    # Note: Replace 'users' with an actual collection name for your service
    users_collection = service.users
    print(f"Successfully accessed collection: {users_collection.lp_self_link}")

    # Example: Fetch a specific item from the collection (if it supports key-based lookup)
    # Assuming 'users_collection' can be indexed by ID, e.g., user with ID '123'
    # This will perform another HTTP GET request
    # user = users_collection[123]
    # print(f"Fetched user: {user.display_name}")

except HTTPError as e:
    print(f"HTTP Error encountered: {e.status} - {e.reason}\n{e.response}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →