def encode(data: str) -> str: assert_type(data, str) hash_object = hashlib.sha256() hash_object.update(bytes(data, "utf-8")) multi = multihash.encode(hash_object.digest(), "sha2-256") result = multibase.encode("base58btc", multi) return result.decode("utf-8")
async def delete_record(context, id: str) -> str: assert_type(id, str) match = await match_table_query_id(context, id) pds = await pds_get_by_name(context, match.pds_type) result = await pds.delete(id) return result
async def save(self, record, metadata: dict, *, addition_meta={}) -> str: """ meta: { "table" - specifies the table name into which save the data "oca_schema_dri" } """ assert_type_or(record, str, dict) assert_type(metadata, dict) await self.update_token_when_expired() table = self.settings.get("repo") table = table if table is not None else "dip.data" meta = metadata dri_value = None if isinstance(record, str): dri_value = encode(record) elif isinstance(record, dict): dri_value = encode(json.dumps(record)) record = { "content": record, "dri": dri_value, "timestamp": int(round(time.time() * 1000)), # current time in milliseconds } LOGGER.debug("OYD save record %s metadata %s", record, meta) if meta.get("table") is not None: table = f"{table}.{meta.get('table')}" body = { "content": record, "dri": dri_value, "table_name": table, "mime_type": "application/json", } if addition_meta: body.update(addition_meta) async with ClientSession() as session: url = f"{self.api_url}/api/data" response = await session.post( url, headers={ "Authorization": "Bearer " + self.token["access_token"] }, json=body, ) result = await unpack_response(response) result = json.loads(result) LOGGER.debug("Result of POST request %s", result) return dri_value
async def pds_save(context, payload, metadata: str = "{}") -> str: assert_type_or(payload, str, dict) assert_type(metadata, str) active_pds_name = await pds_get_active_name(context) pds = await pds_get_by_name(context, active_pds_name) payload_id = await pds.save(payload, json.loads(metadata)) payload_id = await match_save_save_record_id(context, payload_id, active_pds_name) return payload_id
async def pds_load_string(context, id: str, *, with_meta: bool = False) -> str: assert_type(id, str) match = await match_table_query_id(context, id) pds = await pds_get_by_name(context, match.pds_type) result = await pds.load(id) if with_meta: return result else: return result["content"]
async def get_credential(self, credential_id: str) -> str: """ Get a stored credential. Args: credential_id: Credential id to retrieve """ try: credential = await pds_load(self.context, credential_id) except PDSNotFoundError as err: raise HolderError(err.roll_up) credential = json.dumps(credential) assert_type(credential, str) return credential
async def load(self, dri: str) -> dict: assert_type(dri, str) await self.update_token_when_expired() url = f"{self.api_url}/api/data/{dri}?p=dri&f=plain" async with ClientSession() as session: result = await session.get(url, headers={ "Authorization": "Bearer " + self.token["access_token"] }) result = await unpack_response(result) result_dict: dict = json.loads(result) return result_dict
async def pds_load(context, id: str, *, with_meta: bool = False) -> dict: assert_type(id, str) match = await match_table_query_id(context, id) pds = await pds_get_by_name(context, match.pds_type) result = await pds.load(id) try: result["content"] = json.loads(result["content"], object_pairs_hook=OrderedDict) except json.JSONDecodeError: pass except TypeError: pass if with_meta: return result else: return result["content"]