def __init__(self,
                 headers=None,
                 url_params=None,
                 body=None,
                 service=None,
                 method=None,
                 endpoint=None,
                 timeout=None):
        self._headers = {} if headers is None else headers
        self._url_params = {} if url_params is None else url_params
        self._body = {} if body is None else body
        self._service = service if service else self._service
        self._http_method = method if method else self._http_method
        self._base_endpoint = \
            self._base_endpoint if endpoint is None else endpoint
        self._timeout = timeout

        if not isinstance(self._headers, dict):
            raise exception.ValueException("'header' should be dictionary.")

        if not isinstance(self.url_params, (dict, list)):
            raise exception.ValueException(
                "'url_params' should be dictionary or list.")

        if not isinstance(self._body, (six.binary_type, dict, str)):
            raise exception.ValueException(
                "'body' should be dictionary, binary or string.")

        self._host = None
def build_request_object(service, method, path, headers=None,
                         query_params=None, body=None, timeout=None):
    if not service:
        raise exception.ValueException(
            "Service: {} should not be empty. ".format(service))
    if method is None or method.upper() not in ['GET', 'HEAD',
                                                'DELETE', 'POST',
                                                'PUT', 'PATCH', 'OPTIONS']:
        raise exception.ValueException(
            "Http method: {} is not supported now.".format(method))
    if timeout is not None and not isinstance(timeout, int):
        raise exception.ValueException(
            "Timeout only support integer now.")
    return request.BaseRequest(headers=headers, body=body,
                               url_params=query_params,
                               service=service,
                               method=method.upper(),
                               endpoint=path,
                               timeout=timeout)
    def handle_request(self, req):
        """Perform http request with supplied Request object.

        :param req: Request object, is a instance of BaseRequest.
        :return: Response tuple, (code, content, header).
        :raise: SDKException: All of the exceptional cases are wrapped
                              in SDKException.
        """

        if not isinstance(req, request.BaseRequest):
            raise exception.ValueException(
                "request must be an instance of 'BaseRequest'.")
        if not req.service:
            raise exception.ValueException(
                "Request's service attribute must not be empty.")
        # Get service endpoint from endpoint resolver
        endpoint = self.resolver.resolve(req, self.region, self.tenant)
        full_path = utils.get_request_endpoint(req, endpoint)
        return self._do_request(req, full_path)
def encode_parameters(paramters):
    # NOTE(tommylikehu): both '%20' and '+' are valid encode values for ''
    # but we need to convert it into '%20', since it's '%20' at the
    # server side.
    if not isinstance(paramters, (dict, list)):
        raise exception.ValueException("url parameters should be dict or list")
    if isinstance(paramters, dict):
        paramters = [(k, v) for k, v in paramters.items()]
    paramters.sort(key=lambda item: (item[0], item[1]))
    if six.PY2:
        result = urlencode(paramters)
        # NOTE: Since python 2 doesn't support quota_via parameter
        # we hard code the special characters here.
        result = result.replace("+", "%20")
    else:
        result = urlencode(paramters, quote_via=quote)
    return result
 def url_params(self, value):
     if not isinstance(value, dict):
         raise exception.ValueException(
             "'url' params should be a dictionary.")
     self._url_params = value
 def headers(self, value):
     if not isinstance(value, dict):
         raise exception.ValueException("'header' should be a dictionary.")
     self._headers = value
 def body(self, value):
     if not isinstance(value, dict):
         raise exception.ValueException("'body' should be a dictionary.")
     self._body = value