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