From 5fdb7e2408e3fb2bac3e67ad020fd16466879582 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Thu, 14 Mar 2024 17:58:24 +0100 Subject: [PATCH] Fix nitpicks (formatting and typing) --- da/api/common.py | 47 +++++++++++++++++++++++++++------------------ da/api/test_flow.py | 6 ++++-- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/da/api/common.py b/da/api/common.py index 0a55a7f..0d8afc9 100644 --- a/da/api/common.py +++ b/da/api/common.py @@ -1,41 +1,50 @@ from abc import ABC, abstractmethod from dataclasses import dataclass -from typing import Optional, List +from typing import Optional, List, Sequence +from da.common import Certificate from da.verifier import DABlob + @dataclass class Metadata: + # index of VID certificate blob index: int - app_id: int + # app identifier + app_id: bytes + class BlobStore(ABC): @abstractmethod - def add(certificate, metadata): + def add(self, certificate: Certificate, metadata: Metadata): + """ + Raises: ValueError if there is already a registered certificate fot the given metadata + """ pass @abstractmethod - def get_multiple(app_id, indexes) -> List[DABlob]: + def get_multiple(self, app_id: bytes, indexes: Sequence[int]) -> List[Optional[DABlob]]: pass -class Api: + +class DAApi: def __init__(self, bs: BlobStore): self.store = bs - """ - Write method should be used by a service that is able to retrieve verified certificates - from the latest Block. Once a certificate is retrieved, api creates a relation between - the blob of an original data, certificate and index for the app_id of the certificate. - """ - def write(self, certificate, metadata): + def write(self, certificate: Certificate, metadata: Metadata): + """ + Write method should be used by a service that is able to retrieve verified certificates + from the latest Block. Once a certificate is retrieved, api creates a relation between + the blob of an original data, certificate and index for the app_id of the certificate. + Raises: ValueError if there is already a registered certificate for a given metadata + """ self.store.add(certificate, metadata) - return - """ - Read method should accept only `app_id` and a list of indexes. The returned list of - blobs should be ordered in the same sequence as `indexes` in a request. - If node does not have the blob for some indexes, then it should add None object as an - item. - """ - def read(self, app_id, indexes) -> Optional[List[Optional[DABlob]]]: + def read(self, app_id, indexes) -> List[Optional[DABlob]]: + """ + Read method should accept only `app_id` and a list of indexes. The returned list of + blobs should be ordered in the same sequence as `indexes` in a request. + If node does not have the blob for some indexes, then it should add None object as an + item. + """ return self.store.get_multiple(app_id, indexes) diff --git a/da/api/test_flow.py b/da/api/test_flow.py index 1064643..bf88fc1 100644 --- a/da/api/test_flow.py +++ b/da/api/test_flow.py @@ -3,10 +3,12 @@ from collections import defaultdict from da.api.common import * + @dataclass class MockCertificate: cert_id: int + class MockStore(BlobStore): def __init__(self): self.blob_store = {} @@ -41,7 +43,7 @@ class TestFlow(TestCase): mock_store = MockStore() mock_store.populate(expected_blob, cert_id) - api = Api(mock_store) + api = DAApi(mock_store) api.write(cert_id, mock_meta) blobs = api.read(app_id, [idx]) @@ -58,7 +60,7 @@ class TestFlow(TestCase): mock_store = MockStore() mock_store.populate(expected_blob, cert_id) - api = Api(mock_store) + api = DAApi(mock_store) api.write(cert_id, mock_meta) with self.assertRaises(ValueError):