lazr-restfulclient
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
-
lazr.restfulclient.errors.Unauthorized: HTTP Error 401: Unauthorized
cause Attempting to perform an action on a service (e.g., Launchpad) without sufficient authentication or authorization, or trying to access a project you do not own/have permissions for.fixEnsure you have correctly configured your authentication (e.g., OAuth credentials, HTTP Basic Auth) for the `ServiceRoot`. If interacting with Launchpad, verify your project name and ownership, and configure 'quickly' with your Launchpad project name. -
HTTP Error 404: Not Found
cause The requested resource (collection or entry) does not exist at the specified URL on the server.fixDouble-check the service root URL and the paths to collections and entries. Verify the resource ID or key if you are trying to fetch a specific item. -
ConflictError: HTTP Error 409: Conflict
cause Attempting to save changes to a resource that has been modified by another client since it was last fetched (e.g., a concurrent modification).fixBefore saving, refresh the object using `obj.lp_refresh()` to get the latest server-side state. Then, re-apply your changes and attempt to save again. Implement optimistic locking logic if concurrent modifications are common.
Warnings
- breaking The WSGI authentication middleware was moved from `lazr.restful` to `lazr.authentication` in `lazr.restfulclient` version 0.9.9. Direct usage of the old authentication middleware will break.
- gotcha Client-side objects can 're-type' after fetching data. If a collection is initially configured to contain a generic resource type, but the server returns a more specific type upon fetching, the client-side object will adopt the properties of the actual, more specific type. This can be confusing if not expected.
- gotcha The library automatically retries requests on transient server errors (HTTP 502, 503) a configurable number of times. However, it will NOT retry on client-side errors (e.g., HTTP 400 Bad Request or 404 Not Found), escalating them directly as exceptions.
- gotcha Incompatibilities with `httplib2` versions have occurred. If you encounter unexpected HTTP connection issues or errors related to `httplib2`, ensure your `httplib2` version is compatible with `lazr.restfulclient`.
Install
-
pip install lazr-restfulclient
Imports
- ServiceRoot
from lazr.restfulclient.client import ServiceRoot
- lazr.restfulclient
import lazr.restfulclient
Quickstart
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}")