http.py 958 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import io
  2. import requests
  3. from magic_pdf.data.io.base import IOReader, IOWriter
  4. class HttpReader(IOReader):
  5. def read(self, url: str) -> bytes:
  6. """Read the file.
  7. Args:
  8. path (str): file path to read
  9. Returns:
  10. bytes: the content of the file
  11. """
  12. return requests.get(url).content
  13. def read_at(self, path: str, offset: int = 0, limit: int = -1) -> bytes:
  14. """Not Implemented."""
  15. raise NotImplementedError
  16. class HttpWriter(IOWriter):
  17. def write(self, url: str, data: bytes) -> None:
  18. """Write file with data.
  19. Args:
  20. path (str): the path of file, if the path is relative path, it will be joined with parent_dir.
  21. data (bytes): the data want to write
  22. """
  23. files = {'file': io.BytesIO(data)}
  24. response = requests.post(url, files=files)
  25. assert 300 > response.status_code and response.status_code > 199