Simple REST Client

1.2.1 · active · verified Thu Apr 16

Simple REST client for Python 3.8+ that aims to make interacting with REST APIs straightforward. It provides a clean API for defining resources and actions, handles HTTP requests, and parses responses. Currently at version 1.2.1, it follows a stable release cadence with updates addressing bug fixes and minor improvements, focusing on simplicity and ease of use.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `SimpleRestClient`, define a `Resource` class, and perform common REST operations like `list`, `get`, and `create` against a public API. It includes an example of handling authentication headers and emphasizes best practices like enabling SSL verification.

import os
from simple_rest_client.client import SimpleRestClient
from simple_rest_client.resource import Resource

# Define a resource for users. Basic CRUD actions (list, get, create, update, delete) are
# automatically available if not overridden or custom actions defined.
class UserResource(Resource):
    pass

# Initialize the client for a public API (e.g., JSONPlaceholder).
# In a production environment, ensure ssl_verify is True and handle authentication.
client = SimpleRestClient(
    api_root_url="https://jsonplaceholder.typicode.com/",
    ssl_verify=True, # Always prefer True in production. Set to False only for testing untrusted SSL.
    headers={
        "Authorization": f"Bearer {os.environ.get('API_KEY', '')}"
    } # Example for authentication token
)

# Add the user resource to the client, making it accessible via client.users
client.add_resource(resource_name="users", resource_class=UserResource)

try:
    # Example 1: Get all users
    print("\nFetching all users...")
    all_users = client.users.list()
    print(f"First user: {all_users[0]['name']}")

    # Example 2: Get a single user by ID
    print("\nFetching user with ID 1...")
    user_1 = client.users.get(1)
    print(f"User 1 name: {user_1['name']}, email: {user_1['email']}")

    # Example 3: Create a new user (NOTE: JSONPlaceholder is a fake API; this won't persist)
    print("\nCreating a new user (API will return 201, but not persist on JSONPlaceholder)...")
    new_user_data = {
        "name": "Jane Doe",
        "username": "janedoe",
        "email": "jane.doe@example.com",
        "address": {"street": "123 Main St"}
    }
    created_user = client.users.create(new_user_data)
    print(f"Created user ID: {created_user.get('id', 'N/A')}, Name: {created_user['name']}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →