Saltar a contenido

Actions

Clase base de todos los recursos: ofrece read, list, create, update, delete y add_attachment. También incluye validate_id, la función que valida los IDs antes de construir la URL (ver Seguridad).

alegrapy.actions.Actions

Base class for all resource endpoints in the Alegra API.

Provides common CRUD operations and attachment uploads.

client property writable

Get the client instance for this resource.

__init__(client=None)

Initialize the resource.

Parameters:

Name Type Description Default
client AlegraClient

The AlegraClient instance to use. If None, the global default_client is used.

None

read(id, **kwargs)

Search and retrieve a single resource by its ID.

Parameters:

Name Type Description Default
id int or str

The unique identifier of the resource.

required
**kwargs Any

Additional query parameters.

{}

Returns:

Name Type Description
Any Any

The parsed JSON response representing the resource.

Raises:

Type Description
ValueError

If the ID is not a valid resource identifier.

list(start=0, limit=30, **kwargs)

Search multiple results and return a list of resources.

Parameters:

Name Type Description Default
start int

Initial position index for pagination (default 0).

0
limit int

Maximum number of results to return (max 30, default 30).

30
**kwargs Any

Additional query parameters.

{}

Returns:

Type Description
List[Any]

List[Any]: A list of matching resources.

iter_all(max_items=None, page_size=30, **filters)

Iterate over every matching resource, fetching one page at a time.

Pages are requested lazily with start/limit through :meth:list, so the full result is never held in memory. The iteration stops on an empty or short page, when max_items records have been yielded, or if the API ignores start (detected when a full page repeats the previous page's first id).

Offset pagination over a collection that changes while you walk it can skip or repeat records. Ascending id (the default ordering) is the most stable order for "process everything" jobs.

Parameters:

Name Type Description Default
max_items int

Maximum records to yield. 0 yields nothing and sends no request.

None
page_size int

Records per request, between 1 and 30 (default 30).

30
**filters Any

Query parameters forwarded to :meth:list. start, limit and metadata are rejected because the iterator owns pagination.

{}

Yields:

Name Type Description
Any Any

Each resource, in the order returned by the API.

Raises:

Type Description
ValueError

If page_size, max_items or the filters are invalid; no request is sent.

AlegraAPIError

If a page does not have the expected list shape, or if any request fails (the original typed error propagates unchanged).

create(params, **kwargs)

Send information to create a new resource in Alegra.

Parameters:

Name Type Description Default
params dict

The fields and values to create the resource with.

required
**kwargs Any

Additional query parameters.

{}

Returns:

Name Type Description
Any Any

The parsed JSON response from the server.

update(id, params, **kwargs)

Update an existing record in Alegra by its ID.

Parameters:

Name Type Description Default
id int or str

The unique identifier of the resource to update.

required
params dict

The fields and values to update.

required
**kwargs Any

Additional query parameters.

{}

Returns:

Name Type Description
Any Any

The parsed JSON response from the server.

Raises:

Type Description
ValueError

If the ID is not a valid resource identifier.

delete(id, **kwargs)

Delete an existing resource by its ID.

Parameters:

Name Type Description Default
id int or str

The unique identifier of the resource to delete.

required
**kwargs Any

Additional query parameters.

{}

Returns:

Name Type Description
Any Any

The parsed JSON response from the server.

Raises:

Type Description
ValueError

If the ID is not a valid resource identifier.

add_attachment(id, file_path, field_name='file')

Upload an attachment to this resource by its ID.

Parameters:

Name Type Description Default
id int or str

The unique identifier of the resource.

required
file_path str

Path to the local file to upload.

required
field_name str

Name of the form field expected by the server (default 'file').

'file'

Returns:

Name Type Description
Any Any

The parsed JSON response from the server.

Raises:

Type Description
ValueError

If the ID is not a valid resource identifier.

alegrapy.actions.validate_id(id)

Validate a resource ID before it is placed in a URL path.

Parameters:

Name Type Description Default
id int or str

A non-negative integral number (bool excluded), or a string of letters, digits, '_' or '-'.

required

Returns:

Type Description
Union[int, str]

int or str: The ID; integral numbers are normalized to a plain int.

Raises:

Type Description
ValueError

If the ID could alter the URL path or query (e.g. contains '/', '.', '?').