court_listener package

Subpackages

Submodules

court_listener.base module

class court_listener.base.CourtListener(api_key, base_url='https://www.courtlistener.com/api/rest/v3', prettify=True)[source]

Bases: object

A client for interacting with the Court Listener API.

This class allows users to make authenticated requests to the Court Listener API and retrieve JSON data, which can be optionally prettified.

api_key

The API key for authenticating requests.

Type:

str

prettify

Flag indicating whether to prettify the JSON output (default is True).

Type:

bool

headers

HTTP headers for the API requests, including Authorization and Accept.

Type:

dict

base_url

The base URL for the Court Listener API (default is “https://www.courtlistener.com/api/rest/v3”).

Type:

str

get_data(**kwargs)
get_pretty_json(response)[source]

Converts the API response to JSON format, optionally prettifying the output.

Parameters:

response (requests.Response or dict) – The response object returned by the API or a dictionary in case of error.

Returns:

A JSON-formatted string, prettified if the prettify attribute is True.

Return type:

str

Example

>>> response = court_listener.get_data('opinions/')
>>> print(court_listener.get_pretty_json(response))

court_listener.error_handling module

court_listener.error_handling.handle_api_errors(func)[source]

A decorator to handle API errors in HTTP requests.

This decorator wraps around a function that makes HTTP requests using the requests library. It handles various exceptions that can occur during the request process and returns a detailed error message when an exception is raised.

The decorator catches specific exceptions like HTTPError, ConnectionError, Timeout, and RequestException, as well as any other general exceptions, returning a dictionary with error details instead of allowing the exception to propagate.

Parameters:

func (function) – The function to be decorated, typically one that makes an HTTP request.

Returns:

A wrapped function that executes the original function and handles exceptions,

returning a dictionary with error details if an exception occurs.

Return type:

function

Example

>>> @handle_api_errors
>>> def get_data_from_api():
>>>     response = requests.get('https://api.example.com/data')
>>>     return response
>>>
>>> response = get_data_from_api()
>>> if 'error' in response:
>>>     print(response['message'])
>>> else:
>>>     print(response.json())

Module contents