Skip to content

Commit ce1e2c8

Browse files
committed
Update python input file handling
1 parent e5f46cb commit ce1e2c8

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

templates/python/package/client.py.twig

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Client:
6666
stringify = True
6767
for key in data.copy():
6868
if isinstance(data[key], InputFile):
69-
files[key] = (data[key].name, data[key].file)
69+
files[key] = (data[key].filename, data[key].data)
7070
del data[key]
7171
response = None
7272
try:
@@ -108,21 +108,27 @@ class Client:
108108
on_progress = None,
109109
upload_id = ''
110110
):
111-
file_path = str(params[param_name])
112-
file_name = os.path.basename(file_path)
113-
size = os.stat(file_path).st_size
111+
input_file = params[param_name]
112+
113+
if input_file.source_type == 'path':
114+
size = os.stat(input_file.path).st_size
115+
input = open(input_file.path, 'rb')
116+
elif input_file.source_type == 'bytes':
117+
size = len(input_file.data)
118+
input = input_file.data
114119

115120
if size < self._chunk_size:
116-
slice = open(file_path, 'rb').read()
117-
params[param_name] = InputFile(file_path, file_name, slice)
121+
if input_file.source_type == 'path':
122+
input_file.data = input.read()
123+
124+
params[param_name] = input_file
118125
return self.call(
119126
'post',
120127
path,
121128
headers,
122129
params
123130
)
124131

125-
input = open(file_path, 'rb')
126132
offset = 0
127133
counter = 0
128134

@@ -138,9 +144,16 @@ class Client:
138144
input.seek(offset)
139145

140146
while offset < size:
141-
slice = input.read(self._chunk_size) or input.read(size - offset)
147+
if input_file.source_type == 'path':
148+
input_file.data = input.read(self._chunk_size) or input.read(size - offset)
149+
elif input_file.source_type == 'bytes':
150+
if offset + self._chunk_size < size:
151+
end = offset + self._chunk_size
152+
else:
153+
end = size - offset
154+
input_file.data = input[offset:end]
142155

143-
params[param_name] = InputFile(file_path, file_name, slice)
156+
params[param_name] = input_file
144157
headers["content-range"] = f'bytes {offset}-{min((offset + self._chunk_size) - 1, size)}/{size}'
145158

146159
result = self.call(

0 commit comments

Comments
 (0)