2626from __future__ import absolute_import , division
2727
2828import datetime
29+ import io
2930import json
3031import mimetypes
32+ from collections import namedtuple
3133from multiprocessing .pool import ThreadPool
3234import os
3335import re
3739import six
3840from six .moves .urllib .parse import quote
3941
42+ if not six .PY2 :
43+ from typing import Union
44+ import pathlib
45+
4046from aspose_barcode_cloud .configuration import Configuration
4147import aspose_barcode_cloud .models
42- from aspose_barcode_cloud import rest
48+ from aspose_barcode_cloud .rest import RESTClientObject , ApiException
49+
50+ FileFieldData = namedtuple ("FileFieldData" , ("file_name" , "file_bytes" , "mime_type" ))
4351
4452
4553class ApiClient (object ):
4654 """Generic API client for Swagger client library builds.
4755
48- Swagger generic API client. This client handles the client-
49- server communication, and is invariant across implementations. Specifics of
50- the methods and models for each application are generated from the Swagger
51- templates.
56+ Swagger generic API client. This client handles the client-server communication,
57+ and is invariant across implementations. Specifics of the methods and
58+ models for each application are generated from the Swagger templates.
5259
5360 NOTE: This class is auto generated by the swagger code generator program.
5461 Ref: https://github.com/swagger-api/swagger-codegen
@@ -81,7 +88,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None, cook
8188
8289 # Use the pool property to lazily initialize the ThreadPool.
8390 self ._pool = None
84- self .rest_client = rest . RESTClientObject (configuration )
91+ self .rest_client = RESTClientObject (configuration )
8592 self .default_headers = {
8693 "x-aspose-client" : "python sdk" ,
8794 "x-aspose-client-version" : "22.8.0" ,
@@ -485,7 +492,7 @@ def parameters_to_tuples(self, params, collection_formats):
485492 new_params = []
486493 if collection_formats is None :
487494 collection_formats = {}
488- for k , v in six .iteritems (params ) if isinstance (params , dict ) else params : # noqa: E501
495+ for k , v in six .iteritems (params ) if isinstance (params , dict ) else params :
489496 if k in collection_formats :
490497 collection_format = collection_formats [k ]
491498 if collection_format == "multi" :
@@ -504,29 +511,48 @@ def parameters_to_tuples(self, params, collection_formats):
504511 new_params .append ((k , v ))
505512 return new_params
506513
514+ def is_not_ascii (self , string ):
515+ return any (ord (c ) >= 128 for c in string )
516+
517+ def prepare_one_file (self , file_data ):
518+ # type: (Union[bytes, str, file, pathlib.Path, io.BytesIO]) -> FileFieldData # noqa: F821
519+
520+ # Python 2 has no difference between Bytes and Str
521+ # So decide non-ascii string is Bytes
522+ if isinstance (file_data , bytes ) and (not six .PY2 or self .is_not_ascii (file_data )):
523+ return FileFieldData ("data.bin" , file_data , "application/octet-stream" )
524+
525+ if isinstance (file_data , str ) or (not six .PY2 and isinstance (file_data , pathlib .PurePath )):
526+ with open (file_data , "rb" ) as f :
527+ fname = os .path .basename (f .name )
528+ file_bytes = f .read ()
529+ mime_type = mimetypes .guess_type (fname )[0 ] or "application/octet-stream"
530+ return FileFieldData (fname , file_bytes , mime_type )
531+
532+ if isinstance (file_data , io .BytesIO ):
533+ return FileFieldData ("data.bin" , file_data .read (), "application/octet-stream" )
534+
535+ if isinstance (file_data , io .BufferedReader ) or (six .PY2 and isinstance (file_data , file )): # noqa: F821
536+ return FileFieldData (os .path .basename (file_data .name ), file_data .read (), "application/octet-stream" )
537+
538+ raise ApiException (reason = "Unknown type {type_name} for file parameter" .format (type_name = type (file_data )))
539+
507540 def prepare_post_parameters (self , post_params = None , files = None ):
508541 """Builds form parameters.
509542
510543 :param post_params: Normal form parameters.
511544 :param files: File parameters.
512545 :return: Form parameters with files.
513546 """
514- params = []
515-
516- if post_params :
517- params = post_params
547+ params = post_params or []
518548
519549 if files :
520- for k , v in six .iteritems (files ):
521- if not v :
550+ for field_name , data in six .iteritems (files ):
551+ if not data :
522552 continue
523- file_names = v if type (v ) is list else [v ]
524- for n in file_names :
525- with open (n , "rb" ) as f :
526- filename = os .path .basename (f .name )
527- filedata = f .read ()
528- mimetype = mimetypes .guess_type (filename )[0 ] or "application/octet-stream"
529- params .append (tuple ([k , tuple ([filename , filedata , mimetype ])]))
553+ file_names = data if type (data ) is list else [data ]
554+ for file_data in file_names :
555+ params .append ((field_name , self .prepare_one_file (file_data )))
530556
531557 return params
532558
@@ -642,7 +668,7 @@ def __deserialize_date(self, string):
642668 except ImportError :
643669 return string
644670 except ValueError :
645- raise rest . ApiException (status = 0 , reason = "Failed to parse '{0}' as date object" .format (string ))
671+ raise ApiException (reason = "Failed to parse '{0}' as date object" .format (string ))
646672
647673 def __deserialize_datetime (self , string ):
648674 """Deserializes string to datetime.
@@ -659,7 +685,7 @@ def __deserialize_datetime(self, string):
659685 except ImportError :
660686 return string
661687 except ValueError :
662- raise rest . ApiException (status = 0 , reason = ("Failed to parse '{0}' as datetime object" .format (string )))
688+ raise ApiException (reason = ("Failed to parse '{0}' as datetime object" .format (string )))
663689
664690 def __hasattr (self , object , name ):
665691 return name in object .__class__ .__dict__
0 commit comments