コード例 #1
0
ファイル: client.py プロジェクト: bkbarry/swagger-py
def add_param_to_req(param, value, request):
    """Populates request object with the request parameters

    :param param: swagger spec details of a param
    :type param: dict
    :param value: value for the param given in the API call
    :param request: request object to be populated
    """
    pname = param['name']
    type_ = swagger_type.get_swagger_type(param)
    param_req_type = param['paramType']

    if param_req_type == u'path':
        request['url'] = request['url'].replace(
            u'{%s}' % pname,
            urllib.quote_plus(unicode(value)))
    elif param_req_type == u'query':
        request['params'][pname] = value
    elif param_req_type == u'body':
        request['data'] = value
        if not swagger_type.is_primitive(type_):
            # If not primitive, body has to be 'dict'
            # (or has already been converted to dict from model)
            request['headers']['content-type'] = APP_JSON
    elif param_req_type == 'form':
        handle_form_param(pname, value, type_, request)
    # TODO(#31): accept 'header', in paramType
    else:
        raise AssertionError(
            u"Unsupported Parameter type: %s" % param_req_type)
コード例 #2
0
ファイル: client.py プロジェクト: Neston3/swagger-py
def add_param_to_req(param, value, request):
    """Populates request object with the request parameters

    :param param: swagger spec details of a param
    :type param: dict
    :param value: value for the param given in the API call
    :param request: request object to be populated
    """
    pname = param['name']
    type_ = swagger_type.get_swagger_type(param)
    param_req_type = param['paramType']

    if param_req_type == u'path':
        request['url'] = request['url'].replace(
            u'{%s}' % pname, urllib.quote_plus(unicode(value)))
    elif param_req_type == u'query':
        request['params'][pname] = value
    elif param_req_type == u'body':
        request['data'] = value
        if not swagger_type.is_primitive(type_):
            # If not primitive, body has to be 'dict'
            # (or has already been converted to dict from model)
            request['headers']['content-type'] = APP_JSON
    elif param_req_type == 'form':
        handle_form_param(pname, value, type_, request)
    # TODO(#31): accept 'header', in paramType
    else:
        raise AssertionError(u"Unsupported Parameter type: %s" %
                             param_req_type)
コード例 #3
0
def handle_form_param(name, value, type_, request):
    if swagger_type.is_file(type_):
        if 'files' not in request:
            request['files'] = {}
        request['files'][name] = value
    elif swagger_type.is_primitive(type_):
        if 'data' not in request:
            request['data'] = {}
        request['data'][name] = value
    else:
        raise AssertionError(u"%s neither primitive nor File" % name)
コード例 #4
0
ファイル: client.py プロジェクト: bkbarry/swagger-py
def handle_form_param(name, value, type_, request):
    request['headers']['content-type'] = APP_FORM
    if swagger_type.is_file(type_):
        if 'files' not in request:
            request['files'] = {}
        request['files'][name] = value
    elif swagger_type.is_primitive(type_):
        if 'data' not in request:
            request['data'] = {}
        request['data'][name] = value
    else:
        raise AssertionError(
            u"%s neither primitive nor File" % name)
コード例 #5
0
ファイル: response.py プロジェクト: kfreedland/swagger-py
    def create_object(self):
        """Only public method in the class

        Creates the object assuming the response is checked and valid

        :returns: instance of complex Py object or simple primitive object
        """
        if self._response is None:
            return
        if swagger_type.is_primitive(self._type) or self._type == 'void':
            return self._response
        if swagger_type.is_array(self._type):
            return self._create_array_object()
        return self._create_complex_object()
コード例 #6
0
    def create_object(self):
        """Only public method in the class

        Creates the object assuming the response is checked and valid

        :returns: instance of complex Py object or simple primitive object
        """
        if self._response is None:
            return
        if swagger_type.is_primitive(self._type) or self._type == 'void':
            return self._response
        if swagger_type.is_array(self._type):
            return self._create_array_object()
        return self._create_complex_object()
コード例 #7
0
ファイル: client.py プロジェクト: kfreedland/swagger-py
def validate_and_add_params_to_request(param, value, request, models):
    """Validates if a required param is given
    And wraps 'add_param_to_req' to populate a valid request

    :param param: swagger spec details of a param
    :type param: dict
    :param value: value for the param given in the API call
    :param request: request object to be populated
    :param models: models tuple containing all complex model types
    :type models: namedtuple
    """

    # If param not given in args, and not required, just ignore.
    if not param.get('required') and not value:
        return

    pname = param['name']
    type_ = swagger_type.get_swagger_type(param)
    param_req_type = param['paramType']

    if param_req_type == 'path':
        # Parameters in path need to be primitive/array types
        if swagger_type.is_complex(type_):
            raise TypeError("Param %s in path can only be primitive/list" %
                            pname)
    elif param_req_type == 'query':
        # Parameters in query need to be only primitive types
        if not swagger_type.is_primitive(type_):
            raise TypeError("Param %s in query can only be primitive" % pname)

    # Allow lists for query params even if type is primitive
    if isinstance(value, list) and param_req_type == 'query':
        type_ = swagger_type.ARRAY + swagger_type.COLON + type_

    # Check the parameter value against its type
    # And store the refined value back
    value = SwaggerTypeCheck(pname, value, type_, models).value

    # If list in path, Turn list items into comma separated values
    if isinstance(value, list) and param_req_type == 'path':
        value = u",".join(str(x) for x in value)

    # Add the parameter value to the request object
    if value:
        add_param_to_req(param, value, request)
    else:
        if param.get(u'required'):
            raise TypeError(u"Missing required parameter '%s'" % pname)
コード例 #8
0
ファイル: client.py プロジェクト: bkbarry/swagger-py
def validate_and_add_params_to_request(param, value, request, models):
    """Validates if a required param is given
    And wraps 'add_param_to_req' to populate a valid request

    :param param: swagger spec details of a param
    :type param: dict
    :param value: value for the param given in the API call
    :param request: request object to be populated
    :param models: models tuple containing all complex model types
    :type models: namedtuple
    """
    # If param not given in args, and not required, just ignore.
    if not param.get('required') and not value:
        return

    pname = param['name']
    type_ = swagger_type.get_swagger_type(param)
    param_req_type = param['paramType']

    if param_req_type == 'path':
        # Parameters in path need to be primitive/array types
        if swagger_type.is_complex(type_):
            raise TypeError("Param %s in path can only be primitive/list" %
                            pname)
    elif param_req_type == 'query':
        # Parameters in query need to be only primitive types
        if not swagger_type.is_primitive(type_):
            raise TypeError("Param %s in query can only be primitive" % pname)

    # Allow lists for query params even if type is primitive
    if isinstance(value, list) and param_req_type == 'query':
        type_ = swagger_type.ARRAY + swagger_type.COLON + type_

    # Check the parameter value against its type
    # And store the refined value back
    value = SwaggerTypeCheck(pname, value, type_, models).value

    # If list in path, Turn list items into comma separated values
    if isinstance(value, list) and param_req_type == 'path':
        value = u",".join(str(x) for x in value)

    # Add the parameter value to the request object
    if value is not None:
        add_param_to_req(param, value, request)
    else:
        if param.get(u'required'):
            raise TypeError(u"Missing required parameter '%s'" % pname)
コード例 #9
0
def add_param_to_req(param, value, request):
    """Populates request object with the request parameters

    :param param: swagger spec details of a param
    :type param: dict
    :param value: value for the param given in the API call
    :param request: request object to be populated
    """
    pname = param['name']
    type_ = swagger_type.get_swagger_type(param)
    param_req_type = param['paramType']

    if param_req_type == u'path':
        request['url'] = request['url'].replace(u'{%s}' % pname,
                                                urllib.quote(unicode(value)))
    elif param_req_type == u'query':
        request['params'][pname] = value
    elif param_req_type == u'body':
        if not swagger_type.is_primitive(type_):
            # If not primitive, body has to be 'dict'
            # (or has already been converted to dict from model)
            request['headers']['content-type'] = APP_JSON
            request['data'] = json.dumps(value)
        else:
            request['data'] = stringify_body(value)
    elif param_req_type == 'form':
        handle_form_param(pname, value, type_, request)
    elif param_req_type == 'header':
        if pname in request['headers']:
            log.warn(u'Header {0}:{1} has been overridden by {0}:{2}'.format(
                pname, value, request['headers'][pname]))
        else:
            request['headers'][pname] = value
    else:
        raise AssertionError(u"Unsupported Parameter type: %s" %
                             param_req_type)