Exemple #1
0
    def __init__(self,
                 url,
                 threads=1,
                 qsize=0,
                 timeout=5,
                 user_id=None,
                 user_secret=None):
        """Constructor of Client object."""
        self.threads = threads
        self.url = url
        self.qsize = qsize
        self.timeout = timeout

        # check connection type
        https_pattern = r'^https://(.*)'
        http_pattern = r'^http://(.*)'
        m = re.match(https_pattern, url)
        self.https = True
        if m is None:  # not matching https
            m = re.match(http_pattern, url)
            self.https = False
            if m is None:  # not matching http either
                raise InvalidArgumentError("url is not valid: %s" % url)
        self.host = m.group(1)

        self._uid = None  # identified uid
        self._connection = Connection(host=self.host,
                                      threads=self.threads,
                                      qsize=self.qsize,
                                      https=self.https,
                                      timeout=self.timeout,
                                      user_id=user_id,
                                      user_secret=user_secret)
Exemple #2
0
class BaseClient(object):
    def __init__(self,
                 url,
                 threads=1,
                 qsize=0,
                 timeout=5,
                 user_id=None,
                 user_secret=None):
        """Constructor of Client object."""
        self.threads = threads
        self.url = url
        self.qsize = qsize
        self.timeout = timeout

        # check connection type
        https_pattern = r'^https://(.*)'
        http_pattern = r'^http://(.*)'
        m = re.match(https_pattern, url)
        self.https = True
        if m is None:  # not matching https
            m = re.match(http_pattern, url)
            self.https = False
            if m is None:  # not matching http either
                raise InvalidArgumentError("url is not valid: %s" % url)
        self.host = m.group(1)

        self._uid = None  # identified uid
        self._connection = Connection(host=self.host,
                                      threads=self.threads,
                                      qsize=self.qsize,
                                      https=self.https,
                                      timeout=self.timeout,
                                      user_id=user_id,
                                      user_secret=user_secret)

    def close(self):
        """
        Close this client and the connection.
        Call this method when you want to completely terminate the connection with Harness.
        It will wait for all pending requests to finish.
        """
        self._connection.close()

    def pending_requests(self):
        """
        Return the number of pending requests.
        :returns: The number of pending requests of this client.
        """
        return self._connection.pending_requests()

    def get_status(self):
        """
        Get the status of the Harness API Server
        :returns: status message.
        :raises: ServerStatusError.
        """
        path = "/"
        request = AsyncRequest("GET", path)
        request.set_response_handler(self._ok_response_handler)
        self._connection.make_request(request)
        result = request.get_response()
        return result

    def _add_segment(self, segment=None):
        if segment is not None:
            return "%s/%s" % (self.path, quote(segment, ""))
        else:
            return self.path

    def _add_get_params(self, path=None, **params):
        _path = self.path if path is None else path
        return "%s?%s" % (_path, urlencode(params))

    def _response_handler(self, expected_status, response):
        if response.error is not None:
            raise HttpError("Exception happened: {}".format(response.error),
                            response)
        elif response.status == httplib.NOT_IMPLEMENTED:
            raise NotImplementedError(response)
        elif response.status == httplib.BAD_REQUEST:
            raise BadRequestError(response)
        elif response.status == httplib.NOT_FOUND:
            raise NotFoundError(response)
        elif response.status != expected_status:
            raise UnexpectedStatusError(response)
        return response

    def _create_response_handler(self, response):
        return self._response_handler(httplib.CREATED, response)

    def _ok_response_handler(self, response):
        return self._response_handler(httplib.OK, response)