Skip to content

oarepo_oaipmh_harvester.oai_record.results

oarepo_oaipmh_harvester.oai_record.results

Results for the oai_records service.

OAIRecordItem

Bases: RecordItem

Single harvested record result.

Source code in oarepo_oaipmh_harvester/oai_record/results.py
class OAIRecordItem(RecordItem):
    """Single harvested record result."""

    _data: dict | None

    def __init__(  # noqa PLR0913,PLR0917
        self,
        service: OAIRecordService,
        identity: Identity,
        oai_record: OAIRecordAggregate,
        errors: list[Any] | None = None,
        links_tpl: LinksTemplate | None = None,
        schema: ServiceSchemaWrapper | None = None,
        **_kwargs: Any,
    ):
        """Create OAIRecordItem."""
        self._data = None
        self._errors = errors
        self._identity = identity
        self._oai_record = oai_record
        self._service = service
        self._links_tpl = links_tpl
        self._schema = schema or service.schema

    @property
    def id(self) -> str:
        """ID of the record."""
        return str(self._oai_record.id)

    def __getitem__(self, key: str):
        """Get a field from the serialized record data."""
        return self.data[key]

    @property
    def links(self) -> dict[str, str]:
        """Get links for this result item."""
        if self._links_tpl:
            return self._links_tpl.expand(self._identity, self._oai_record)
        return {}

    @property
    def _obj(self) -> OAIRecordAggregate:
        """Return the object to dump."""
        return self._oai_record

    @property
    def data(self) -> dict[str, Any]:
        """Serialized record, dumped on first access."""
        if self._data:
            return self._data

        self._data = self._schema.dump(
            self._obj,
            context={
                "identity": self._identity,
                "record": self._oai_record,
            },
        )

        if self._links_tpl:
            self._data["links"] = self.links

        return self._data

    @property
    def errors(self) -> list[Any] | None:
        """Get the errors."""
        return self._errors

    def to_dict(self) -> dict[str, Any]:
        """Serialize the record result, including errors."""
        res = self.data
        if self._errors:
            res["errors"] = self._errors
        return res

id: str property

ID of the record.

Get links for this result item.

data: dict[str, Any] property

Serialized record, dumped on first access.

errors: list[Any] | None property

Get the errors.

__init__(service: OAIRecordService, identity: Identity, oai_record: OAIRecordAggregate, errors: list[Any] | None = None, links_tpl: LinksTemplate | None = None, schema: ServiceSchemaWrapper | None = None, **_kwargs: Any)

Create OAIRecordItem.

Source code in oarepo_oaipmh_harvester/oai_record/results.py
def __init__(  # noqa PLR0913,PLR0917
    self,
    service: OAIRecordService,
    identity: Identity,
    oai_record: OAIRecordAggregate,
    errors: list[Any] | None = None,
    links_tpl: LinksTemplate | None = None,
    schema: ServiceSchemaWrapper | None = None,
    **_kwargs: Any,
):
    """Create OAIRecordItem."""
    self._data = None
    self._errors = errors
    self._identity = identity
    self._oai_record = oai_record
    self._service = service
    self._links_tpl = links_tpl
    self._schema = schema or service.schema

__getitem__(key: str)

Get a field from the serialized record data.

Source code in oarepo_oaipmh_harvester/oai_record/results.py
def __getitem__(self, key: str):
    """Get a field from the serialized record data."""
    return self.data[key]

to_dict() -> dict[str, Any]

Serialize the record result, including errors.

Source code in oarepo_oaipmh_harvester/oai_record/results.py
def to_dict(self) -> dict[str, Any]:
    """Serialize the record result, including errors."""
    res = self.data
    if self._errors:
        res["errors"] = self._errors
    return res

OAIRecordList

Bases: RecordList

List of harvested record results.

Source code in oarepo_oaipmh_harvester/oai_record/results.py
class OAIRecordList(RecordList):
    """List of harvested record results."""

    @property
    @override
    def hits(self) -> Iterator[dict[str, Any]]:
        """Iterator over the hits."""
        oai_record_cls = self._service.record_cls

        for hit in self._results:
            # load dump
            oai_record = oai_record_cls.loads(hit.to_dict())
            schema = self._service.schema

            # project the oai_record
            projection = schema.dump(
                oai_record,
                context={
                    "identity": self._identity,
                    "record": oai_record,
                },
            )

            # inject the links
            if self._links_item_tpl:
                projection["links"] = self._links_item_tpl.expand(self._identity, oai_record)

            yield projection

    def to_dict(self) -> dict[str, Any]:
        """Return result as a dictionary."""
        # TODO: This part should imitate the result item above. I.e. add a
        # "data" property which uses a ServiceSchema to dump the entire object.
        res = {
            "hits": {
                "hits": list(self.hits),
                "total": self.total,
            }
        }

        if self.aggregations:
            res["aggregations"] = self.aggregations

        if self._params:
            res["sortBy"] = self._params["sort"]
            if self._links_tpl:
                res["links"] = self._links_tpl.expand(self._identity, self.pagination)

        return res

hits: Iterator[dict[str, Any]] property

Iterator over the hits.

to_dict() -> dict[str, Any]

Return result as a dictionary.

Source code in oarepo_oaipmh_harvester/oai_record/results.py
def to_dict(self) -> dict[str, Any]:
    """Return result as a dictionary."""
    # TODO: This part should imitate the result item above. I.e. add a
    # "data" property which uses a ServiceSchema to dump the entire object.
    res = {
        "hits": {
            "hits": list(self.hits),
            "total": self.total,
        }
    }

    if self.aggregations:
        res["aggregations"] = self.aggregations

    if self._params:
        res["sortBy"] = self._params["sort"]
        if self._links_tpl:
            res["links"] = self._links_tpl.expand(self._identity, self.pagination)

    return res