Skip to content

bulk_transactions

stac_fastapi.extensions.bulk_transactions

Fields extension module.

AsyncBaseBulkTransactionsClient

Bases: ABC

BulkTransactionsClient.

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@attr.s
class AsyncBaseBulkTransactionsClient(abc.ABC):
    """BulkTransactionsClient."""

    @abc.abstractmethod
    async def bulk_item_insert(
        self,
        items: Items,
        **kwargs,
    ) -> BulkTransaction | Response:
        """Bulk creation of items.

        Args:
            items: list of items.

        Returns:
            Message indicating the status of the insert.
        """
        raise NotImplementedError

bulk_item_insert abstractmethod async

bulk_item_insert(items: Items, **kwargs) -> BulkTransaction | Response

Bulk creation of items.

Parameters:

  • items (Items) –

    list of items.

Returns:

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@abc.abstractmethod
async def bulk_item_insert(
    self,
    items: Items,
    **kwargs,
) -> BulkTransaction | Response:
    """Bulk creation of items.

    Args:
        items: list of items.

    Returns:
        Message indicating the status of the insert.
    """
    raise NotImplementedError

BaseBulkTransactionsClient

Bases: ABC

BulkTransactionsClient.

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@attr.s
class BaseBulkTransactionsClient(abc.ABC):
    """BulkTransactionsClient."""

    @staticmethod
    def _chunks(lst, n):
        """Yield successive n-sized chunks from list.

        https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
        """
        for i in range(0, len(lst), n):
            yield lst[i : i + n]

    @abc.abstractmethod
    def bulk_item_insert(
        self,
        items: Items,
        chunk_size: int | None = None,
        **kwargs,
    ) -> BulkTransaction | Response:
        """Bulk creation of items.

        Args:
            items: list of items.
            chunk_size: number of items processed at a time.

        Returns:
            Message indicating the status of the insert.
        """
        raise NotImplementedError

bulk_item_insert abstractmethod

bulk_item_insert(
    items: Items, chunk_size: int | None = None, **kwargs
) -> BulkTransaction | Response

Bulk creation of items.

Parameters:

  • items (Items) –

    list of items.

  • chunk_size (int | None, default: None ) –

    number of items processed at a time.

Returns:

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@abc.abstractmethod
def bulk_item_insert(
    self,
    items: Items,
    chunk_size: int | None = None,
    **kwargs,
) -> BulkTransaction | Response:
    """Bulk creation of items.

    Args:
        items: list of items.
        chunk_size: number of items processed at a time.

    Returns:
        Message indicating the status of the insert.
    """
    raise NotImplementedError

BulkTransaction

Bases: TypedDict

Bulk Transaction Response.

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
26
27
28
29
30
31
32
class BulkTransaction(TypedDict):
    """Bulk Transaction Response."""

    received: int
    success: int
    skipped: int
    errors: list[TransactionError]

BulkTransactionExtension

Bases: ApiExtension

Bulk Transaction Extension.

Bulk Transaction extension adds the POST /collections/{collection_id}/bulk_items endpoint to the application for efficient bulk insertion of items. The input to this is an object with an attribute "items", that has a value that is an object with a group of attributes that are the ids of each Item, and the value is the Item entity.

Optionally, clients can specify a "method" attribute that is either "insert" or "upsert". If "insert", then the items will be inserted if they do not exist, and an error will be returned if they do. If "upsert", then the items will be inserted if they do not exist, and updated if they do. This defaults to "insert".

{
    "items": {
        "id1": { "type": "Feature", ... },
        "id2": { "type": "Feature", ... },
        "id3": { "type": "Feature", ... }
    },
    "method": "insert"
}
Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
@attr.s
class BulkTransactionExtension(ApiExtension):
    """Bulk Transaction Extension.

    Bulk Transaction extension adds the `POST
    /collections/{collection_id}/bulk_items` endpoint to the application for
    efficient bulk insertion of items. The input to this is an object with an
    attribute  "items", that has a value that is an object with a group of
    attributes that are the ids of each Item, and the value is the Item entity.

    Optionally, clients can specify a "method" attribute that is either "insert"
    or "upsert". If "insert", then the items will be inserted if they do not
    exist, and an error will be returned if they do. If "upsert", then the items
    will be inserted if they do not exist, and updated if they do. This defaults
    to "insert".

        {
            "items": {
                "id1": { "type": "Feature", ... },
                "id2": { "type": "Feature", ... },
                "id3": { "type": "Feature", ... }
            },
            "method": "insert"
        }
    """

    client: AsyncBaseBulkTransactionsClient | BaseBulkTransactionsClient = attr.ib()
    conformance_classes: list[str] = attr.ib(default=list())
    schema_href: str | None = attr.ib(default=None)
    route_dependencies: Sequence[Depends] | None = attr.ib(default=None)
    enable_response_models: bool = attr.ib(default=True)

    def register(self, app: FastAPI) -> None:
        """Register the extension with a FastAPI application.

        Args:
            app: target FastAPI application.

        Returns:
            None
        """
        items_request_model = create_request_model("Items", base_model=Items)

        router = APIRouter(prefix=app.state.router_prefix)
        router.add_api_route(
            name="Bulk Create Item",
            path="/collections/{collection_id}/bulk_items",
            response_model=BulkTransactionModel,
            responses={
                200: {
                    "content": {
                        "application/json": {},
                    },
                    "model": BulkTransactionModel,
                },
            },
            response_class=JSONResponse,
            response_model_exclude_unset=False,
            methods=["POST"],
            endpoint=create_async_endpoint(
                self.client.bulk_item_insert, items_request_model
            ),
            dependencies=self.route_dependencies,
        )
        app.include_router(router, tags=["Bulk Transaction Extension"])

register

register(app: FastAPI) -> None

Register the extension with a FastAPI application.

Parameters:

  • app (FastAPI) –

    target FastAPI application.

Returns:

  • None –

    None

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def register(self, app: FastAPI) -> None:
    """Register the extension with a FastAPI application.

    Args:
        app: target FastAPI application.

    Returns:
        None
    """
    items_request_model = create_request_model("Items", base_model=Items)

    router = APIRouter(prefix=app.state.router_prefix)
    router.add_api_route(
        name="Bulk Create Item",
        path="/collections/{collection_id}/bulk_items",
        response_model=BulkTransactionModel,
        responses={
            200: {
                "content": {
                    "application/json": {},
                },
                "model": BulkTransactionModel,
            },
        },
        response_class=JSONResponse,
        response_model_exclude_unset=False,
        methods=["POST"],
        endpoint=create_async_endpoint(
            self.client.bulk_item_insert, items_request_model
        ),
        dependencies=self.route_dependencies,
    )
    app.include_router(router, tags=["Bulk Transaction Extension"])

BulkTransactionMethod

Bases: StrEnum

Bulk Transaction Methods.

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
51
52
53
54
55
class BulkTransactionMethod(StrEnum):
    """Bulk Transaction Methods."""

    INSERT = "insert"
    UPSERT = "upsert"

BulkTransactionModel

Bases: BaseModel

Bulk Transaction Model.

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
42
43
44
45
46
47
48
class BulkTransactionModel(BaseModel):
    """Bulk Transaction Model."""

    received: int
    success: int
    skipped: int
    errors: list[TransactionErrorModel]

Items

Bases: BaseModel

A group of STAC Item objects, in the form of a dictionary from Item.id -> Item.

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
58
59
60
61
62
63
64
65
66
class Items(BaseModel):
    """A group of STAC Item objects, in the form of a dictionary from Item.id -> Item."""

    items: dict[str, Any]
    method: BulkTransactionMethod = BulkTransactionMethod.INSERT

    def __iter__(self):
        """Return an iterable of STAC Item objects."""
        return iter(self.items.values())

__iter__

__iter__()

Return an iterable of STAC Item objects.

Source code in stac_fastapi/extensions/stac_fastapi/extensions/bulk_transactions/bulk_transactions.py
64
65
66
def __iter__(self):
    """Return an iterable of STAC Item objects."""
    return iter(self.items.values())