def execute(self, args: Any, output: Any) -> None: network = self.get_network(args.url, contract_names={'DIDContract': args.contract_name}) ddo = network.resolve_agent(args.asset_did, username=args.username, password=args.password) if not ddo: output.add_line(f'cannot resolve asset {args.asset_did}') return authentication = None if args.username or args.password: authentication = AuthenticationBasic(args.username, args.password) agent = RemoteAgent(ddo=ddo, authentication=authentication) asset = agent.download_asset(args.asset_did) asset_filename = args.filename if asset_filename is None: asset_filename = f'{asset.id}.dat' asset.save_to_file(asset_filename) output.add_line(f'saved asset {asset_filename}') output.add_line(f'asset metadata {asset.metadata}') output.set_value('asset_did', asset.did) output.set_value('filename', asset_filename) output.set_value('metadata', asset.metadata)
def execute(self, args: Any, output: Any) -> None: if not os.path.exists(args.filename): output.add_line(f'cannot find file {args.filename}') return network = self.get_network(args.url, contract_names={'DIDContract': args.contract_name}) ddo = network.resolve_agent(args.agent, username=args.username, password=args.password) if not ddo: output.add_line(f'cannot resolve asset {args.asset}') return authentication = None if args.username or args.password: authentication = AuthenticationBasic(args.username, args.password) agent = RemoteAgent(ddo=ddo, authentication=authentication) metadata = {} if args.description: metadata['description'] = args.description if args.created_now: metadata['dateCreated'] = datetime.datetime.now().isoformat() if args.date_created: metadata['dateCreated'] = args.date_created if args.author: metadata['author'] = args.author if args.license: metadata['license'] = args.license if args.copyright_holder: metadata['copyrightHolder'] = args.copyright_holder if args.in_language: metadata['inLanguage'] = args.in_language if args.tags: print('tags', args.tags) tag_list = re.split(r'[\s\S]+', args.tags) metadata['tags'] = tag_list if not metadata.keys(): metadata = None asset = DataAsset.create_from_file(args.name, args.filename, metadata=metadata) asset = agent.register_asset(asset) agent.upload_asset(asset) output.add_line(f'stored asset {asset.did}') output.set_value('asset_did', asset.did)
def test_05_agent_endpoint_update(config, remote_agent_surfer): endpoint = remote_agent_surfer.get_endpoint('DEP.Meta.v1') assert (endpoint) assert (re.search('meta', endpoint)) local_agent = config['agents']['surfer'] ddo = DDO.create(local_agent['url'], service_list=['meta'], version='v99') new_endpoint_uri = '/app/v99/meta/test' new_agent = RemoteAgent(ddo=ddo) with pytest.raises(ValueError): new_endpoint = new_agent.get_endpoint('DEP.Meta.v99')
def load_agent(self, authentication=None, use_cache=True): """ Load this agent and return a starfish RemoteAgent object. :param dict authentication: Authentication dict to use to load this agent. If None use the authentication in the access object :param bool use_cache: If True then use the agent cache to store future requests for remote agents :param object http_client: HTTP Client to use to access the agent. :returns: Remote Agent object that has been loaded """ agent = None if authentication is None: authentication = self._authentication if use_cache: cache_key = self.calc_cache_key(authentication) if cache_key not in self._agent_cache: # same method but with no cache self._agent_cache[cache_key] = self.load_agent(authentication, use_cache=False) agent = self._agent_cache[cache_key] else: if self._ddo is None: self.resolve_url(self._http_client) logger.debug(f'loading remote agent {self._name}: {self._did}') agent = RemoteAgent(self._ddo, authentication=authentication, http_client=self._http_client) return agent
def remote_agent_invokable(config): ddo_options = None local_agent = config['agents']['invokable'] ddo = DDO.create(local_agent['url']) authentication = AuthenticationBasic(local_agent['username'], local_agent['password']) return RemoteAgent(ddo, authentication=authentication)
def resolve_agent_did(self, did, network=None, authentication=None, http_client=None): """ Resolve an agent using ony the DID of the remote agent. You need to set the network property on this object before searching for a did in the network :param str did: DID of the remote agent to resolve :param Network network: Optional network object to resolve the DID, if not used then use the class network value instead :param dict authentication: Authentication dict that can access the agent api :param object http_client: Test http client to make requests to the agent :returns: RemoteAgent object if found, else return None """ if network is None: network = self._network if not network: raise ValueError('No network set to resolve a DID') ddo = AgentAccess.resolve_agent_did(did, network) if ddo: return RemoteAgent(ddo, authentication=authentication, http_client=http_client)
def remote_agent_surfer(config): ddo_options = None local_agent = config['agents']['surfer'] ddo = DDO.create(local_agent['url'], ['meta', 'storage', 'invoke', 'market', 'trust', 'auth']) authentication = AuthenticationBasic(local_agent['username'], local_agent['password']) return RemoteAgent(ddo, authentication=authentication)
def test_04_agent_register_and_resolve(convex_network, config, convex_accounts): local_agent = config['agents']['surfer'] ddo = DDO.create(local_agent['url']) authentication = AuthenticationBasic(local_agent['username'], local_agent['password']) register_account = convex_accounts[0] did = ddo.did remote_agent = RemoteAgent.register(convex_network, register_account, ddo, authentication) assert (remote_agent) found_ddo = DDO.import_from_text(convex_network.resolve_did(did)) assert (found_ddo.as_text == ddo.as_text) resolved_agent = RemoteAgent.load(ddo.did, convex_network, authentication=authentication) assert (resolved_agent) assert (resolved_agent.ddo) assert (resolved_agent.ddo.as_text == ddo.as_text)
def resolve_agent_url(self, url, authentication=None, http_client=None): """ Resolve an agent using ony the URL of the remote agent. :param str url: URL of the remote agent to resolve :param dict authentication: Authentication dict that can access the agent api :param object http_client: Test http client to make requests to the agent :returns: RemoteAgent object if found, else return None """ ddo = AgentAccess.resolve_agent_url(url, authentication=authentication, http_client=http_client) if ddo: return RemoteAgent(ddo, authentication=authentication, http_client=http_client)
def resolve_agent_url(url, authentication=None, http_client=None): """ Resolve an agent based on it's url. :param str url: URL of the agent :param dict authentication: Authentication of the agent. :param object http_client: HTTP client to use to access the agent api :returns: DDO object of the remote agent """ logger.debug(f'resolving remote agent did from {url}') try: ddo_text = RemoteAgent.resolve_url(url, authentication=authentication, http_client=http_client) if ddo_text: return DDO.import_from_text(ddo_text) # ignore connetion errors to remote agents except StarfishConnectionError: pass
def main(): # Create a new convex network instance. network = ConvexNetwork('https://convex.world') # Create a remote agent to do the work. agent_url = 'http://localhost:3030' authentication = AuthenticationBasic('Aladdin', 'OpenSesame') # find an agent based on it's url, you can also use an agent did or asset did instead agent = RemoteAgent.load(agent_url, network, authentication=authentication) if not agent: print('failed to find the agent') # create a listing specifying the information about the asset listing_data = { 'name': 'The white paper', 'author': 'Datacraft', 'license': 'CC0: Public Domain', 'price': '0' } # Now create a memory asset asset = DataAsset.create( 'TestAsset', 'Some test data that I want to save for this asset') asset = create_asset_provenance_publish(asset, agent.did) # Print the asset data print('my asset:', asset.data) asset = agent.register_asset(asset) print(asset.did) listing = agent.create_listing(listing_data, asset.did) print(listing.did, listing.data) # now upload the asset data to Surfer agent.upload_asset(asset)