Coretex
coretex.networking.file_data.FileData Class Reference

Public Member Functions

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)
 

Detailed Description

    Class which describes file which will be uploaded
    using NetworkManager.genericUpload function.
    To upload a file either its "filePath" or "fileBytes"
    must be set, otherwise it will raise an exception.
    "filePath" will upload the file from the specified path, while
    "fileBytes" will upload the file directly from the memory.
    If both parameters have value "fileBytes" will be used.

    Objects of this class should not be instantiated directly,
    use either "FileData.createFromPath" or "FileData.createFromBytes"
    to instantiate the object.

    Properties
    ----------
    parameterName : str
        Name of the form-data parameter
    fileName : str
        Name of the file which will be uploaded
    mimeType : str
        Mime type of the file which will be uploaded
    filePath : Optional[str]
        Path to the file which will be uploaded
    fileBytes : Optional[bytes]
        Bytes of the file which will be uploaded

Definition at line 26 of file file_data.py.

Member Function Documentation

◆ createFromBytes()

Self coretex.networking.file_data.FileData.createFromBytes (   cls,
str  parameterName,
bytes  fileBytes,
str  fileName,
Optional[str]   mimeType = None 
)
    Creates "FileData" object from the provided bytes. Use this
    function if you want to upload a file directly from memory.

    Parameters
    ----------
    parameterName : str
        Name of the form-data parameter
    fileBytes : bytes
        Bytes of the file which will be uploaded
    fileName : str
        Name of the file which will be uploaded, if None it will
        be extracted from the "filePath" parameter
    mimeType : Optional[str]
        Mime type of the file which will be uploaded, if None it will
        be set to "application/octet-stream".

    Returns
    -------
    Self -> on object which describes how a path should be uploaded from memory

Definition at line 124 of file file_data.py.

130  ) -> Self:
131 
132  """
133  Creates "FileData" object from the provided bytes. Use this
134  function if you want to upload a file directly from memory.
135 
136  Parameters
137  ----------
138  parameterName : str
139  Name of the form-data parameter
140  fileBytes : bytes
141  Bytes of the file which will be uploaded
142  fileName : str
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".
148 
149  Returns
150  -------
151  Self -> on object which describes how a path should be uploaded from memory
152  """
153 
154  if mimeType is None:
155  mimeType = "application/octet-stream"
156 
157  return cls(parameterName, fileName, mimeType, fileBytes = fileBytes)
158 

◆ createFromPath()

Self coretex.networking.file_data.FileData.createFromPath (   cls,
str  parameterName,
Union[Path, str]  filePath,
Optional[str]   fileName = None,
Optional[str]   mimeType = None 
)
    Creates "FileData" object from the specified file path. Use
    this function if you want to upload a file directly from path.

    Parameters
    ----------
    parameterName : str
        Name of the form-data parameter
    filePath : Union[Path, str]
        Path to the file which will be uploaded
    fileName : Optional[str]
        Name of the file which will be uploaded, if None it will
        be extracted from the "filePath" parameter
    mimeType : Optional[str]
        Mime type of the file which will be uploaded, if None it will
        be guessed. If it is not possible to guess an exception will be
        raised. In that case provide the mime type manually.

    Returns
    -------
    Self -> on object which describes how a path should be uploaded from path

    Raises
    ------
    ValueError -> if "filePath" is not a valid file

Definition at line 74 of file file_data.py.

80  ) -> Self:
81 
82  """
83  Creates "FileData" object from the specified file path. Use
84  this function if you want to upload a file directly from path.
85 
86  Parameters
87  ----------
88  parameterName : str
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.
99 
100  Returns
101  -------
102  Self -> on object which describes how a path should be uploaded from path
103 
104  Raises
105  ------
106  ValueError -> if "filePath" is not a valid file
107  """
108 
109  if isinstance(filePath, str):
110  filePath = Path(filePath)
111 
112  if not filePath.is_file():
113  raise ValueError(">> [Coretex] \"filePath\" is not a valid file")
114 
115  if fileName is None:
116  fileName = filePath.stem
117 
118  if mimeType is None:
119  mimeType = guessMimeType(filePath)
120 
121  return cls(parameterName, fileName, mimeType, filePath = filePath)
122 

◆ prepareForUpload()

Tuple[str, Tuple[str, Union[bytes, BinaryIO], str]] coretex.networking.file_data.FileData.prepareForUpload (   self,
ExitStack  exitStack 
)
    Converts the "FileData" object into a format which can be used
    by the requests library for uploading files.

    Parameters
    ----------
    exitStack : ExitStack
        Context stack which contains the context of files
        opened by the "FileData" object. Used to join multiple file
        contexts, so if one raises an exception all the files will
        properly get closed.

    Returns
    -------
    Tuple[str, Tuple[str, Any, str]] -> Format accepted by the requests
    library for uploading files.

Definition at line 168 of file file_data.py.

168  def prepareForUpload(self, exitStack: ExitStack) -> Tuple[str, Tuple[str, Union[bytes, BinaryIO], str]]:
169  """
170  Converts the "FileData" object into a format which can be used
171  by the requests library for uploading files.
172 
173  Parameters
174  ----------
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
179  properly get closed.
180 
181  Returns
182  -------
183  Tuple[str, Tuple[str, Any, str]] -> Format accepted by the requests
184  library for uploading files.
185  """
186 
187  return self.parameterName, (self.fileName, self.__getFileData(exitStack), self.mimeType)

The documentation for this class was generated from the following file: