18 from typing
import Optional, Tuple, Union, BinaryIO
19 from typing_extensions
import Self
20 from pathlib
import Path
21 from contextlib
import ExitStack
23 from ..utils
import guessMimeType
29 Class which describes file which will be uploaded
30 using NetworkManager.genericUpload function.
31 To upload a file either its "filePath" or "fileBytes"
32 must be set, otherwise it will raise an exception.
33 "filePath" will upload the file from the specified path, while
34 "fileBytes" will upload the file directly from the memory.
35 If both parameters have value "fileBytes" will be used.
37 Objects of this class should not be instantiated directly,
38 use either "FileData.createFromPath" or "FileData.createFromBytes"
39 to instantiate the object.
44 Name of the form-data parameter
46 Name of the file which will be uploaded
48 Mime type of the file which will be uploaded
49 filePath : Optional[str]
50 Path to the file which will be uploaded
51 fileBytes : Optional[bytes]
52 Bytes of the file which will be uploaded
60 filePath: Optional[Path] =
None,
61 fileBytes: Optional[bytes] =
None
64 if filePath
is None and fileBytes
is None:
65 raise ValueError(
">> [Coretex] Either \"filePath\" or \"fileData\" have to provided for file upload. \"fileData\" will be used if both are provided")
77 filePath: Union[Path, str],
78 fileName: Optional[str] =
None,
79 mimeType: Optional[str] =
None
83 Creates "FileData" object from the specified file path. Use
84 this function if you want to upload a file directly from path.
89 Name of the form-data parameter
90 filePath : Union[Path, str]
91 Path to the file which will be uploaded
92 fileName : Optional[str]
93 Name of the file which will be uploaded, if None it will
94 be extracted from the "filePath" parameter
95 mimeType : Optional[str]
96 Mime type of the file which will be uploaded, if None it will
97 be guessed. If it is not possible to guess an exception will be
98 raised. In that case provide the mime type manually.
102 Self -> on object which describes how a path should be uploaded from path
106 ValueError -> if "filePath" is not a valid file
109 if isinstance(filePath, str):
110 filePath = Path(filePath)
112 if not filePath.is_file():
113 raise ValueError(
">> [Coretex] \"filePath\" is not a valid file")
116 fileName = filePath.stem
119 mimeType = guessMimeType(filePath)
121 return cls(parameterName, fileName, mimeType, filePath = filePath)
129 mimeType: Optional[str] =
None
133 Creates "FileData" object from the provided bytes. Use this
134 function if you want to upload a file directly from memory.
139 Name of the form-data parameter
141 Bytes of the file which will be uploaded
143 Name of the file which will be uploaded, if None it will
144 be extracted from the "filePath" parameter
145 mimeType : Optional[str]
146 Mime type of the file which will be uploaded, if None it will
147 be set to "application/octet-stream".
151 Self -> on object which describes how a path should be uploaded from memory
155 mimeType =
"application/octet-stream"
157 return cls(parameterName, fileName, mimeType, fileBytes = fileBytes)
159 def __getFileData(self, exitStack: ExitStack) -> Union[bytes, BinaryIO]:
163 if self.
filePathfilePath
is not None:
164 return exitStack.enter_context(self.
filePathfilePath.open(
"rb"))
166 raise ValueError(
">> [Coretex] Either \"filePath\" or \"fileData\" have to provided for file upload. \"fileData\" will be used if both are provided")
168 def prepareForUpload(self, exitStack: ExitStack) -> Tuple[str, Tuple[str, Union[bytes, BinaryIO], str]]:
170 Converts the "FileData" object into a format which can be used
171 by the requests library for uploading files.
175 exitStack : ExitStack
176 Context stack which contains the context of files
177 opened by the "FileData" object. Used to join multiple file
178 contexts, so if one raises an exception all the files will
183 Tuple[str, Tuple[str, Any, str]] -> Format accepted by the requests
184 library for uploading files.
Union[bytes, BinaryIO] __getFileData(self, ExitStack exitStack)
Self createFromPath(cls, str parameterName, Union[Path, str] filePath, Optional[str] fileName=None, Optional[str] mimeType=None)
Self createFromBytes(cls, str parameterName, bytes fileBytes, str fileName, Optional[str] mimeType=None)
Tuple[str, Tuple[str, Union[bytes, BinaryIO], str]] prepareForUpload(self, ExitStack exitStack)