async def get_token_from_login_process(address, private_key): http_client = HTTPClient(CONFIG.http_uri + '/users') response = http_client.request(method_name='login_hash', address=address) random_result = json.loads(response.text)['result'] random_bytes = bytes.fromhex(random_result[2:]) signature_base64str = sign(private_key, random_bytes) response = http_client.request(method_name='login', address=address, signature=signature_base64str) token = json.loads(response.text)['result'] response = http_client.request(method_name='set_nickname', token=token, nickname=address) return token
def getMoneroBlockCount(url): client = HTTPClient(url) u = urlparse.urlparse(url) client.session.auth = HTTPDigestAuth(u.username, u.password) response = client.request('getblockcount') blocks = response.data.result['count'] return blocks
class ZilliqaAPI: """Json-RPC interface of Zilliqa APIs.""" class APIMethod: def __init__(self, api: "ZilliqaAPI", method_name: str): self.api = api self.method_name = method_name def __call__(self, *params, **kwargs): resp = self.api.call(self.method_name, *params, **kwargs) return resp and resp.data and resp.data.result def __init__(self, endpoint: str): self.endpoint = endpoint self.api_client = HTTPClient(self.endpoint) def __getattr__(self, item: str): return ZilliqaAPI.APIMethod(self, method_name=item) def call(self, method_name: str, *params, **kwargs): # fixme: fix for jsonrpcclient < 3.3.1 # if len(params) == 1 and (isinstance(params[0], (dict, list))): # params = (list(params), ) try: return self.api_client.request(method_name, *params, trim_log_values=True, **kwargs) except JsonRpcClientError as e: raise APIError(e)
def test_client(): url = "http://{0}{1}".format('172.19.3.111:38080/', 'com.ofpay.demo.api.UserProvider') client = HTTPClient(url) for x in range(number): response = client.request('getUser', 'A003') response2 = client.request( 'queryUser', { 'age': 18, 'time': 1428463514153, 'sex': 'MAN', 'id': 'A003', 'name': 'zhangsan' }) response3 = client.request('isLimit', 'MAN', 'Joe') response4 = client.request('getUser', 'A005')
def call_aiohttp(endpoint: str, **kwargs) -> Dict: try: client = HTTPClient(f'http://{endpoint}') r = client.request("_call", **kwargs) return {'status': SUCCEED_STATUS, 'data': r.data.result} except Exception as e: return { 'status': FAILED_STATUS, 'data': f'{str(e)}\n{traceback.format_exc()}' }
def request(url: str, method: str, params: dict = None): if not params: params = {} client = HTTPClient(url) try: response = client.request(method, **params) except: raise else: return response.data.result
def call(self, method, *args, **kwargs): provider = self.registry.get_random_provider(self.interface, version=self.version, group=self.group) # print service_url.location url = "http://{0}{1}".format(provider.location, provider.path) # print(url) client = HTTPClient(url) try: response = client.request(method, *args, **kwargs) if response.data.ok: return response.data.result else: raise DubboClientError(message=response.text, data=response.data) except HTTPError as e: raise ConnectionFail(None, e.filename) except ReceivedNon2xxResponseError as error: if error.code in dubbo_client_errors: message = str(error) raise dubbo_client_errors[error.code](message=message, data=error.args, code=error.code) else: message = str(error) raise DubboClientError(message=message, data=error.args, code=error.code) except ReceivedErrorResponseError as error: if error.response.code in dubbo_client_errors: message = str(error.response.message) raise dubbo_client_errors[error.response.code](message=message, data=error.response.data, code=error.response.code) else: message = str(error.response.message) raise DubboClientError(message=message, data=error.args, code=error.code) except JsonRpcClientError as error: raise DubboClientError(message=str(error), data=error.args) except Exception as ue: if hasattr(ue, 'message'): if hasattr(ue, 'reason'): raise InternalError(ue.message, ue.reason) else: raise InternalError(ue.message, None) else: raise InternalError(str(ue), None)
class JsonRpcTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.gen_id = random() def setUp(self): self.client = HTTPClient('http://127.0.0.1:8080') def tearDown(self): self.client.session.close() def test_single_args(self): r = self.client.request('test.hello', 'WORLD', id_generator=self.gen_id) self.assertEqual(r.data.result, 'Hello WORLD!') def test_single_kwargs(self): r = self.client.request('test.hello', who='WORLD', id_generator=self.gen_id) self.assertEqual(r.data.result, 'Hello WORLD!') def test_single_notification(self): r = self.client.notify('test.hello', who='WORLD') self.assertEqual(r.text, '') def test_single_err(self): with self.assertRaises(ReceivedErrorResponseError) as cm: self.client.request('test.test_div', 10, 0, id_generator=self.gen_id) exception = cm.exception self.assertEqual(exception.args[0], 'division by zero') def test_batch(self): gen_id = iter(range(100)) batch = ( Request('vadd.test', 5, 10, id_generator=gen_id), Request('vsub.test', arg1=10, arg2=9, id_generator=gen_id), Request('test.hello', 'WORLD', id_generator=gen_id), Request('nonexistent_method', id_generator=gen_id), Request('test.test_div', 10, 0, id_generator=gen_id), ) resp = self.client.send(batch) self.assertEqual(len(resp.data), 5) for r in resp.data: if r.id == 0: self.assertEqual(r.result, 15) elif r.id == 1: self.assertEqual(r.result, 1) elif r.id == 2: self.assertEqual(r.result, 'Hello WORLD!') elif r.id == 3: self.assertFalse(r.ok) self.assertEqual(r.code, -32601) elif r.id == 4: self.assertFalse(r.ok) self.assertEqual(r.code, -32000) self.assertEqual(r.message, 'division by zero')
import logging from jsonrpcclient.clients.http_client import HTTPClient client = HTTPClient("http://localhost:5000") response = client.request("ping") if response.data.ok: print(response.data.result) else: logging.error(response.data.message)
def test_client_every_new(): url = "http://{0}{1}".format('zookeeper:38081/', 'com.ofpay.demo.api.UserProvider2') client = HTTPClient(url) response = client.request('getUser', 'A003') print(response)
def monitor_all( self, zone_id: str, all_transform_ids: list, provider_client: HTTPClient, stdscr: '_curses.window', refresh_time: float, ): all_transforms_last_block = {tid: 0 for tid in all_transform_ids} all_transforms_prev_last_block = {tid: 0 for tid in all_transform_ids} all_transforms_prev_time = {tid: 0 for tid in all_transform_ids} all_transforms_speed = {tid: 0 for tid in all_transform_ids} latest_block_height = 0 while 1: upstream_connected = rpc_client.call(self.upstream_endpoint, call_id='ping')['status'] aggregator_connected = rpc_client.call(self.aggregator_endpoint, call_id='ping')[ 'status' ] warehouse_connected = rpc_client.call(self.warehouse_endpoint, call_id='ping')['status'] provider_connected = provider_client.request("_call", call_id='ping').data.result if upstream_connected: upstream_response = rpc_client.call( self.upstream_endpoint, call_id='last_block_height' ) if upstream_response['status']: latest_block_height = upstream_response['data'] if ( upstream_connected and aggregator_connected and warehouse_connected and provider_connected ): for tid in all_transform_ids: last_block = provider_client.request( "_call", call_id='api_call', api_id='last_block_height', api_params={'transform_id': tid}, ).data.result['result'] if last_block: all_transforms_prev_last_block[tid] = all_transforms_last_block[tid] all_transforms_last_block[tid] = last_block all_transforms_speed[tid] = int( (last_block - all_transforms_prev_last_block[tid]) / (time.time() - all_transforms_prev_time[tid]) ) all_transforms_prev_time[tid] = time.time() c1 = C if upstream_connected else D c2 = C if aggregator_connected else D c3 = C if warehouse_connected else D c4 = C if provider_connected else D stdscr.erase() stdscr.addstr( 0, 0, f'== Data Aggregation Monitor | Zone ID: {zone_id} | All Transforms ==', ) stdscr.addstr(1, 0, f'Upstream service: {self.upstream_endpoint} {c1}') stdscr.addstr(2, 0, f'Aggregator service: {self.aggregator_endpoint} {c2}') stdscr.addstr(3, 0, f'Warehouse service: {self.warehouse_endpoint} {c3}') stdscr.addstr(4, 0, f'Provider service: {self.provider_endpoint} {c4}') stdscr.addstr(5, 0, f'Working dir: {self.working_dir}') stdscr.addstr(6, 0, f'----') stdscr.addstr(7, 0, f'Latest upstream block: {latest_block_height:,}') stdscr.addstr(8, 0, f'Latest aggregated block of all transforms') for i, tid in enumerate(all_transforms_last_block): stdscr.addstr( 9 + i, 0, f'----{tid}: {all_transforms_last_block[tid]:,} | {all_transforms_speed[tid]} blocks/s', ) stdscr.refresh() time.sleep(refresh_time)
def monitor_basic( self, zone_id: str, transform_id: str, provider_client: HTTPClient, stdscr: '_curses.window', refresh_time: float, ): latest_block_height = 0 prev_last_block = 0 prev_time = time.time() last_block = 0 while 1: upstream_connected = rpc_client.call(self.upstream_endpoint, call_id='ping')['status'] aggregator_connected = rpc_client.call(self.aggregator_endpoint, call_id='ping')[ 'status' ] warehouse_connected = rpc_client.call(self.warehouse_endpoint, call_id='ping')['status'] provider_connected = provider_client.request("_call", call_id='ping').data.result if upstream_connected: upstream_response = rpc_client.call( self.upstream_endpoint, call_id='last_block_height' ) if upstream_response['status']: latest_block_height = upstream_response['data'] if ( upstream_connected and aggregator_connected and warehouse_connected and provider_connected ): r = provider_client.request( "_call", call_id='api_call', api_id='last_block_height', api_params={'transform_id': transform_id}, ).data.result['result'] if r: last_block = r speed = int((last_block - prev_last_block) / (time.time() - prev_time)) prev_last_block = last_block prev_time = time.time() if latest_block_height > last_block > 0 and speed > 0: remaining_time = seconds_to_datetime((latest_block_height - last_block) / speed) elif latest_block_height == last_block and last_block > 0: remaining_time = 'Fully synced' elif latest_block_height < last_block: remaining_time = ( 'Upstream block height is lower than latest aggregated block (out-of-date)' ) else: remaining_time = 'N/A' remaining_blocks = abs(latest_block_height - last_block) c1 = C if upstream_connected else D c2 = C if aggregator_connected else D c3 = C if warehouse_connected else D c4 = C if provider_connected else D stdscr.erase() stdscr.addstr( 0, 0, f'== Data Aggregation Monitor | Zone ID: {zone_id} | Transform ID: {transform_id} ==', ) stdscr.addstr(1, 0, f'Upstream service: {self.upstream_endpoint} {c1}') stdscr.addstr(2, 0, f'Aggregator service: {self.aggregator_endpoint} {c2}') stdscr.addstr(3, 0, f'Warehouse service: {self.warehouse_endpoint} {c3}') stdscr.addstr(4, 0, f'Provider service: {self.provider_endpoint} {c4}') stdscr.addstr(5, 0, f'Working dir: {self.working_dir}') stdscr.addstr(6, 0, f'----') stdscr.addstr(7, 0, f'Latest upstream block: {latest_block_height:,}') stdscr.addstr(8, 0, f'Latest aggregated block: {last_block:,}') stdscr.addstr(9, 0, f'Data aggregation speed: {speed} blocks/s') stdscr.addstr(10, 0, f'Remaining blocks: {remaining_blocks:,}') stdscr.addstr(11, 0, f'Estimated time remaining: {remaining_time}') stdscr.refresh() time.sleep(refresh_time)
class DotmeshClient(object): """ The Dotmesh client. :param cluster_url: the dotmesh cluster URL, for example: `http://localhost:6969/rpc` :param username: the HTTP Basic auth user name, such as `admin` :param api_key: the HTTP Basic auth password as an API Key """ def __init__(self, cluster_url, username, api_key): """ Creates the Dotmesh client. """ self.cluster_url = cluster_url self.client = HTTPClient(cluster_url) self.client.session.auth = (username, api_key) def ping(self): """ Pings the cluster. """ return self.client.request("DotmeshRPC.Ping").data.result def getDot(self, dotname, ns="admin"): """ Looks up an existing dot by name. :param dotname: the name of the dot, as a string or a DotName :param ns: the namespace to operate in (if dotname isn't a Dotname), defaults to 'admin' :return: a Dot object """ if isinstance(dotname, DotName): ns = dotname.namespace dotname = dotname.name id = self.client.request("DotmeshRPC.Lookup", Namespace=ns, Name=dotname, Branch="").data.result return Dot(client=self.client, id=id, name=dotname, ns=ns) def createDot(self, dotname, ns="admin"): """ Creates a dot by name. :param dotname: the name of the dot :param ns: the namespace to operate in, defaults to 'admin' :return: a Dot object """ if isinstance(dotname, DotName): ns = dotname.namespace dotname = dotname.name self.client.request("DotmeshRPC.Create", Namespace=ns, Name=dotname).data.result return self.getDot(dotname, ns) def deleteDot(self, dotname, ns="admin"): """ Deletes a dot by name. :param dotname: the name of the dot :param ns: the namespace to operate in, defaults to 'admin' """ if isinstance(dotname, DotName): ns = dotname.namespace dotname = dotname.name return self.client.request("DotmeshRPC.Delete", Namespace=ns, Name=dotname).data.result
class InstantClient(Generic[T]): """ A type-safe JSON-RPC client with automatic (de)serialization. For more info, see https://github.com/alexmojaki/instant_client Usage looks like this: from dataclasses import dataclass from instant_client import InstantClient @dataclass class Point: x: int y: int class Methods: def translate(self, p: Point, dx: int, dy: int) -> Point: pass def scale(self, p: Point, factor: int) -> Point: pass methods = InstantClient("http://127.0.0.1:5000/api/", Methods()).methods assert methods.scale(Point(1, 2), factor=3) == Point(3, 6) While this looks like it just called `Methods.scale()` directly, under the hood it sent an HTTP request to a server. The same code written more manually looks like this: import requests response = requests.post( 'http://127.0.0.1:5000/api/', json={ 'id': 0, 'jsonrpc': '2.0', 'method': 'scale', 'params': { 'p': {'x': 1, 'y': 2}, 'factor': 3, }, }, ) assert response.json()['result'] == {'x': 3, 'y': 6} The constructor has two required parameters: 1. [A client from the jsonrpcclient library](https://jsonrpcclient.readthedocs.io/en/latest/examples.html) for your desired transport. As a convenience, you can also just pass a string representing a URL, which will be used to construct an HTTPClient. 2. An object defining your methods. The method body can be empty, InstantClient just uses the signature and type hints to serialize the arguments and deserialize the result with the help of [datafunctions](https://github.com/alexmojaki/datafunctions). The `methods` attribute of the client is a simple proxy so that this: client.methods.scale(Point(1, 2), factor=3) is equivalent to: client.request("scale", Point(1, 2), factor=3) which in turn looks up the signature of the original method. """ def __init__(self, url_or_client: Union[str, Client], methods: T): self.client: Client if isinstance(url_or_client, str): self.client = HTTPClient(url_or_client) else: self.client = url_or_client self.methods = self._original_methods = methods client_self = self class MethodsProxy: def __getattr__(self, method_name): return partial(client_self.request, method_name) # Replace self.methods with the proxy and hope that # static analysers don't notice setattr(self, "methods"[::-1][::-1], MethodsProxy()) def request(self, method_name, *args, **kwargs): method = getattr(self._original_methods, method_name) method = datafunction(method) # Here we use datafunction in reverse data = method.dump_arguments(*args, **kwargs) response = self.client.request(method_name, **data) return method.load_result(response.data.result)