{"id":9304,"library":"simple-rest-client","title":"Simple REST Client","description":"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.","status":"active","version":"1.2.1","language":"en","source_language":"en","source_url":"https://github.com/allisson/python-simple-rest-client","tags":["rest","api client","http client","requests","restful"],"install":[{"cmd":"pip install simple-rest-client","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"SimpleRestClient","correct":"from simple_rest_client.client import SimpleRestClient"},{"symbol":"Resource","correct":"from simple_rest_client.resource import Resource"},{"symbol":"ClientError","correct":"from simple_rest_client.exceptions import ClientError"}],"quickstart":{"code":"import os\nfrom simple_rest_client.client import SimpleRestClient\nfrom simple_rest_client.resource import Resource\n\n# Define a resource for users. Basic CRUD actions (list, get, create, update, delete) are\n# automatically available if not overridden or custom actions defined.\nclass UserResource(Resource):\n    pass\n\n# Initialize the client for a public API (e.g., JSONPlaceholder).\n# In a production environment, ensure ssl_verify is True and handle authentication.\nclient = SimpleRestClient(\n    api_root_url=\"https://jsonplaceholder.typicode.com/\",\n    ssl_verify=True, # Always prefer True in production. Set to False only for testing untrusted SSL.\n    headers={\n        \"Authorization\": f\"Bearer {os.environ.get('API_KEY', '')}\"\n    } # Example for authentication token\n)\n\n# Add the user resource to the client, making it accessible via client.users\nclient.add_resource(resource_name=\"users\", resource_class=UserResource)\n\ntry:\n    # Example 1: Get all users\n    print(\"\\nFetching all users...\")\n    all_users = client.users.list()\n    print(f\"First user: {all_users[0]['name']}\")\n\n    # Example 2: Get a single user by ID\n    print(\"\\nFetching user with ID 1...\")\n    user_1 = client.users.get(1)\n    print(f\"User 1 name: {user_1['name']}, email: {user_1['email']}\")\n\n    # Example 3: Create a new user (NOTE: JSONPlaceholder is a fake API; this won't persist)\n    print(\"\\nCreating a new user (API will return 201, but not persist on JSONPlaceholder)...\")\n    new_user_data = {\n        \"name\": \"Jane Doe\",\n        \"username\": \"janedoe\",\n        \"email\": \"jane.doe@example.com\",\n        \"address\": {\"street\": \"123 Main St\"}\n    }\n    created_user = client.users.create(new_user_data)\n    print(f\"Created user ID: {created_user.get('id', 'N/A')}, Name: {created_user['name']}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Migrate existing `get_action` usages to define `Resource` classes with custom actions or use default CRUD methods via `add_resource`. This provides a more structured and extensible way to interact with your API.","message":"Prior to version 1.0.0, the primary method for defining API interactions was `client.get_action()`. Version 1.0.0 introduced `client.add_resource()` and the `Resource` class, which are now the recommended and more powerful approach for CRUD operations and custom endpoints.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Always ensure `ssl_verify=True` (the default) in production environments. If connecting to internal APIs with self-signed certificates, configure your system to trust the CA certificate or pass the certificate path to `ssl_verify`.","message":"The `ssl_verify=False` parameter, often seen in examples or used during development, disables SSL certificate verification. Using this in a production environment makes connections vulnerable to man-in-the-middle attacks.","severity":"gotcha","affected_versions":"All"},{"fix":"Wrap API calls in `try...except simple_rest_client.exceptions.ClientError as e:` blocks (or more specific HTTP exception types) to gracefully handle API errors. Inspect `e.status_code` and `e.message` for details.","message":"By default, `simple-rest-client` raises exceptions (e.g., `simple_rest_client.exceptions.ClientError`, `BadRequestError`, `UnauthorizedError`) for non-2xx HTTP responses. Users unfamiliar with this might not implement proper error handling, leading to unhandled exceptions.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Double-check the `api_root_url` and the resource/action paths being used. Verify the API documentation for correct endpoints and ensure no typos.","cause":"The requested endpoint or resource path does not exist on the API server, or the base URL (`api_root_url`) is incorrect.","error":"simple_rest_client.exceptions.ClientError: 404 Client Error: Not Found for url: ..."},{"fix":"Verify your network connection and the correctness of the `api_root_url`. Check if the API server is running, accessible from your environment, and correctly configured to accept connections.","cause":"A network connectivity issue, incorrect `api_root_url` (e.g., wrong port or hostname), or the API server is unavailable or actively refusing the connection.","error":"requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))"},{"fix":"Ensure you have called `client.add_resource(\"my_resource\", MyResourceClass)` or `client.add_action(\"my_action\", MyActionClass)` *before* trying to access `client.my_resource` or `client.my_action`.","cause":"You attempted to access a resource (e.g., `client.my_resource`) that has not been registered with the `SimpleRestClient` instance using `add_resource` or `add_action`.","error":"AttributeError: 'SimpleRestClient' object has no attribute 'my_resource'"}]}