def request(self, method, path, **kwargs): path = "{}{}".format(self.api_root, path) if self.credentials: self.credentials.before_request(self.session, self.auth_root) try: response = self.session.request(method, path, **kwargs) except requests.exceptions.SSLError as sslError: msg = "Connection to {} failed: {}".format(self.address, sslError) six.raise_from(ConnectionFailure(msg), None) except requests.exceptions.ConnectionError as e: raise ConnectionFailure("Connection to {} failed: {}".format( self.address, e)) if 200 <= response.status_code < 300: return response exception_message = exception_pb2.ExceptionMessage() try: exception_message.ParseFromString(response.content) except DecodeError: pass if response.status_code == 401: raise Unauthorized("401 Client Error: Unauthorized") elif response.status_code == 404: raise NotFound("404 Client Error: {}".format( getattr(exception_message, "msg"))) elif 400 <= response.status_code < 500: raise YamcsError("{} Client Error: {}".format( response.status_code, getattr(exception_message, "msg"))) raise YamcsError("{} Server Error: {}".format( response.status_code, getattr(exception_message, "msg")))
def _build_value_proto(value): proto = yamcs_pb2.Value() if isinstance(value, bool): proto.type = proto.BOOLEAN proto.booleanValue = value elif isinstance(value, float): proto.type = proto.DOUBLE proto.doubleValue = value elif isinstance(value, int) and value > 2147483647: proto.type = proto.SINT64 proto.sint64Value = value elif isinstance(value, int): proto.type = proto.SINT32 proto.sint32Value = value elif isinstance(value, str): proto.type = proto.STRING proto.stringValue = value elif isinstance(value, bytes): proto.type = proto.BINARY proto.binaryValue = value elif isinstance(value, bytearray): proto.type = proto.BINARY proto.binaryValue = bytes(value) elif isinstance(value, datetime.datetime): proto.type = proto.TIMESTAMP proto.stringValue = to_isostring(value) elif isinstance(value, collections.abc.Mapping): proto.type = proto.AGGREGATE proto.aggregateValue.name.extend(value.keys()) proto.aggregateValue.value.extend( [_build_value_proto(v) for v in value.values()]) elif isinstance(value, collections.abc.Sequence): proto.type = proto.ARRAY proto.arrayValue.extend([_build_value_proto(v) for v in value]) else: raise YamcsError("Unrecognized type") return proto