Skip to content

Writes

client.post(), put(), patch() and delete() return a WriteResult. client.bulk() returns a BulkResult. See the writes guide.

WriteResult dataclass

WriteResult(dry_run: bool, message: str | None, data: Any, response: Response)

What a write returned.

Attributes:

Name Type Description
dry_run bool

True if T3 only checked the request: format, required fields and types, not whether the records it names exist. Nothing reached Metrc.

message str | None

The server's message: "Dry run" or "Success" for Metrc writes.

data Any

The parsed response body.

response Response

The httpx2.Response.

dry_run instance-attribute

dry_run: bool

message instance-attribute

message: str | None

data instance-attribute

data: Any

response class-attribute instance-attribute

response: Response = field(repr=False)

BulkResult dataclass

BulkResult(endpoint: str, rows: list[RowResult], submitted: bool = False)

Every row's outcome, in input order.

endpoint instance-attribute

endpoint: str

rows instance-attribute

rows: list[RowResult]

submitted class-attribute instance-attribute

submitted: bool = False

invalid property

invalid: list[RowResult]

Rows that failed validation (the dry run).

failed property

failed: list[RowResult]

Rows whose submit the server rejected.

ok property

ok: bool

True when no row was invalid or failed.

by_status

by_status(status: RowStatus) -> list[RowResult]

The rows with one status.

counts

counts() -> dict[str, int]

How many rows have each status.

summary

summary() -> str

One line, e.g. POST /v2/items/discontinue: 1 failed validation, 3 submitted.

to_csv

to_csv(path: str | Path) -> Path

Write one line per row: index, status, message, and the row as JSON.

RowResult dataclass

RowResult(index: int, row: Mapping[str, Any], status: RowStatus = 'not_sent', message: str | None = None, data: Any = None, error: APIError | None = None)

One row of a bulk write.

status is valid (passed the dry run's static checks, not submitted), invalid, submitted, failed or not_sent.

index instance-attribute

index: int

row instance-attribute

row: Mapping[str, Any]

status class-attribute instance-attribute

status: RowStatus = 'not_sent'

message class-attribute instance-attribute

message: str | None = None

data class-attribute instance-attribute

data: Any = None

error class-attribute instance-attribute

error: APIError | None = field(default=None, repr=False)

bulk

bulk(client: T3Client, endpoint: str, rows: Iterable[Mapping[str, Any]], *, license: str | None = None, method: str | None = None, submit: bool | Literal['ask'] = False, confirm: bool = False, on_error: OnError = 'stop', log_to: str | Path | None = None, progress: bool | None = None, path_params: Mapping[str, Any] | None = None, dynamic: bool = False, batch_size: int = 1) -> BulkResult

Send many writes safely: validate every row, then submit.

For Metrc write routes:

  1. Every row is sent as a dry run and its result recorded. A dry run checks format, required fields and types, not whether the records a row names exist, so valid means "passed validation", not "Metrc will accept it".
  2. submit=False stops here. submit="ask" shows a summary (and any failures) and asks whether to submit, default no. submit=True carries on without asking unless some rows failed; then, in a terminal, it shows the failures and asks whether to submit only the valid rows. on_error="skip" submits them without asking.
  3. Each valid row is sent with submit=true, in order. With on_error="stop", the first failed submit stops the rest.

Each row is one object. On routes whose body is a list (most Metrc writes), rows are wrapped in a list for you.

Routes with no dry run (immediate_write) can't be validated first, so they need confirm=True ("I know this takes effect at once"), or submit="ask" to ask a person before sending.

Parameters:

Name Type Description Default
client T3Client

The client to send with.

required
endpoint str

The write route's path (or endpoint ID).

required
rows Iterable[Mapping[str, Any]]

One mapping per object to write.

required
license str | None

Fills licenseNumber. Defaults to the client's license when the route requires one.

None
method str | None

Picks the operation when a path has several write methods.

None
submit bool | Literal['ask']

False validates only, True submits the valid rows, "ask" asks first. "ask" raises NotInteractive without a terminal, before sending anything, so a scheduled job can't hang or quietly submit nothing.

False
confirm bool

Required for routes with no dry run, unless submit="ask".

False
on_error OnError

"stop" (the default) or "skip", as described above.

'stop'
log_to str | Path | None

Write the results to this CSV file.

None
progress bool | None

Show progress bars. Defaults to on when stderr is a terminal.

None
path_params Mapping[str, Any] | None

Values for the route's path parameters.

None
dynamic bool

Also ask T3 for the route's dynamic checks (applyDynamicValidation). What they cover depends on the route.

False
batch_size int

Rows per request, on routes whose body is a list. A request that fails marks every row in it with the same error, so the default of 1 keeps each row's error separate.

1

Raises:

Type Description
ValueError

a bad submit, on_error or batch_size; an immediate route without confirm=True; batch_size above 1 on a route that takes one object; dynamic=True on a route without dynamic validation.

NotInteractive

submit="ask" without a terminal.

AuthError

the credential was rejected. It stops the run whatever on_error says.