def test_context_manager(identity: PrivateIdentity) -> None: """Close connection when leaving the context manager.""" with mock.patch.object(HttpClient, "close") as mocked_close: with Client("https://example.org", identity) as _: pass mocked_close.assert_called_once_with()
def read_endorsement( ctx: click.core.Context, identity: str, endorser_id: str, entity_type: str, entity_id: str, asset_type_id: str, claim: str, ) -> None: """Read specific endorsement of a claim (identity, asset type, unique asset).""" if entity_type.lower() == "asset": entity = Asset(asset_type_id=asset_type_id, asset_id=entity_id) else: raise NotImplementedError # pragma: no cover id = _load_identity(identity) client = Client(ctx.obj["url"], id) response = client.get( entity, claim=claim.encode(), endorser_id=endorser_id, request_id=ctx.obj["request_id"], ) print(f"{entity!r}") print(f"{claim!r}") print(f"endorser: {response.endorser_id!r}") # type: ignore[union-attr]
def main() -> None: """Create identity, asset type and register products.""" product_data = read_product_data("nfc-tags.csv") # Usually we would store the identity (ID and key) on a safe place. manufacturer = PrivateIdentity( CryptoProtocol.SHA256WithECDSA.generate_private_key()) with Client(cfg.iov42_platform["url"], manufacturer) as client: # Create the identity client.put(manufacturer.public_identity) print(f"Created manufacturer identity: {manufacturer.identity_id}") # Create the asset typ used for the NFC tags. tag_type = AssetType() client.put(tag_type) print(f"Created tag asset type: {tag_type}") # Register the NFC tags on the distributed ledger in parallel. with ThreadPoolExecutor(max_workers=20) as executor: _ = executor.map( register_product, [client] * len(product_data), [tag_type] * len(product_data), product_data, ) executor.shutdown(wait=True)
def create_asset(ctx: click.core.Context, identity: str, asset_type_id: str, asset_id: str) -> None: """Create an asset.""" asset = Asset(asset_type_id=asset_type_id, asset_id=asset_id) id = _load_identity(identity) client = Client(ctx.obj["url"], id) _ = client.put(asset, request_id=ctx.obj["request_id"]) print(f"asset_id: {asset}")
def create_asset_type(ctx: click.core.Context, identity: str, asset_type_id: str, scale: int) -> None: """Create an asset type.""" asset_type = AssetType(asset_type_id) id = _load_identity(identity) client = Client(ctx.obj["url"], id) _ = client.put(asset_type, request_id=ctx.obj["request_id"]) print(f"asset_type_id: {asset_type_id}")
def test_propagate_close(identity: PrivateIdentity) -> None: """Propagate close to the wrapped HTTP client implementation.""" with mock.patch.object(HttpClient, "close") as mocked_close: client = Client("https://example.org", identity) client.close() mocked_close.assert_called_once_with()
def test_close_on_del(identity: PrivateIdentity) -> None: """Resources are freed on deleting the object.""" with mock.patch.object(HttpClient, "close") as mocked_close: client = Client("https://example.org", identity) del client mocked_close.assert_called_once_with()
def create_identity(ctx: click.core.Context, identity_id: str, crypto_protocol: str) -> None: """Create an identity.""" private_key = generate_private_key(crypto_protocol) identity = PrivateIdentity(private_key, identity_id) client = Client(ctx.obj["url"], identity) _ = client.put(identity.public_identity, request_id=ctx.obj["request_id"]) print(_identity_json(identity))
def create_endorsement( ctx: click.core.Context, identity: str, entity_type: str, entity_id: str, asset_type_id: str, claims: List[str], ) -> None: """Endorse claims about an entity (identity, asset type, unique asset).""" if entity_type.lower() == "asset": entity = Asset(asset_type_id=asset_type_id, asset_id=entity_id) else: raise NotImplementedError # pragma: no cover id = _load_identity(identity) client = Client(ctx.obj["url"], id) claims_bytes = [c.encode() for c in claims] response = client.put(entity, claims=claims_bytes, endorse=True, request_id=ctx.obj["request_id"]) print(f"claims on {entity}: {entity_id}") print(f"affected resources: {response.resources}" ) # type: ignore[union-attr]
def bob_client(bob: PrivateIdentity) -> Client: """Returns Bob's client.""" client = Client(IOV42_TEST_SERVICE, bob) client.put(bob.public_identity) return client
def alice_client(alice: PrivateIdentity) -> Client: """Creates Alice's identity on an iov42 platform.""" client = Client(IOV42_TEST_SERVICE, alice) client.put(alice.public_identity) return client
def client(identity: PrivateIdentity) -> Client: """Client for easy access to iov42 platform.""" return Client(PLATFORM_URL, identity)