def test_sdk(): org_id = "test_org" service_id = "test_service" group_name = "default_group" config = { "private_key": "0xc71478a6d0fe44e763649de0a0deb5a080b788eefbbcf9c6f7aef0dd5dbd67e0", "eth_rpc_endpoint": "http://*****:*****@test.com" } snet_sdk = sdk.SnetSDK(config) service_client = snet_sdk.create_service_client( org_id, service_id, examples_service_pb2_grpc.CalculatorStub, group_name=group_name, concurrent_calls=3) check_channel_status(service_client, 3000) make_cuncurrent_calls(service_client) check_channel_status(service_client, 6000)
def __init__(self): self.org_id = 'odyssey-org' self.service_id = 'uclnlp-service' self.group_name = 'default_group' self.count = 0 self.call_count = 0 self.rpc_endpoints = rpc_endpoints self.snet_sdk = sdk.SnetSDK(config) self.start_time = time.time() self.proxy_port = 7005
def get_proto(self): self.log.info("Getting protobuf files from IPFS...") snet_sdk = sdk.SnetSDK( config={ "private_key": self.private_key, "eth_rpc_endpoint": self.eth_rpc_endpoint }) metadata = snet_sdk.get_service_metadata(self.org_id, self.service_id) ipfs_client = ipfsapi.connect("http://ipfs.singularitynet.io", 80) proto_dir = "{}/protos".format(SDK_SERVER_DIR) if os.path.exists(proto_dir): shutil.rmtree(proto_dir) safe_extract_proto_from_ipfs(ipfs_client, metadata["model_ipfs_hash"], proto_dir) self.log.info("Done!") return proto_dir
def test_sdk(): org_id = "6ce80f485dae487688c3a083688819bb" service_id = "test_freecall" group_name = "default_group" config = { "private_key": "0x484573a7949da33d9aceeba372fc697532fdb69278318c03d04418a1af8a6208", "eth_rpc_endpoint": "https://ropsten.infura.io/v3/09027f4a13e841d48dbfefc67e7685d5", "mpe_contract_address": "0x8fb1dc8df86b388c7e00689d1ecb533a160b4d0c", "registry_contract_address": "0x663422c6999ff94933dbcb388623952cf2407f6f", "token_contract_address": "0xb97E9bBB6fd49865709d3F1576e8506ad640a13B", "ipfs_rpc_endpoint": "http://ipfs.singularitynet.io:80", "free_call_auth_token-bin": "", "free-call-token-expiry-block": 9808238, "email": "*****@*****.**" } snet_sdk = sdk.SnetSDK(config) service_client = snet_sdk.create_service_client( org_id, service_id, examples_service_pb2_grpc.CalculatorStub, group_name=group_name, concurrent_calls=2) token, channel = service_client.get_concurrency_token_and_channel() try: thread_1 = threading.Thread(target=service_call, args=(service_client, token, channel, 2, 3), name="thread_1") thread_2 = threading.Thread(target=service_call, args=(service_client, token, channel, 4, 5), name="thread_2") thread_1.start() thread_2.start() thread_1.join() thread_2.join() except Exception as e: print("threading failed", e)
def rest_to_grpc(path=None): if request.method in ["GET", "POST"]: try: self.log.info("Got a request...") req = None if request.method == "GET": if not request.args: ret = dict() for s in self.services_dict.keys(): ret[s] = list(self.services_dict[s].keys()) return ret, 200 else: req = request.args.to_dict() if not path: return self.services_dict, 500 path_list = path.split("/") if not path_list or path_list[0].upper() == "HELP": return self.services_dict, 500 service = path_list[0] if service not in self.services_dict: error_msg = "Invalid gRPC service." self.log.error(error_msg) return {"Error": error_msg, **self.services_dict}, 500 if not req: if request.data: req = json.loads(request.data.decode("utf-8")) else: req = request.json if request.json else request.form.to_dict( ) if len(path_list) > 1: method = path_list[1] else: method = req.get( "method", list(self.services_dict[service].keys())[0]) if method not in self.services_dict[service].keys(): error_msg = "Invalid gRPC method." self.log.error(error_msg) return { "Error": error_msg, **self.services_dict[service] }, 500 # Free calls: Removing its fields from the Request to route only the gPRC ones. token = req.get("token", "") expiration = int(req.get("expiration", "0")) email = req.get("email", "") if all(p in req.keys() for p in ["token", "expiration", "email"]): del req["token"] del req["expiration"] del req["email"] else: error_msg = "email, token, expiration and email fields are required!" self.log.error(error_msg) return {"Error": error_msg}, 500 input_message = self.services_dict[service][method][ "input"] input_dict = input_factory(req, input_message, self.classes) grpc_input = self.classes[input_message["name"]]( **input_dict) config = { "eth_rpc_endpoint": self.eth_rpc_endpoint, "private_key": self.private_key, "free_call_auth_token-bin": token, "free-call-token-expiry-block": expiration, "email": email } self.log.info("Setting up the SDK...") snet_sdk = sdk.SnetSDK(config) client = snet_sdk.create_service_client( self.org_id, self.service_id, self.stubs[service], group_name=self.group_name) self.log.info("Calling the service...") method_stub = getattr(client.service, method, None) response = method_stub(grpc_input) output_message = self.services_dict[service][method][ "output"] output_dict = output_factory(response, output_message) self.log.info("Request processed!") return output_dict, 200 except Exception as e: self.log.error("{}\n{}".format(e, traceback.print_exc())) return { "Error": "Invalid gRPC request.", **self.services_dict }, 500 error_msg = "Invalid HTTP request (use POST)." self.log.error(error_msg) return {"Error": error_msg}, 500
from extract import * from snet import sdk from config import config, rpc_endpoints import uclnlp_service_pb2 import uclnlp_service_pb2_grpc import athenefnc_pb2 import athenefnc_pb2_grpc import fake_news_score_pb2 import fake_news_score_pb2_grpc service = os.getenv("SERVICE") snet_sdk = sdk.SnetSDK(config) org_id = 'odyssey-org' if service == "uclnlp": service_id = 'uclnlp-service' service_client = snet_sdk.create_service_client( org_id, service_id, uclnlp_service_pb2_grpc.UCLNLPStanceClassificationStub, group_name='default_group', concurrent_calls=10) elif service == "athene": service_id = 'athene-service' service_client = snet_sdk.create_service_client( org_id, service_id,