hosted-files.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ************
  2. Hosted files
  3. ************
  4. The Launchpad web service sets restrictions on what kinds of documents
  5. can be written to a particular file. This test shows what happens when
  6. you try to upload a non-image for a field that expects an image.
  7. >>> from launchpadlib.testing.helpers import salgado_with_full_permissions
  8. >>> launchpad = salgado_with_full_permissions.login()
  9. >>> from launchpadlib.errors import HTTPError
  10. >>> mugshot = launchpad.me.mugshot
  11. >>> file_handle = mugshot.open("w", "image/png", "nonimage.txt")
  12. >>> file_handle.content_type
  13. 'image/png'
  14. >>> file_handle.filename
  15. 'nonimage.txt'
  16. >>> file_handle.write(b"Not an image.")
  17. >>> try:
  18. ... file_handle.close()
  19. ... except HTTPError as e:
  20. ... print(e.content.decode())
  21. <BLANKLINE>
  22. The file uploaded was not recognized as an image; please
  23. check it and retry.
  24. Of course, uploading an image works fine.
  25. >>> import os
  26. >>> def load_image(filename):
  27. ... image_file = os.path.join(
  28. ... os.path.dirname(__file__), 'files', filename)
  29. ... with open(image_file, "rb") as f:
  30. ... return f.read()
  31. >>> image = load_image("mugshot.png")
  32. >>> len(image)
  33. 2260
  34. >>> file_handle = mugshot.open("w", "image/png", "a-mugshot.png")
  35. >>> file_handle.write(image)
  36. >>> file_handle.close()
  37. == Error handling ==
  38. The server may set restrictions on what kinds of documents can be
  39. written to a particular file.
  40. >>> file_handle = mugshot.open("w", "image/png", "nonimage.txt")
  41. >>> file_handle.content_type
  42. 'image/png'
  43. >>> file_handle.filename
  44. 'nonimage.txt'
  45. >>> file_handle.write(b"Not an image.")
  46. >>> file_handle.close()
  47. Traceback (most recent call last):
  48. ...
  49. lazr.restfulclient.errors.BadRequest: HTTP Error 400: Bad Request
  50. ...
  51. == Caching ==
  52. Hosted file resources implement the normal server-side caching
  53. mechanism.
  54. >>> file_handle = mugshot.open("w", "image/png", "image.png")
  55. >>> file_handle.write(image)
  56. >>> file_handle.close()
  57. >>> import httplib2
  58. >>> httplib2.debuglevel = 1
  59. >>> launchpad = salgado_with_full_permissions.login()
  60. send: ...
  61. >>> mugshot = launchpad.me.mugshot
  62. send: ...
  63. The first request for a file retrieves the file from the server.
  64. >>> len(mugshot.open().read())
  65. send: ...
  66. reply: 'HTTP/1.1 303 See Other...
  67. reply: 'HTTP/1.1 200 OK...
  68. 2260
  69. The second request retrieves the file from the cache. After receiving
  70. the 303 request with its Location header, no further HTTP requests are
  71. issued because the Librarian's Cache-Control: headers tell us we
  72. already have a fresh copy.
  73. >>> len(mugshot.open().read())
  74. send: ...
  75. reply: 'HTTP/1.1 303 See Other...
  76. header: Location...
  77. 2260
  78. Finally, some cleanup code that deletes the mugshot.
  79. >>> mugshot.delete()
  80. send: b'DELETE...
  81. reply: 'HTTP/1.1 200...
  82. >>> httplib2.debuglevel = 0