简体   繁体   中英

How do I call a api client generated method with header/body params?

Given this API client class generated via swagger codege 3.0

# coding: utf-8


from __future__ import absolute_import

import re  # noqa: F401

# python 2 and python 3 compatibility library
import six

from swagger_client.api_client import ApiClient


class DummyKeyApi(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    Ref: https://github.com/swagger-api/swagger-codegen
    """

    def __init__(self, api_client=None):
        if api_client is None:
            api_client = ApiClient()
        self.api_client = api_client

    def delete_license_key(self, **kwargs):  # noqa: E501
        """Disable license key by token  # noqa: E501

        Disable license key by token  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.delete_license_key(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param PartnerLicenseKeyBody1 body:
        :return: Model200
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('async_req'):
            return self.delete_license_key_with_http_info(**kwargs)  # noqa: E501
        else:
            (data) = self.delete_license_key_with_http_info(**kwargs)  # noqa: E501
            return data

    def delete_license_key_with_http_info(self, **kwargs):  # noqa: E501
        """Disable dummy key by token  # noqa: E501

        Disable dummy key by token  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.delete_dummy_key_with_http_info(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param PartnerdummyKeyBody1 body:
        :return: Model200
                 If the method is called asynchronously,
                 returns the request thread.
        """

        all_params = ['body']  # noqa: E501
        all_params.append('async_req')
        all_params.append('_return_http_data_only')
        all_params.append('_preload_content')
        all_params.append('_request_timeout')

        params = locals()
        for key, val in six.iteritems(params['kwargs']):
            if key not in all_params:
                raise TypeError(
                    "Got an unexpected keyword argument '%s'"
                    " to method delete_dummy_key" % key
                )
            params[key] = val
        del params['kwargs']

        collection_formats = {}

        path_params = {}

        query_params = []

        header_params = {}

        form_params = []
        local_var_files = {}

        body_params = None
        if 'body' in params:
            body_params = params['body']
        # HTTP header `Accept`
        header_params['Accept'] = self.api_client.select_header_accept(
            ['application/json'])  # noqa: E501

        # HTTP header `Content-Type`
        header_params['Content-Type'] = self.api_client.select_header_content_type(  # noqa: E501
            ['application/json'])  # noqa: E501

        # Authentication setting
        auth_settings = ['bearerAuth']  # noqa: E501

        return self.api_client.call_api(
            '/partner/dummy_key', 'DELETE',
            path_params,
            query_params,
            header_params,
            body=body_params,
            post_params=form_params,
            files=local_var_files,
            response_type='Model200',  # noqa: E501
            auth_settings=auth_settings,
            async_req=params.get('async_req'),
            _return_http_data_only=params.get('_return_http_data_only'),
            _preload_content=params.get('_preload_content', True),
            _request_timeout=params.get('_request_timeout'),
            collection_formats=collection_formats)

    def get_dummy_key(self, **kwargs):  # noqa: E501
        """Get dummy key  # noqa: E501

        Get dummy key by given token  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.get_dummy_key(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param str token: dummy key token
        :return: PartnerdummyKey
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('async_req'):
            return self.get_dummy_key_with_http_info(**kwargs)  # noqa: E501
        else:
            (data) = self.get_dummy_key_with_http_info(**kwargs)  # noqa: E501
            return data

    def get_dummy_key_with_http_info(self, **kwargs):  # noqa: E501
        """Get dummy key  # noqa: E501

        Get dummy key by given token  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.get_dummy_key_with_http_info(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param str token: dummy key token
        :return: PartnerdummyKey
                 If the method is called asynchronously,
                 returns the request thread.
        """

        all_params = ['token']  # noqa: E501
        all_params.append('async_req')
        all_params.append('_return_http_data_only')
        all_params.append('_preload_content')
        all_params.append('_request_timeout')

        params = locals()
        for key, val in six.iteritems(params['kwargs']):
            if key not in all_params:
                raise TypeError(
                    "Got an unexpected keyword argument '%s'"
                    " to method get_dummy_key" % key
                )
            params[key] = val
        del params['kwargs']

        collection_formats = {}

        path_params = {}

        query_params = []
        if 'token' in params:
            query_params.append(('token', params['token']))  # noqa: E501

        header_params = {}

        form_params = []
        local_var_files = {}

        body_params = None
        # HTTP header `Accept`
        header_params['Accept'] = self.api_client.select_header_accept(
            ['application/json'])  # noqa: E501

        # Authentication setting
        auth_settings = ['bearerAuth']  # noqa: E501

        return self.api_client.call_api(
            '/partner/dummy_key', 'GET',
            path_params,
            query_params,
            header_params,
            body=body_params,
            post_params=form_params,
            files=local_var_files,
            response_type='PartnerdummyKey',  # noqa: E501
            auth_settings=auth_settings,
            async_req=params.get('async_req'),
            _return_http_data_only=params.get('_return_http_data_only'),
            _preload_content=params.get('_preload_content', True),
            _request_timeout=params.get('_request_timeout'),
            collection_formats=collection_formats)

    def get_dummy_key_list(self, **kwargs):  # noqa: E501
        """Get paginated project dummy key list  # noqa: E501

        Get paginated project dummy key list  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.get_dummy_key_list(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param int limit: Page list size limit
        :param str last_token: Last dummy key token returned from previous call
        :return: PartnerdummyKeyList
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('async_req'):
            return self.get_dummy_key_list_with_http_info(**kwargs)  # noqa: E501
        else:
            (data) = self.get_dummy_key_list_with_http_info(**kwargs)  # noqa: E501
            return data

    def get_dummy_key_list_with_http_info(self, **kwargs):  # noqa: E501
        """Get paginated project dummy key list  # noqa: E501

        Get paginated project dummy key list  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.get_dummy_key_list_with_http_info(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param int limit: Page list size limit
        :param str last_token: Last dummy key token returned from previous call
        :return: PartnerdummyKeyList
                 If the method is called asynchronously,
                 returns the request thread.
        """

        all_params = ['limit', 'last_token']  # noqa: E501
        all_params.append('async_req')
        all_params.append('_return_http_data_only')
        all_params.append('_preload_content')
        all_params.append('_request_timeout')

        params = locals()
        for key, val in six.iteritems(params['kwargs']):
            if key not in all_params:
                raise TypeError(
                    "Got an unexpected keyword argument '%s'"
                    " to method get_dummy_key_list" % key
                )
            params[key] = val
        del params['kwargs']

        collection_formats = {}

        path_params = {}

        query_params = []
        if 'limit' in params:
            query_params.append(('limit', params['limit']))  # noqa: E501
        if 'last_token' in params:
            query_params.append(('last_token', params['last_token']))  # noqa: E501

        header_params = {}

        form_params = []
        local_var_files = {}

        body_params = None
        # HTTP header `Accept`
        header_params['Accept'] = self.api_client.select_header_accept(
            ['application/json'])  # noqa: E501

        # Authentication setting
        auth_settings = ['bearerAuth']  # noqa: E501

        return self.api_client.call_api(
            '/partner/dummy_key/list', 'GET',
            path_params,
            query_params,
            header_params,
            body=body_params,
            post_params=form_params,
            files=local_var_files,
            response_type='PartnerdummyKeyList',  # noqa: E501
            auth_settings=auth_settings,
            async_req=params.get('async_req'),
            _return_http_data_only=params.get('_return_http_data_only'),
            _preload_content=params.get('_preload_content', True),
            _request_timeout=params.get('_request_timeout'),
            collection_formats=collection_formats)

    def post_dummy_key(self, **kwargs):  # noqa: E501
        """Create or update existed dummy key  # noqa: E501

        Create or update existed dummy key  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.post_dummy_key(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param PartnerdummyKeyBody body:
        :return: InlineResponse2001
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('async_req'):
            return self.post_dummy_key_with_http_info(**kwargs)  # noqa: E501
        else:
            (data) = self.post_dummy_key_with_http_info(**kwargs)  # noqa: E501
            return data

    def post_dummy_key_with_http_info(self, **kwargs):  # noqa: E501
        """Create or update existed dummy key  # noqa: E501

        Create or update existed dummy key  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.post_dummy_key_with_http_info(async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param PartnerdummyKeyBody body:
        :return: InlineResponse2001
                 If the method is called asynchronously,
                 returns the request thread.
        """

        all_params = ['body']  # noqa: E501
        all_params.append('async_req')
        all_params.append('_return_http_data_only')
        all_params.append('_preload_content')
        all_params.append('_request_timeout')

        params = locals()
        for key, val in six.iteritems(params['kwargs']):
            if key not in all_params:
                raise TypeError(
                    "Got an unexpected keyword argument '%s'"
                    " to method post_dummy_key" % key
                )
            params[key] = val
        del params['kwargs']

        collection_formats = {}

        path_params = {}

        query_params = []

        header_params = {}

        form_params = []
        local_var_files = {}

        body_params = None
        if 'body' in params:
            body_params = params['body']
        # HTTP header `Accept`
        header_params['Accept'] = self.api_client.select_header_accept(
            ['application/json'])  # noqa: E501

        # HTTP header `Content-Type`
        header_params['Content-Type'] = self.api_client.select_header_content_type(  # noqa: E501
            ['application/json'])  # noqa: E501

        # Authentication setting
        auth_settings = ['bearerAuth']  # noqa: E501

        return self.api_client.call_api(
            '/partner/dummy_key', 'POST',
            path_params,
            query_params,
            header_params,
            body=body_params,
            post_params=form_params,
            files=local_var_files,
            response_type='InlineResponse2001',  # noqa: E501
            auth_settings=auth_settings,
            async_req=params.get('async_req'),
            _return_http_data_only=params.get('_return_http_data_only'),
            _preload_content=params.get('_preload_content', True),
            _request_timeout=params.get('_request_timeout'),
            collection_formats=collection_formats)

I tried:

headers is:

        headers = {
            "Authorization": f"Bearer ASDQWE123",
        }

body is:

        self.body = {
            "dummyKey": "asdqwe123",
            "some_data": {},
            "exp": 0
        }
        dummy_api = dummy_key_api.LisenseKeyApi()
        print(dummy_api.post_dummy_key_with_http_info(body=self.body, headers=headers))

But raises:

 TypeError: Got an unexpected keyword argument 'headers' to method post_license_key

Bcs there is a check in post_license_key_with_http_info method for if key not in all_params: but all_params = ['body'] # noqa: E501

Basically i just want to know how can i give proper values to path_params, query_params, header_params, body_params when calling post_license_key_with_http_info

I would expect to just give as input attrs for the method as header=, body=, but it has a check for all_params only containing "body" key therefore i cannot specify a header value at all

In your functions you have lines that checks kwargs and raises an TypeError if kwargs contains unexpected arguments.

for key, val in six.iteritems(params['kwargs']):
            if key not in all_params:
                raise TypeError(
                    "Got an unexpected keyword argument '%s'"
                    " to method ..." % key
                )
            params[key] = val

You need to append headers to expected values

all_params.append('headers')

And then unpack this params into header_params in functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM