usage.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. Sample Usage
  2. ============
  3. LDP (REST) API
  4. --------------
  5. The following are very basic examples of LDP interaction. For a more complete
  6. reference, please consult the `Fedora API guide
  7. <https://wiki.duraspace.org/display/FEDORA4x/RESTful+HTTP+API+-+Containers>`__.
  8. **Note**: At the moment the LDP API only support the Turtle format for
  9. serializing and deserializing RDF.
  10. Create an empty LDP container (LDPC)
  11. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. ::
  13. curl -X POST http://localhost:8000/ldp
  14. Create a resource with RDF payload
  15. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  16. ::
  17. curl -X POST -H'Content-Type:text/turtle' --data-binary '<> <urn:ns:p1> <urn:ns:o1> .' http://localhost:8000/ldp
  18. Create a resource at a specific location
  19. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. ::
  21. curl -X PUT http://localhost:8000/ldp/res1
  22. Create a binary resource
  23. ~~~~~~~~~~~~~~~~~~~~~~~~
  24. ::
  25. curl -X PUT -H'Content-Type:image/png' --data-binary '@/home/me/image.png' http://localhost:8000/ldp/bin1
  26. Retrieve an RDF resource (LDP-RS)
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. ::
  29. curl http://localhost:8000/ldp/res1
  30. Retrieve a non-RDF source (LDP-NR)
  31. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. ::
  33. curl http://localhost:8000/ldp/bin1
  34. Or::
  35. curl http://localhost:8000/ldp/bin1/fcr:content
  36. Or::
  37. curl -H'Accept:image/png' http://localhost:8000/ldp/bin1
  38. Retrieve RDF metadata of a LDP-NR
  39. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. ::
  41. curl http://localhost:8000/ldp/bin1/fcr:metadata
  42. Or::
  43. curl -H'Accept:text/turtle' http://localhost:8000/ldp/bin1
  44. Soft-delete a resource
  45. ~~~~~~~~~~~~~~~~~~~~~~~
  46. ::
  47. curl -X DELETE http://localhost:8000/ldp/bin1
  48. Restore ("resurrect") a resource
  49. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50. ::
  51. curl -X POST http://localhost:8000/ldp/bin1/fcr:tombstone
  52. Permanently delete ("forget") a soft-deleted resource
  53. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  54. **Note**: the following command cannot be issued after the previous one. It has
  55. to be issued on a soft-deleted, non-resurrected resource.
  56. ::
  57. curl -X DELETE http://localhost:8000/ldp/bin1/fcr:tombstone
  58. Immediately forget a resource
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. ::
  61. curl -X DELETE -H'Prefer:no-tombstone' http://localhost:8000/ldp/res1
  62. Admin REST API
  63. --------------
  64. Fixity check
  65. ~~~~~~~~~~~~
  66. Check the fixity of a resource, i.e. if the checksum stored in the metadata
  67. corresponds to the current checksum of the stored file. This requires a
  68. checksum calculation and may take a long time depending on the file size and
  69. the hashing algorithm chosen::
  70. curl http://localhost:8000/admin/<resource UID>/fixity
  71. The response is a JSON document with two keys: ``uid`` indicating the UID of
  72. the resource checked; and ``pass`` that can be True or False depending on the
  73. outcome of the check.
  74. Python API
  75. ----------
  76. Set up the environment
  77. ~~~~~~~~~~~~~~~~~~~~~~
  78. Before using the API, either do::
  79. >>> import lakesuperior.env_setup
  80. Or, to specify an alternative configuration::
  81. >>> from lakesuperior import env
  82. >>> from lakesuperior.config_parser import parse_config
  83. >>> from lakesuperior.globals import AppGlobals
  84. >>> config = parse_config('/my/custom/config_dir')
  85. Reading configuration at /my/custom/config_dir
  86. >>> env.app_globals = AppGlobals(config)
  87. Create and replace resources
  88. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. Create an LDP-RS (RDF reseouce) providng a Graph object::
  90. >>> from rdflib import Graph, URIRef
  91. >>> uid = '/rsrc_from_graph'
  92. >>> gr = Graph().parse(data='<> a <http://ex.org/type#A> .',
  93. ... format='text/turtle', publicID=nsc['fcres'][uid])
  94. >>> rsrc_api.create_or_replace(uid, init_gr=gr)
  95. Issuing a ``create_or_replace()`` on an existing UID will replace the existing
  96. property set with the provided one (PUT style).
  97. Create an LDP-NR (non-RDF source)::
  98. >>> uid = '/test_ldpnr01'
  99. >>> data = b'Hello. This is some dummy content.'
  100. >>> rsrc_api.create_or_replace(
  101. ... uid, stream=BytesIO(data), mimetype='text/plain')
  102. '_create_'
  103. Create or replace providing a serialized RDF byte stream::
  104. >>> uid = '/rsrc_from_rdf'
  105. >>> rdf = b'<#a1> a <http://ex.org/type#B> .'
  106. >>> rsrc_api.create_or_replace(uid, rdf_data=rdf, rdf_fmt='turtle')
  107. Relative URIs such as ``<#a1>`` will be resolved relative to the resource URI.
  108. Create under a known parent, providing a slug (POST style)::
  109. >>> rsrc_api.create('/rsrc_from_stream', 'res1')
  110. This will create ``/rsrc_from_stream/res1`` if not existing; otherwise the
  111. resource URI will have a random UUID4 instead of ``res1``.
  112. To use a random UUID by default, use ``None`` for the second argument.
  113. Retrieve Resources
  114. ~~~~~~~~~~~~~~~~~~
  115. Retrieve a resource::
  116. >>> rsrc = rsrc_api.get('/rsrc_from_stream')
  117. >>> rsrc.uid
  118. '/rsrc_from_stream'
  119. >>> rsrc.uri
  120. rdflib.term.URIRef('info:fcres/rsrc_from_stream')
  121. >>> set(rsrc.metadata)
  122. {(rdflib.term.URIRef('info:fcres/rsrc_from_stream'),
  123. rdflib.term.URIRef('http://fedora.info/definitions/v4/repository#created'),
  124. rdflib.term.Literal('2018-04-06T03:30:49.460274+00:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))),
  125. [...]
  126. Retrieve non-RDF content::
  127. >>> ldpnr = rsrc_api.get('/test_ldpnr01')
  128. >>> ldpnr.content.read()
  129. b'Hello. This is some dummy content.'
  130. See the :doc:`API docs <api>` for more details on resource methods.
  131. Update Resources
  132. ~~~~~~~~~~~~~~~~
  133. Using a SPARQL update string::
  134. >>> uid = '/test_delta_patch_wc'
  135. >>> uri = nsc['fcres'][uid]
  136. >>> init_trp = {
  137. ... (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  138. ... (URIRef(uri), nsc['foaf'].name, Literal('Joe Bob')),
  139. ... (URIRef(uri), nsc['foaf'].name, Literal('Joe Average Bob')),
  140. ... }
  141. >>> update_str = '''
  142. ... DELETE {}
  143. ... INSERT { <> foaf:name "Joe Average 12oz Bob" . }
  144. ... WHERE {}
  145. ... '''
  146. Using add/remove triple sets::
  147. >>> remove_trp = {
  148. ... (URIRef(uri), nsc['foaf'].name, None),
  149. ... }
  150. >>> add_trp = {
  151. ... (URIRef(uri), nsc['foaf'].name, Literal('Joan Knob')),
  152. ... }
  153. >>> gr = Graph()
  154. >>> gr += init_trp
  155. >>> rsrc_api.create_or_replace(uid, graph=gr)
  156. >>> rsrc_api.update_delta(uid, remove_trp, add_trp)
  157. Note above that wildcards can be used, only in the remove triple set. Wherever
  158. ``None`` is used, all matches will be removed (in this example, all values of
  159. ``foaf:name``.
  160. Generally speaking, the delta approach providing a set of remove triples and/or
  161. a set of add triples is more convenient than SPARQL, which is a better fit for
  162. complex query/update scenarios.