def subscribe(c, message): # if authenticated if message.status == 1: c.logger.info('Authenticated') # we'll subscribe to spread updates in this example # use * instead of market ID to subscribe to all markets # subscribe to bitfinex:ltc-usd and kraken:btc-usd spread updates msg_data = { 'subscribe': { 'subscriptions': [ {'streamSubscription': {'resource': 'markets:2:book:spread'}}, {'streamSubscription': {'resource': 'markets:87:book:spread'}}, ] } } # build msg from dict if it looks easier # see simple_subscribe for example on how to build message manually msg = ParseDict(msg_data, ClientMessage()) # send subscription message subscribing to spread updates c.send(msg.SerializeToString()) else: c.logger.warning(message)
def dict_to_proto_message(data_dict, obj): try: obj = ParseDict(data_dict, obj, ignore_unknown_fields=True) return obj.SerializeToString() except Exception as e: message = "%s\t%s" % ("Error in converting dict to proto", repr(e)) logger.critical(message)
def Proto(args): json_content = '' with open(args.source) as f: for line in f: if not line.lstrip().startswith('//'): json_content += line obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict) pb = ParseDict(obj, linker_config_pb2.LinkerConfig()) with open(args.output, 'wb') as f: f.write(pb.SerializeToString())
def prepare_data(self): requestID = uuid.uuid4().hex[:10] data = {} data['RequestID'] = requestID data['Query'] = {} data['Params'] = dict(ID=self.measurementID) wsID = uuid.uuid4().hex[:10] # Make this ID sequential or variable websocketRouteID = '510' requestMessageProto = ParseDict(data, SubscribeResultsRequest(), ignore_unknown_fields=True) self.requestData = f'{websocketRouteID:4}{wsID:10}'.encode( ) + requestMessageProto.SerializeToString()
def predict_raw(self, request) -> SeldonMessage: is_proto = True if not isinstance(request, SeldonMessage): # If `request` is a dict, parse it into a protobuf object is_proto = False request = ParseDict(request, SeldonMessage()) logger.debug("[PYTHON] Serialising request") serialised = request.SerializeToString() logger.debug("[PYTHON] Sending request to Java model") prediction_raw = self._model.predictGRPC(serialised) logger.debug("[PYTHON] De-serialising response") response = SeldonMessage() response.ParseFromString(bytes(prediction_raw)) if not is_proto: response = MessageToDict(response) return response
def test_from_pb_w_unknown_metadata(self): from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.protobuf.json_format import ParseDict from google.protobuf.struct_pb2 import Struct from google.cloud._testing import _Monkey from google.cloud import operation as MUT type_url = 'type.googleapis.com/%s' % (Struct.DESCRIPTOR.full_name, ) client = _Client() meta = ParseDict({'foo': 'Bar'}, Struct()) metadata_pb = Any(type_url=type_url, value=meta.SerializeToString()) operation_pb = operations_pb2.Operation(name=self.OPERATION_NAME, metadata=metadata_pb) klass = self._get_target_class() with _Monkey(MUT, _TYPE_URL_MAP={type_url: Struct}): operation = klass.from_pb(operation_pb, client) self.assertEqual(operation.name, self.OPERATION_NAME) self.assertIs(operation.client, client) self.assertEqual(operation.metadata, meta) self.assertEqual(operation.caller_metadata, {})
def Proto(args): with open(args.input) as f: obj = json.load(f, object_pairs_hook=collections.OrderedDict) pb = ParseDict(obj, apex_manifest_pb2.ApexManifest()) with open(args.out, "wb") as f: f.write(pb.SerializeToString())