Mercurial > p > roundup > code
comparison doc/rest.txt @ 8180:d02ce1d14acd
feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
Use Allow header to change format of /binary_content endpoint. If
Allow header for endpoint is not application/json, it will be matched
against the mime type for the file. */*, text/* are supported and will
return the native mime type if present.
Changes:
move */* mime type from static dict of supported types. It was
hardcoded to return json only. Now it can return a matching
non-json mime type for the /binary_content endpoint.
Edited some errors to explicitly add */* mime type.
Cleanups to use ', ' separation in lists of valid mime types rather
than just space separated.
Remove ETag header when sending raw content. See issue 2551375 for
background.
Doc added to rest.txt.
Small format fix up (add dash) in CHANGES.txt.
Make passing an unset/None/False accept_mime_type to
format_dispatch_output a 500 error. This used to be the fallback
to produce a 406 error after all processing had happened. It
should no longer be possible to take that code path as all 406
errors (with valid accept_mime_types) are generated before
processing takes place.
Make format_dispatch_output handle output other than json/xml so it
can send back binary_content data.
Removed a spurious client.response_code = 400 that seems to not be
used.
Tests added for all code paths.
Database setup for tests msg and file entry. This required a file
upload test to change so it doesn't look for file1 as the link
returned by the upload. Download the link and verify the data
rather than verifying the link.
Multiple formatting changes to error messages to make all lists of
valid mime types ', ' an not just space separated.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 08 Dec 2024 17:22:33 -0500 |
| parents | bd628e64725f |
| children | 79b9343794f5 |
comparison
equal
deleted
inserted
replaced
| 8179:53afc3ffd4a2 | 8180:d02ce1d14acd |
|---|---|
| 366 To make testing from the browser easier, you can also append the | 366 To make testing from the browser easier, you can also append the |
| 367 extension ``.json`` or ``.xml`` to the path component of the url. This | 367 extension ``.json`` or ``.xml`` to the path component of the url. This |
| 368 will force json or xml (if supported) output. If you use an extension | 368 will force json or xml (if supported) output. If you use an extension |
| 369 it takes priority over any accept headers. Note the extension does not | 369 it takes priority over any accept headers. Note the extension does not |
| 370 work for the ``/rest`` or ``/rest/data`` paths. In these cases it | 370 work for the ``/rest`` or ``/rest/data`` paths. In these cases it |
| 371 returs a 404 error. Adding the header ``Accept: application/xml`` | 371 returns a 404 error. Adding the header ``Accept: application/xml`` |
| 372 allows these paths to return xml data. | 372 allows these paths to return xml data. |
| 373 | 373 |
| 374 The rest interface returns status 406 if you use an unrecognized | 374 The rest interface returns status 406 if you use an unrecognized |
| 375 extension. You will also get a 406 status if none of the entries in | 375 extension. You will also get a 406 status if none of the entries in |
| 376 the accept header are available or if the accept header is invalid. | 376 the accept header are available or if the accept header is invalid. |
| 377 | 377 |
| 378 Note: ``dicttoxml2.py`` is an updated version of ``dicttoxml.py``. If | 378 Note: ``dicttoxml2.py`` is an updated version of ``dicttoxml.py``. If |
| 379 you are still using Python 2.7 or 3.6, you can use ``dicttoxml.py``. | 379 you are still using Python 2.7 or 3.6, you can use ``dicttoxml.py``. |
| 380 | 380 |
| 381 Also the ``/binary_content`` attribute endpoint can be used to | |
| 382 retrieve raw file data in many formats. | |
| 381 | 383 |
| 382 General Guidelines | 384 General Guidelines |
| 383 ------------------ | 385 ------------------ |
| 384 | 386 |
| 385 Performing a ``GET`` on an item or property of an item will return an | 387 Performing a ``GET`` on an item or property of an item will return an |
| 904 }, | 906 }, |
| 905 "@etag": "\"584f82231079e349031bbb853747df1c\"" | 907 "@etag": "\"584f82231079e349031bbb853747df1c\"" |
| 906 } | 908 } |
| 907 } | 909 } |
| 908 | 910 |
| 911 With Roundup 2.5 you can retrieve the data directly from the rest | |
| 912 interface using the ``Accept`` header value to select a structured (json | |
| 913 or optional xml) representation (as above) or a stream with just the | |
| 914 content data. | |
| 915 | |
| 916 Using the wildcard type ``*/*`` in the ``Accept`` header with the url | |
| 917 ``.../binary_content`` will return the raw data and the recorded mime | |
| 918 type of the the data as the ``Content-Type``. Using ``*/*`` with | |
| 919 another end point will return ``json`` data. An ``Accept`` value of | |
| 920 ``application/octet-stream`` matches any mime type and retrieves the | |
| 921 raw data as ``Content-Type: application/octet-stream``. | |
| 922 | |
| 923 To access the contents of a PNG image file (in file23), you use the | |
| 924 following link: | |
| 925 ``https://.../demo/rest/data/file/23/binary_content``. To find out the | |
| 926 mime type, you can check this URL: | |
| 927 ``https://.../demo/rest/data/file/23/type``. | |
| 928 | |
| 929 By setting the header to ``Accept: application/octet-stream; q=1.0, | |
| 930 application/json; q=0.5``, you will receive the binary PNG file with | |
| 931 the header ``Content-Type: application/octet-stream``. If you switch | |
| 932 the ``q`` values, you will receive the encoded JSON version:: | |
| 933 | |
| 934 { | |
| 935 "data": { | |
| 936 "id": "23", | |
| 937 "type": "<class 'bytes'>", | |
| 938 "link": "https://.../demo/rest/data/file/23/binary_content", | |
| 939 "data": "b'\\x89PNG\\r\\n\\x1a\\n\\x00[...]0\\x00\\x00\\x00IEND\\xaeB`\\x82'", | |
| 940 "@etag": "\"db6adc1b09d95b0388d79c7905bc7982\"" | |
| 941 } | |
| 942 } | |
| 943 | |
| 944 with ``Content-Type: application/json`` and a (4x larger) json encoded | |
| 945 representation of the binary data. | |
| 946 | |
| 947 If you want it returned with a ``Content-Type: image/png`` header, | |
| 948 you can use ``image/png`` or ``*/*`` in the Accept header. | |
| 949 | |
| 950 For message files, you can use | |
| 951 ``https://.../demo/rest/data/msg/23/binary_content`` with ``Accept: | |
| 952 application/octet-stream; q=0.5, application/json; q=0.4, image/png; | |
| 953 q=0.495, text/*``. It will return the plain text of the message. | |
| 954 | |
| 955 Most message files are not stored with a mime type. Getting | |
| 956 ``https://.../demo/rest/data/msg/23/type`` returns:: | |
| 957 | |
| 958 { | |
| 959 "data": { | |
| 960 "id": "23", | |
| 961 "type": "<class 'NoneType'>", | |
| 962 "link": "https://.../demo/rest/data/msg/23/type", | |
| 963 "data": null, | |
| 964 "@etag": "\"ba98927a8bb4c56f6cfc31a36f94ad16\"" | |
| 965 } | |
| 966 } | |
| 967 | |
| 968 The data attribute will usually be null/empty. As a result, mime type | |
| 969 matching for an item without a mime type is forgiving. | |
| 970 | |
| 971 Messages are meant to be human readable, so the mime type ``text/*`` | |
| 972 can be used to access any text style mime type (``text/plain``, | |
| 973 ``text/x-rst``, ``text/markdown``, ``text/html``, ...) or an empty | |
| 974 mime type. If the item's type is not empty, it will be used as the | |
| 975 Content-Type (similar to ``*/*``). Otherwise ``text/*`` will be the | |
| 976 Content-Type. If your tracker supports markup languages | |
| 977 (e.g. markdown), you should set the mime type (e.g. ``text/markdown``) | |
| 978 when storing your message. | |
| 979 | |
| 980 Note that the header ``X-Content-Type-Options: nosniff`` is returned | |
| 981 with a non javascript or xml binary_content response to prevent the | |
| 982 browser from trying to interpret the returned data. | |
| 983 | |
| 984 Legacy Method (HTML interface) | |
| 985 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
| 986 | |
| 987 With the addition of file binary content streaming in the rest | |
| 988 interface to Roundup 2.5.0, this method (using the html interface) is | |
| 989 considered legacy but still works. | |
| 990 | |
| 909 To retreive the content, you can use the content link property: | 991 To retreive the content, you can use the content link property: |
| 910 ``https://.../demo/msg11/``. The trailing / is required. Without the | 992 ``https://.../demo/msg11/``. The trailing / is required. Without the |
| 911 /, you get a web page that includes metadata about the message. With | 993 /, you get a web page that includes metadata about the message. With |
| 912 the slash you get a text/plain (in most cases) data stream. | 994 the slash you get a text/plain (in most cases) data stream. |
| 913 | 995 |
