usage.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 importing the API modules or other Lakesuperior modules, the environment
  79. must be set up. This can be done in several ways. The simplest one is to rely
  80. on a default configuration directory set up by the ``FCREPO_CONFIG_DIR``
  81. environment variable::
  82. >>> from lakesuperior import env
  83. >>> env.setup()
  84. Reading configuration at /my/default/config_dir
  85. If ``FCREPO_CONFIG_DIR`` is not set up, the ``etc.defaults`` location under
  86. the library root is used.
  87. Alternatively, a custom configuration directory can be specified::
  88. >>> from lakesuperior import env
  89. >>> env.setup('/my/config/dir')
  90. Reading configuration at /my/custom/config_dir
  91. A configuration can also be loaded and modified before setting up the
  92. environment::
  93. >>> from lakesuperior import env
  94. >>> from lakesuperior.config_parser import parse_config
  95. >>> config = parse_config('/my/config/dir')
  96. Reading configuration at /my/custom/config_dir
  97. >>> config['application']['data_dir'] = '/data/ext/mystore'
  98. >>> env.setup(config=config)
  99. Create and replace resources
  100. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101. Create an LDP-RS (RDF reseouce) providng a Graph object::
  102. >>> from rdflib import Graph, URIRef
  103. >>> uid = '/rsrc_from_graph'
  104. >>> gr = Graph().parse(data='<> a <http://ex.org/type#A> .',
  105. ... format='text/turtle', publicID=nsc['fcres'][uid])
  106. >>> rsrc_api.create_or_replace(uid, init_gr=gr)
  107. Issuing a ``create_or_replace()`` on an existing UID will replace the existing
  108. property set with the provided one (PUT style).
  109. Create an LDP-NR (non-RDF source)::
  110. >>> uid = '/test_ldpnr01'
  111. >>> data = b'Hello. This is some dummy content.'
  112. >>> rsrc_api.create_or_replace(
  113. ... uid, stream=BytesIO(data), mimetype='text/plain')
  114. '_create_'
  115. Create or replace providing a serialized RDF byte stream::
  116. >>> uid = '/rsrc_from_rdf'
  117. >>> rdf = b'<#a1> a <http://ex.org/type#B> .'
  118. >>> rsrc_api.create_or_replace(uid, rdf_data=rdf, rdf_fmt='turtle')
  119. Relative URIs such as ``<#a1>`` will be resolved relative to the resource URI.
  120. Create under a known parent, providing a slug (POST style)::
  121. >>> rsrc_api.create('/rsrc_from_stream', 'res1')
  122. This will create ``/rsrc_from_stream/res1`` if not existing; otherwise the
  123. resource URI will have a random UUID4 instead of ``res1``.
  124. To use a random UUID by default, use ``None`` for the second argument.
  125. Retrieve Resources
  126. ~~~~~~~~~~~~~~~~~~
  127. Retrieve a resource::
  128. >>> rsrc = rsrc_api.get('/rsrc_from_stream')
  129. >>> rsrc.uid
  130. '/rsrc_from_stream'
  131. >>> rsrc.uri
  132. rdflib.term.URIRef('info:fcres/rsrc_from_stream')
  133. >>> set(rsrc.metadata)
  134. {(rdflib.term.URIRef('info:fcres/rsrc_from_stream'),
  135. rdflib.term.URIRef('http://fedora.info/definitions/v4/repository#created'),
  136. rdflib.term.Literal('2018-04-06T03:30:49.460274+00:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))),
  137. [...]
  138. Retrieve non-RDF content::
  139. >>> ldpnr = rsrc_api.get('/test_ldpnr01')
  140. >>> ldpnr.content.read()
  141. b'Hello. This is some dummy content.'
  142. See the :doc:`API docs <api>` for more details on resource methods.
  143. Update Resources
  144. ~~~~~~~~~~~~~~~~
  145. Using a SPARQL update string::
  146. >>> uid = '/test_delta_patch_wc'
  147. >>> uri = nsc['fcres'][uid]
  148. >>> init_trp = {
  149. ... (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  150. ... (URIRef(uri), nsc['foaf'].name, Literal('Joe Bob')),
  151. ... (URIRef(uri), nsc['foaf'].name, Literal('Joe Average Bob')),
  152. ... }
  153. >>> update_str = '''
  154. ... DELETE {}
  155. ... INSERT { <> foaf:name "Joe Average 12oz Bob" . }
  156. ... WHERE {}
  157. ... '''
  158. Using add/remove triple sets::
  159. >>> remove_trp = {
  160. ... (URIRef(uri), nsc['foaf'].name, None),
  161. ... }
  162. >>> add_trp = {
  163. ... (URIRef(uri), nsc['foaf'].name, Literal('Joan Knob')),
  164. ... }
  165. >>> gr = Graph()
  166. >>> gr += init_trp
  167. >>> rsrc_api.create_or_replace(uid, graph=gr)
  168. >>> rsrc_api.update_delta(uid, remove_trp, add_trp)
  169. Note above that wildcards can be used, only in the remove triple set. Wherever
  170. ``None`` is used, all matches will be removed (in this example, all values of
  171. ``foaf:name``.
  172. Generally speaking, the delta approach providing a set of remove triples and/or
  173. a set of add triples is more convenient than SPARQL, which is a better fit for
  174. complex query/update scenarios.