Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
api_client.py648 linesDownload Raw Back to client
1# coding: utf-82"""3    Kubernetes4 5    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)  # noqa: E5016 7    The version of the OpenAPI document: release-1.358    Generated by: https://openapi-generator.tech9"""10 11from __future__ import absolute_import12 13import atexit14import datetime15from dateutil.parser import parse16import json17import mimetypes18from multiprocessing.pool import ThreadPool19import os20import re21import tempfile22 23# python 2 and python 3 compatibility library24import six25from six.moves.urllib.parse import quote26 27from kubernetes.client.configuration import Configuration28import kubernetes.client.models29from kubernetes.client import rest30from kubernetes.client.exceptions import ApiValueError31 32 33class ApiClient(object):34    """Generic API client for OpenAPI client library builds.35 36    OpenAPI generic API client. This client handles the client-37    server communication, and is invariant across implementations. Specifics of38    the methods and models for each application are generated from the OpenAPI39    templates.40 41    NOTE: This class is auto generated by OpenAPI Generator.42    Ref: https://openapi-generator.tech43    Do not edit the class manually.44 45    :param configuration: .Configuration object for this client46    :param header_name: a header to pass when making calls to the API.47    :param header_value: a header value to pass when making calls to48        the API.49    :param cookie: a cookie to include in the header when making calls50        to the API51    :param pool_threads: The number of threads to use for async requests52        to the API. More threads means more concurrent API requests.53    """54 55    PRIMITIVE_TYPES = (float, bool, bytes, six.text_type) + six.integer_types56    NATIVE_TYPES_MAPPING = {57        'int': int,58        'long': int if six.PY3 else long,  # noqa: F82159        'float': float,60        'str': str,61        'bool': bool,62        'date': datetime.date,63        'datetime': datetime.datetime,64        'object': object,65    }66    _pool = None67 68    def __init__(self, configuration=None, header_name=None, header_value=None,69                 cookie=None, pool_threads=1):70        if configuration is None:71            configuration = Configuration.get_default_copy()72        self.configuration = configuration73        self.pool_threads = pool_threads74 75        self.rest_client = rest.RESTClientObject(configuration)76        self.default_headers = {}77        if header_name is not None:78            self.default_headers[header_name] = header_value79        self.cookie = cookie80        # Set default User-Agent.81        self.user_agent = 'OpenAPI-Generator/35.0.0/python'82        self.client_side_validation = configuration.client_side_validation83 84    def __enter__(self):85        return self86 87    def __exit__(self, exc_type, exc_value, traceback):88        self.close()89 90    def close(self):91        if self._pool:92            self._pool.close()93            self._pool.join()94            self._pool = None95            if hasattr(atexit, 'unregister'):96                atexit.unregister(self.close)97 98    @property99    def pool(self):100        """Create thread pool on first request101         avoids instantiating unused threadpool for blocking clients.102        """103        if self._pool is None:104            atexit.register(self.close)105            self._pool = ThreadPool(self.pool_threads)106        return self._pool107 108    @property109    def user_agent(self):110        """User agent for this API client"""111        return self.default_headers['User-Agent']112 113    @user_agent.setter114    def user_agent(self, value):115        self.default_headers['User-Agent'] = value116 117    def set_default_header(self, header_name, header_value):118        self.default_headers[header_name] = header_value119 120    def __call_api(121            self, resource_path, method, path_params=None,122            query_params=None, header_params=None, body=None, post_params=None,123            files=None, response_type=None, auth_settings=None,124            _return_http_data_only=None, collection_formats=None,125            _preload_content=True, _request_timeout=None, _host=None):126 127        config = self.configuration128 129        # header parameters130        header_params = header_params or {}131        header_params.update(self.default_headers)132        if self.cookie:133            header_params['Cookie'] = self.cookie134        if header_params:135            header_params = self.sanitize_for_serialization(header_params)136            header_params = dict(self.parameters_to_tuples(header_params,137                                                           collection_formats))138 139        # path parameters140        if path_params:141            path_params = self.sanitize_for_serialization(path_params)142            path_params = self.parameters_to_tuples(path_params,143                                                    collection_formats)144            for k, v in path_params:145                # specified safe chars, encode everything146                resource_path = resource_path.replace(147                    '{%s}' % k,148                    quote(str(v), safe=config.safe_chars_for_path_param)149                )150 151        # query parameters152        if query_params:153            query_params = self.sanitize_for_serialization(query_params)154            query_params = self.parameters_to_tuples(query_params,155                                                     collection_formats)156 157        # post parameters158        if post_params or files:159            post_params = post_params if post_params else []160            post_params = self.sanitize_for_serialization(post_params)161            post_params = self.parameters_to_tuples(post_params,162                                                    collection_formats)163            post_params.extend(self.files_parameters(files))164 165        # auth setting166        self.update_params_for_auth(header_params, query_params, auth_settings)167 168        # body169        if body:170            body = self.sanitize_for_serialization(body)171 172        # request url173        if _host is None:174            url = self.configuration.host + resource_path175        else:176            # use server/host defined in path or operation instead177            url = _host + resource_path178 179        # perform request and return response180        response_data = self.request(181            method, url, query_params=query_params, headers=header_params,182            post_params=post_params, body=body,183            _preload_content=_preload_content,184            _request_timeout=_request_timeout)185 186        self.last_response = response_data187 188        return_data = response_data189        if _preload_content:190            # deserialize response data191            if response_type:192                return_data = self.deserialize(response_data, response_type)193            else:194                return_data = None195 196        if _return_http_data_only:197            return (return_data)198        else:199            return (return_data, response_data.status,200                    response_data.getheaders())201 202    def sanitize_for_serialization(self, obj):203        """Builds a JSON POST object.204 205        If obj is None, return None.206        If obj is str, int, long, float, bool, return directly.207        If obj is datetime.datetime, datetime.date208            convert to string in iso8601 format.209        If obj is list, sanitize each element in the list.210        If obj is dict, return the dict.211        If obj is OpenAPI model, return the properties dict.212 213        :param obj: The data to serialize.214        :return: The serialized form of data.215        """216        if obj is None:217            return None218        elif isinstance(obj, self.PRIMITIVE_TYPES):219            return obj220        elif isinstance(obj, list):221            return [self.sanitize_for_serialization(sub_obj)222                    for sub_obj in obj]223        elif isinstance(obj, tuple):224            return tuple(self.sanitize_for_serialization(sub_obj)225                         for sub_obj in obj)226        elif isinstance(obj, (datetime.datetime, datetime.date)):227            return obj.isoformat()228 229        if isinstance(obj, dict):230            obj_dict = obj231        else:232            # Convert model obj to dict except233            # attributes `openapi_types`, `attribute_map`234            # and attributes which value is not None.235            # Convert attribute name to json key in236            # model definition for request.237            obj_dict = {obj.attribute_map[attr]: getattr(obj, attr)238                        for attr, _ in six.iteritems(obj.openapi_types)239                        if getattr(obj, attr) is not None}240 241        return {key: self.sanitize_for_serialization(val)242                for key, val in six.iteritems(obj_dict)}243 244    def deserialize(self, response, response_type):245        """Deserializes response into an object.246 247        :param response: RESTResponse object to be deserialized.248        :param response_type: class literal for249            deserialized object, or string of class name.250 251        :return: deserialized object.252        """253        # handle file downloading254        # save response body into a tmp file and return the instance255        if response_type == "file":256            return self.__deserialize_file(response)257 258        # fetch data from response object259        try:260            data = json.loads(response.data)261        except ValueError:262            data = response.data263 264        return self.__deserialize(data, response_type)265 266    def __deserialize(self, data, klass):267        """Deserializes dict, list, str into an object.268 269        :param data: dict, list or str.270        :param klass: class literal, or string of class name.271 272        :return: object.273        """274        if data is None:275            return None276 277        if type(klass) == str:278            if klass.startswith('list['):279                sub_kls = re.match(r'list\[(.*)\]', klass).group(1)280                return [self.__deserialize(sub_data, sub_kls)281                        for sub_data in data]282 283            if klass.startswith('dict('):284                sub_kls = re.match(r'dict\(([^,]*), (.*)\)', klass).group(2)285                return {k: self.__deserialize(v, sub_kls)286                        for k, v in six.iteritems(data)}287 288            # convert str to class289            if klass in self.NATIVE_TYPES_MAPPING:290                klass = self.NATIVE_TYPES_MAPPING[klass]291            else:292                klass = getattr(kubernetes.client.models, klass)293 294        if klass in self.PRIMITIVE_TYPES:295            return self.__deserialize_primitive(data, klass)296        elif klass == object:297            return self.__deserialize_object(data)298        elif klass == datetime.date:299            return self.__deserialize_date(data)300        elif klass == datetime.datetime:301            return self.__deserialize_datetime(data)302        else:303            return self.__deserialize_model(data, klass)304 305    def call_api(self, resource_path, method,306                 path_params=None, query_params=None, header_params=None,307                 body=None, post_params=None, files=None,308                 response_type=None, auth_settings=None, async_req=None,309                 _return_http_data_only=None, collection_formats=None,310                 _preload_content=True, _request_timeout=None, _host=None):311        """Makes the HTTP request (synchronous) and returns deserialized data.312 313        To make an async_req request, set the async_req parameter.314 315        :param resource_path: Path to method endpoint.316        :param method: Method to call.317        :param path_params: Path parameters in the url.318        :param query_params: Query parameters in the url.319        :param header_params: Header parameters to be320            placed in the request header.321        :param body: Request body.322        :param post_params dict: Request post form parameters,323            for `application/x-www-form-urlencoded`, `multipart/form-data`.324        :param auth_settings list: Auth Settings names for the request.325        :param response: Response data type.326        :param files dict: key -> filename, value -> filepath,327            for `multipart/form-data`.328        :param async_req bool: execute request asynchronously329        :param _return_http_data_only: response data without head status code330                                       and headers331        :param collection_formats: dict of collection formats for path, query,332            header, and post parameters.333        :param _preload_content: if False, the urllib3.HTTPResponse object will334                                 be returned without reading/decoding response335                                 data. Default is True.336        :param _request_timeout: timeout setting for this request. If one337                                 number provided, it will be total request338                                 timeout. It can also be a pair (tuple) of339                                 (connection, read) timeouts.340        :return:341            If async_req parameter is True,342            the request will be called asynchronously.343            The method will return the request thread.344            If parameter async_req is False or missing,345            then the method will return the response directly.346        """347        if not async_req:348            return self.__call_api(resource_path, method,349                                   path_params, query_params, header_params,350                                   body, post_params, files,351                                   response_type, auth_settings,352                                   _return_http_data_only, collection_formats,353                                   _preload_content, _request_timeout, _host)354 355        return self.pool.apply_async(self.__call_api, (resource_path,356                                                       method, path_params,357                                                       query_params,358                                                       header_params, body,359                                                       post_params, files,360                                                       response_type,361                                                       auth_settings,362                                                       _return_http_data_only,363                                                       collection_formats,364                                                       _preload_content,365                                                       _request_timeout,366                                                       _host))367 368    def request(self, method, url, query_params=None, headers=None,369                post_params=None, body=None, _preload_content=True,370                _request_timeout=None):371        """Makes the HTTP request using RESTClient."""372        if method == "GET":373            return self.rest_client.GET(url,374                                        query_params=query_params,375                                        _preload_content=_preload_content,376                                        _request_timeout=_request_timeout,377                                        headers=headers)378        elif method == "HEAD":379            return self.rest_client.HEAD(url,380                                         query_params=query_params,381                                         _preload_content=_preload_content,382                                         _request_timeout=_request_timeout,383                                         headers=headers)384        elif method == "OPTIONS":385            return self.rest_client.OPTIONS(url,386                                            query_params=query_params,387                                            headers=headers,388                                            _preload_content=_preload_content,389                                            _request_timeout=_request_timeout)390        elif method == "POST":391            return self.rest_client.POST(url,392                                         query_params=query_params,393                                         headers=headers,394                                         post_params=post_params,395                                         _preload_content=_preload_content,396                                         _request_timeout=_request_timeout,397                                         body=body)398        elif method == "PUT":399            return self.rest_client.PUT(url,400                                        query_params=query_params,401                                        headers=headers,402                                        post_params=post_params,403                                        _preload_content=_preload_content,404                                        _request_timeout=_request_timeout,405                                        body=body)406        elif method == "PATCH":407            return self.rest_client.PATCH(url,408                                          query_params=query_params,409                                          headers=headers,410                                          post_params=post_params,411                                          _preload_content=_preload_content,412                                          _request_timeout=_request_timeout,413                                          body=body)414        elif method == "DELETE":415            return self.rest_client.DELETE(url,416                                           query_params=query_params,417                                           headers=headers,418                                           _preload_content=_preload_content,419                                           _request_timeout=_request_timeout,420                                           body=body)421        else:422            raise ApiValueError(423                "http method must be `GET`, `HEAD`, `OPTIONS`,"424                " `POST`, `PATCH`, `PUT` or `DELETE`."425            )426 427    def parameters_to_tuples(self, params, collection_formats):428        """Get parameters as list of tuples, formatting collections.429 430        :param params: Parameters as dict or list of two-tuples431        :param dict collection_formats: Parameter collection formats432        :return: Parameters as list of tuples, collections formatted433        """434        new_params = []435        if collection_formats is None:436            collection_formats = {}437        for k, v in six.iteritems(params) if isinstance(params, dict) else params:  # noqa: E501438            if k in collection_formats:439                collection_format = collection_formats[k]440                if collection_format == 'multi':441                    new_params.extend((k, value) for value in v)442                else:443                    if collection_format == 'ssv':444                        delimiter = ' '445                    elif collection_format == 'tsv':446                        delimiter = '\t'447                    elif collection_format == 'pipes':448                        delimiter = '|'449                    else:  # csv is the default450                        delimiter = ','451                    new_params.append(452                        (k, delimiter.join(str(value) for value in v)))453            else:454                new_params.append((k, v))455        return new_params456 457    def files_parameters(self, files=None):458        """Builds form parameters.459 460        :param files: File parameters.461        :return: Form parameters with files.462        """463        params = []464 465        if files:466            for k, v in six.iteritems(files):467                if not v:468                    continue469                file_names = v if type(v) is list else [v]470                for n in file_names:471                    with open(n, 'rb') as f:472                        filename = os.path.basename(f.name)473                        filedata = f.read()474                        mimetype = (mimetypes.guess_type(filename)[0] or475                                    'application/octet-stream')476                        params.append(477                            tuple([k, tuple([filename, filedata, mimetype])]))478 479        return params480 481    def select_header_accept(self, accepts):482        """Returns `Accept` based on an array of accepts provided.483 484        :param accepts: List of headers.485        :return: Accept (e.g. application/json).486        """487        if not accepts:488            return489 490        accepts = [x.lower() for x in accepts]491 492        if 'application/json' in accepts:493            return 'application/json'494        else:495            return ', '.join(accepts)496 497    def select_header_content_type(self, content_types):498        """Returns `Content-Type` based on an array of content_types provided.499 500        :param content_types: List of content-types.501        :return: Content-Type (e.g. application/json).502        """503        if not content_types:504            return 'application/json'505 506        content_types = [x.lower() for x in content_types]507 508        if 'application/json' in content_types or '*/*' in content_types:509            return 'application/json'510        else:511            return content_types[0]512 513    def update_params_for_auth(self, headers, querys, auth_settings):514        """Updates header and query params based on authentication setting.515 516        :param headers: Header parameters dict to be updated.517        :param querys: Query parameters tuple list to be updated.518        :param auth_settings: Authentication setting identifiers list.519        """520        if not auth_settings:521            return522 523        for auth in auth_settings:524            auth_setting = self.configuration.auth_settings().get(auth)525            if auth_setting:526                if auth_setting['in'] == 'cookie':527                    headers['Cookie'] = auth_setting['value']528                elif auth_setting['in'] == 'header':529                    headers[auth_setting['key']] = auth_setting['value']530                elif auth_setting['in'] == 'query':531                    querys.append((auth_setting['key'], auth_setting['value']))532                else:533                    raise ApiValueError(534                        'Authentication token must be in `query` or `header`'535                    )536 537    def __deserialize_file(self, response):538        """Deserializes body to file539 540        Saves response body into a file in a temporary folder,541        using the filename from the `Content-Disposition` header if provided.542 543        :param response:  RESTResponse.544        :return: file path.545        """546        fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)547        os.close(fd)548        os.remove(path)549 550        content_disposition = response.getheader("Content-Disposition")551        if content_disposition:552            filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',553                                 content_disposition).group(1)554            path = os.path.join(os.path.dirname(path), filename)555 556        with open(path, "wb") as f:557            f.write(response.data)558 559        return path560 561    def __deserialize_primitive(self, data, klass):562        """Deserializes string to primitive type.563 564        :param data: str.565        :param klass: class literal.566 567        :return: int, long, float, str, bool.568        """569        try:570            return klass(data)571        except UnicodeEncodeError:572            return six.text_type(data)573        except TypeError:574            return data575 576    def __deserialize_object(self, value):577        """Return an original value.578 579        :return: object.580        """581        return value582 583    def __deserialize_date(self, string):584        """Deserializes string to date.585 586        :param string: str.587        :return: date.588        """589        try:590            return parse(string).date()591        except ImportError:592            return string593        except ValueError:594            raise rest.ApiException(595                status=0,596                reason="Failed to parse `{0}` as date object".format(string)597            )598 599    def __deserialize_datetime(self, string):600        """Deserializes string to datetime.601 602        The string should be in iso8601 datetime format.603 604        :param string: str.605        :return: datetime.606        """607        try:608            return parse(string)609        except ImportError:610            return string611        except ValueError:612            raise rest.ApiException(613                status=0,614                reason=(615                    "Failed to parse `{0}` as datetime object"616                    .format(string)617                )618            )619 620    def __deserialize_model(self, data, klass):621        """Deserializes list or dict to model.622 623        :param data: dict, list.624        :param klass: class literal.625        :return: model object.626        """627 628        if not klass.openapi_types and not hasattr(klass,629                                                   'get_real_child_model'):630            return data631 632        kwargs = {}633        if (data is not None and634                klass.openapi_types is not None and635                isinstance(data, (list, dict))):636            for attr, attr_type in six.iteritems(klass.openapi_types):637                if klass.attribute_map[attr] in data:638                    value = data[klass.attribute_map[attr]]639                    kwargs[attr] = self.__deserialize(value, attr_type)640 641        instance = klass(**kwargs)642 643        if hasattr(instance, 'get_real_child_model'):644            klass_name = instance.get_real_child_model(data)645            if klass_name:646                instance = self.__deserialize(data, klass_name)647        return instance648 
codekingpro/portable-devtools · Team Ai