Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
namara-python / rpc / query_pb2_twirp.py
Size: Mime:
# Code generated by protoc-gen-twirp_python v5.9.0, DO NOT EDIT.
# source: query.proto

try:
    import httplib
    from urllib2 import Request, HTTPError, urlopen
except ImportError:
    import http.client as httplib
    from urllib.request import Request, urlopen
    from urllib.error import HTTPError
import json
from google.protobuf import symbol_database as _symbol_database
import sys

_sym_db = _symbol_database.Default()

class TwirpException(httplib.HTTPException):
    def __init__(self, code, message, meta):
        self.code = code
        self.message = message
        self.meta = meta
        super(TwirpException, self).__init__(message)

    @classmethod
    def from_http_err(cls, err):
        try:
            jsonerr = json.load(err)
            code = jsonerr["code"]
            msg = jsonerr["msg"]
            meta = jsonerr.get("meta")
            if meta is None:
                meta = {}
        except:
            code = "internal"
            msg = "Error from intermediary with HTTP status code {} {}".format(
                err.code, httplib.responses[err.code],
            )
            meta = {}
        return cls(code, msg, meta)

class QueryServiceClient(object):
    def __init__(self, server_address, request_decorator=None):
        """Creates a new client for the QueryService service.

        Args:
            server_address: The address of the server to send requests to, in
                the full protocol://host:port form.
            request_decorator: A function to modify the http request being
		         sent. Takes in a urllib.request.Request object and returns the
		         same request object
        """
        if sys.version_info[0] > 2:
            self.__target = server_address
        else:
            self.__target = server_address.encode('ascii')
        self.__service_name = "query.QueryService"

        self.__request_decorator = request_decorator

    def __make_request(self, body, full_method):
        req = Request(
            url=self.__target + "/twirp" + full_method,
            data=body,
            headers={"Content-Type": "application/protobuf"},
        )

        if self.__request_decorator:
            req = self.__request_decorator(req)

        try:
            resp = urlopen(req)
        except HTTPError as err:
            raise TwirpException.from_http_err(err)

        return resp.read()

    def healthz(self, healthz_request):
        """
        Check if we are alive
        """

        serialize = _sym_db.GetSymbol("query.HealthzRequest").SerializeToString
        deserialize = _sym_db.GetSymbol("query.HealthzResponse").FromString

        full_method = "/{}/{}".format(self.__service_name, "Healthz")
        body = serialize(healthz_request)
        resp_str = self.__make_request(body=body, full_method=full_method)
        return deserialize(resp_str)

    def warehousez(self, warehousez_request):
        """
        Check warehouse connectivity
        """

        serialize = _sym_db.GetSymbol("query.WarehousezRequest").SerializeToString
        deserialize = _sym_db.GetSymbol("query.WarehousezResponse").FromString

        full_method = "/{}/{}".format(self.__service_name, "Warehousez")
        body = serialize(warehousez_request)
        resp_str = self.__make_request(body=body, full_method=full_method)
        return deserialize(resp_str)

    def query(self, query_request):
        """
        Get to the data
        """

        serialize = _sym_db.GetSymbol("query.QueryRequest").SerializeToString
        deserialize = _sym_db.GetSymbol("query.QueryResponse").FromString

        full_method = "/{}/{}".format(self.__service_name, "Query")
        body = serialize(query_request)
        resp_str = self.__make_request(body=body, full_method=full_method)
        return deserialize(resp_str)

    def get_metadata(self, get_metadata_request):
        """
        Get the response meta without querying the data
        """

        serialize = _sym_db.GetSymbol("query.GetMetadataRequest").SerializeToString
        deserialize = _sym_db.GetSymbol("query.GetMetadataResponse").FromString

        full_method = "/{}/{}".format(self.__service_name, "GetMetadata")
        body = serialize(get_metadata_request)
        resp_str = self.__make_request(body=body, full_method=full_method)
        return deserialize(resp_str)