def _log_entry_mapping_to_pb(mapping): """Helper for :meth:`write_entries`, et aliae Ideally, would use a function from :mod:`protobuf.json_format`, but the right one isn't public. See: https://github.com/google/protobuf/issues/1351 """ # pylint: disable=too-many-branches entry_pb = LogEntry() optional_scalar_keys = { 'logName': 'log_name', 'insertId': 'insert_id', 'textPayload': 'text_payload', } for key, pb_name in optional_scalar_keys.items(): if key in mapping: setattr(entry_pb, pb_name, mapping[key]) if 'resource' in mapping: entry_pb.resource.type = mapping['resource']['type'] if 'severity' in mapping: severity = mapping['severity'] if isinstance(severity, str): severity = LogSeverity.Value(severity) entry_pb.severity = severity if 'timestamp' in mapping: timestamp = _datetime_to_pb_timestamp(mapping['timestamp']) entry_pb.timestamp.CopyFrom(timestamp) if 'labels' in mapping: for key, value in mapping['labels'].items(): entry_pb.labels[key] = value if 'jsonPayload' in mapping: for key, value in mapping['jsonPayload'].items(): entry_pb.json_payload[key] = value if 'protoPayload' in mapping: Parse(json.dumps(mapping['protoPayload']), entry_pb.proto_payload) if 'httpRequest' in mapping: _http_request_mapping_to_pb( mapping['httpRequest'], entry_pb.http_request) if 'operation' in mapping: _log_operation_mapping_to_pb( mapping['operation'], entry_pb.operation) return entry_pb
def _log_entry_mapping_to_pb(mapping): """Helper for :meth:`write_entries`, et aliae Performs "impedance matching" between the protobuf attrs and the keys expected in the JSON API. """ # pylint: disable=too-many-branches entry_pb = LogEntry() optional_scalar_keys = { 'logName': 'log_name', 'insertId': 'insert_id', 'textPayload': 'text_payload', } for key, pb_name in optional_scalar_keys.items(): if key in mapping: setattr(entry_pb, pb_name, mapping[key]) if 'resource' in mapping: entry_pb.resource.type = mapping['resource']['type'] if 'severity' in mapping: severity = mapping['severity'] if isinstance(severity, str): severity = LogSeverity.Value(severity) entry_pb.severity = severity if 'timestamp' in mapping: timestamp = _datetime_to_pb_timestamp(mapping['timestamp']) entry_pb.timestamp.CopyFrom(timestamp) if 'labels' in mapping: for key, value in mapping['labels'].items(): entry_pb.labels[key] = value if 'jsonPayload' in mapping: for key, value in mapping['jsonPayload'].items(): entry_pb.json_payload[key] = value if 'protoPayload' in mapping: Parse(json.dumps(mapping['protoPayload']), entry_pb.proto_payload) if 'httpRequest' in mapping: _http_request_mapping_to_pb( mapping['httpRequest'], entry_pb.http_request) if 'operation' in mapping: _log_operation_mapping_to_pb( mapping['operation'], entry_pb.operation) return entry_pb