github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/python/templates/client.mustache (about)

     1  import warnings
     2  
     3  from urllib3.util import parse_url, Url
     4  from lakefs_sdk import ApiClient
     5  {{#apiInfo}}
     6  {{#apis}}
     7  from {{apiPackage}} import {{classFilename}}
     8  {{/apis}}
     9  {{/apiInfo}}
    10  
    11  
    12  class _WrappedApiClient(ApiClient):
    13      """ApiClient that fixes some weirdness"""
    14  
    15      def files_parameters(self, files=None):
    16          """
    17          Transforms input file data into a formatted list to return file_parameters.
    18          Assume a string file_name is a path to the file to read.
    19          Assume a bytes file_name is a file-like object that we append the information.
    20          The parent class will handle the files to read.
    21          """
    22          if not files:
    23              return []
    24  
    25          params = []
    26          files_to_read = {}
    27  
    28          for idx, (key, value) in enumerate(files.items()):
    29              if not value:
    30                  continue
    31  
    32              # Ensure the value is always a list.
    33              file_names = value if isinstance(value, list) else [value]
    34  
    35              for file_name in file_names:
    36                  if type(file_name) is str:
    37                      files_to_read[key] = file_name
    38                  else:
    39                      name = f'{key}_{idx}'
    40                      mimetype = 'application/octet-stream'
    41                      params.append(tuple([key, tuple([name, value, mimetype])]))
    42  
    43          return super().files_parameters(files_to_read) + params
    44  
    45  class LakeFSClient:
    46      def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None, pool_threads=1):
    47          configuration = LakeFSClient._ensure_endpoint(configuration)
    48          self._api = _WrappedApiClient(configuration=configuration, header_name=header_name,
    49                                            header_value=header_value, cookie=cookie, pool_threads=pool_threads)
    50  {{#apiInfo}}
    51  {{#apis}}
    52          self.{{classFilename}} = {{classFilename}}.{{{classname}}}(self._api)
    53  {{/apis}}
    54  {{/apiInfo}}
    55  
    56      @staticmethod
    57      def _ensure_endpoint(configuration):
    58          """Normalize lakefs connection endpoint found in configuration's host"""
    59          if not configuration or not configuration.host:
    60              return configuration
    61          try:
    62              # prefix http scheme if missing
    63              if not configuration.host.startswith('http://') and not configuration.host.startswith('https://'):
    64                  configuration.host = 'http://' + configuration.host
    65              # if 'host' not set any 'path', format the endpoint url with default 'path' based on the generated code
    66              o = parse_url(configuration.host)
    67              if not o.path or o.path == '/':
    68                  settings = configuration.get_host_settings()
    69                  if settings:
    70                      base_path = parse_url(settings[0].get('url')).path
    71                      configuration.host = Url(scheme=o.scheme, auth=o.auth, host=o.host, port=o.port,
    72                                               path=base_path, query=o.query, fragment=o.fragment).url
    73          except ValueError:
    74              pass
    75          return configuration