Skip to content

http

Implementation of HTTP catalog store group.

NOTE: indicating the remote catalog that an entry should be sent to/read from is not properly supported through the store's interface, as it only applies to HTTP catalogs and not to local ones. To add a kinda hacky way to support this, the following assumptions are made of the values received by the CRUD methods:

  • Read/Delete: since we only receive an Entry id when indicating what to read/delete, the remote catalog id this should be read from will come bundled with the entry id, with a prefix, like this "remote_catalog_id--entry_id". The "remote_catalog.py" module is used to extract this, and could be used to bundle it as well.
  • Create/Edit: besides the same bundling as above, in the entry_id field of the new entry's header, the catalog_id in the new entry's header will have the actual remote catalog id, and this is the value used.

HTTPCatalogGroupEntryMapper

Bases: CatalogEntryMapper

HTTP mapper for the catalog group entry resource.

Source code in mlte/store/catalog/underlying/http.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
class HTTPCatalogGroupEntryMapper(CatalogEntryMapper):
    """HTTP mapper for the catalog group entry resource."""

    def __init__(self, storage: HttpResourceStorage) -> None:
        self.storage = storage
        """The HTTP storage access."""

    def create(
        self, new_entry: CatalogEntry, context: Any = None
    ) -> CatalogEntry:
        """This method assumes that the caller put in catalog_id the id of the remote catalog it would be stored into."""
        new_entry = remote_catalog.remove_remote_catalog_id(new_entry)
        response = self.storage.post(
            json=new_entry.to_json(),
            groups=_entry_group(new_entry.header.catalog_id),
        )
        return CatalogEntry(**response)

    def edit(
        self, edited_entry: CatalogEntry, context: Any = None
    ) -> CatalogEntry:
        """This method assumes that the caller put in catalog_id the id of the remote catalog it would be stored into."""
        edited_entry = remote_catalog.remove_remote_catalog_id(edited_entry)
        response = self.storage.put(
            json=edited_entry.to_json(),
            groups=_entry_group(edited_entry.header.catalog_id),
        )
        return CatalogEntry(**response)

    def read(
        self, catalog_and_entry_id: str, context: Any = None
    ) -> CatalogEntry:
        """This method assumes that the caller prefixed the entry_id with the local catalog id."""
        catalog_id, entry_id = remote_catalog.split_ids(catalog_and_entry_id)
        response = self.storage.get(
            id=entry_id, groups=_entry_group(catalog_id)
        )
        return CatalogEntry(**response)

    def list(self, context: Any = None) -> List[str]:
        entries = self.list_details()
        return [entry.header.identifier for entry in entries]

    def delete(
        self, catalog_and_entry_id: str, context: Any = None
    ) -> CatalogEntry:
        """This method assumes that the caller prefixed the entry_id with the local catalog id."""
        local_catalog_id, entry_id = remote_catalog.split_ids(
            catalog_and_entry_id
        )
        response = self.storage.delete(
            id=entry_id, groups=_entry_group(local_catalog_id)
        )
        return CatalogEntry(**response)

    def list_details(
        self,
        context: Any = None,
        limit: int = CatalogEntryMapper.DEFAULT_LIST_LIMIT,
        offset: int = 0,
    ) -> List[CatalogEntry]:
        # This is a bit hacky, in that we have a pseudo resource type "catalogs",
        # and a pseudo resource id "entry" to get all details of all entries.
        response = self.storage.send_command(
            MethodType.GET,
            id="entry",
            resource_type=f"{ResourceType.CATALOG.value}s",
        )
        return [CatalogEntry(**entry) for entry in response][
            offset : offset + limit
        ]

storage = storage instance-attribute

The HTTP storage access.

create(new_entry, context=None)

This method assumes that the caller put in catalog_id the id of the remote catalog it would be stored into.

Source code in mlte/store/catalog/underlying/http.py
106
107
108
109
110
111
112
113
114
115
def create(
    self, new_entry: CatalogEntry, context: Any = None
) -> CatalogEntry:
    """This method assumes that the caller put in catalog_id the id of the remote catalog it would be stored into."""
    new_entry = remote_catalog.remove_remote_catalog_id(new_entry)
    response = self.storage.post(
        json=new_entry.to_json(),
        groups=_entry_group(new_entry.header.catalog_id),
    )
    return CatalogEntry(**response)

delete(catalog_and_entry_id, context=None)

This method assumes that the caller prefixed the entry_id with the local catalog id.

Source code in mlte/store/catalog/underlying/http.py
142
143
144
145
146
147
148
149
150
151
152
def delete(
    self, catalog_and_entry_id: str, context: Any = None
) -> CatalogEntry:
    """This method assumes that the caller prefixed the entry_id with the local catalog id."""
    local_catalog_id, entry_id = remote_catalog.split_ids(
        catalog_and_entry_id
    )
    response = self.storage.delete(
        id=entry_id, groups=_entry_group(local_catalog_id)
    )
    return CatalogEntry(**response)

edit(edited_entry, context=None)

This method assumes that the caller put in catalog_id the id of the remote catalog it would be stored into.

Source code in mlte/store/catalog/underlying/http.py
117
118
119
120
121
122
123
124
125
126
def edit(
    self, edited_entry: CatalogEntry, context: Any = None
) -> CatalogEntry:
    """This method assumes that the caller put in catalog_id the id of the remote catalog it would be stored into."""
    edited_entry = remote_catalog.remove_remote_catalog_id(edited_entry)
    response = self.storage.put(
        json=edited_entry.to_json(),
        groups=_entry_group(edited_entry.header.catalog_id),
    )
    return CatalogEntry(**response)

read(catalog_and_entry_id, context=None)

This method assumes that the caller prefixed the entry_id with the local catalog id.

Source code in mlte/store/catalog/underlying/http.py
128
129
130
131
132
133
134
135
136
def read(
    self, catalog_and_entry_id: str, context: Any = None
) -> CatalogEntry:
    """This method assumes that the caller prefixed the entry_id with the local catalog id."""
    catalog_id, entry_id = remote_catalog.split_ids(catalog_and_entry_id)
    response = self.storage.get(
        id=entry_id, groups=_entry_group(catalog_id)
    )
    return CatalogEntry(**response)

HttpCatalogGroupStore

Bases: CatalogStore

A HTTP implementation of the MLTE catalog store group. Note that it is slightly different than the other implementations, in mapping to a store group through a backend, instead of a simple catalog store.

Source code in mlte/store/catalog/underlying/http.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class HttpCatalogGroupStore(CatalogStore):
    """
    A HTTP implementation of the MLTE catalog store group. Note that it is slightly
    different than the other implementations, in mapping to a store group through a
    backend, instead of a simple catalog store.
    """

    def __init__(
        self, *, uri: StoreURI, client: Optional[OAuthHttpClient] = None
    ) -> None:
        super().__init__(uri=uri)

        self.storage = HttpResourceStorage(
            uri=uri, resource_type=ResourceType.CATALOG, client=client
        )
        """HTTP storage."""

    def session(self) -> CatalogStoreSession:
        """
        Return a session handle for the store instance.
        :return: The session handle
        """
        return HttpCatalogGroupStoreSession(
            storage=self.storage, read_only=self.read_only
        )

storage = HttpResourceStorage(uri=uri, resource_type=(ResourceType.CATALOG), client=client) instance-attribute

HTTP storage.

session()

Return a session handle for the store instance.

Returns:

Type Description
CatalogStoreSession

The session handle

Source code in mlte/store/catalog/underlying/http.py
57
58
59
60
61
62
63
64
def session(self) -> CatalogStoreSession:
    """
    Return a session handle for the store instance.
    :return: The session handle
    """
    return HttpCatalogGroupStoreSession(
        storage=self.storage, read_only=self.read_only
    )

HttpCatalogGroupStoreSession

Bases: CatalogStoreSession

An HTTP implementation of the MLTE catalog store session.

Source code in mlte/store/catalog/underlying/http.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class HttpCatalogGroupStoreSession(CatalogStoreSession):
    """An HTTP implementation of the MLTE catalog store session."""

    def __init__(
        self, *, storage: HttpResourceStorage, read_only: bool = False
    ) -> None:
        self.storage = storage
        """HTTP storage"""

        self.read_only = read_only
        """Whether this is read only or not."""

        self.entry_mapper = HTTPCatalogGroupEntryMapper(storage=self.storage)
        """The mapper to entries CRUD."""

        storage.start_session()

    def close(self):
        # No closing needed.
        pass

entry_mapper = HTTPCatalogGroupEntryMapper(storage=(self.storage)) instance-attribute

The mapper to entries CRUD.

read_only = read_only instance-attribute

Whether this is read only or not.

storage = storage instance-attribute

HTTP storage