usage.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. Sample Usage
  2. ============
  3. LDP 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. Python API
  63. ----------
  64. Set up the environment
  65. ~~~~~~~~~~~~~~~~~~~~~~
  66. Before using the API, either do::
  67. >>> import lakesuperior.env_setup
  68. Or, to specify an alternative configuration::
  69. >>> from lakesuperior import env
  70. >>> from lakesuperior.config_parser import parse_config
  71. >>> from lakesuperior.globals import AppGlobals
  72. >>> config = parse_config('/my/custom/config_dir')
  73. Reading configuration at /my/custom/config_dir
  74. >>> env.app_globals = AppGlobals(config)
  75. Create and replace resources
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. Create an LDP-RS (RDF reseouce) providng a Graph object::
  78. >>> from rdflib import Graph, URIRef
  79. >>> uid = '/rsrc_from_graph'
  80. >>> gr = Graph().parse(data='<> a <http://ex.org/type#A> .',
  81. ... format='text/turtle', publicID=nsc['fcres'][uid])
  82. >>> rsrc_api.create_or_replace(uid, init_gr=gr)
  83. Issuing a ``create_or_replace()`` on an existing UID will replace the existing
  84. property set with the provided one (PUT style).
  85. Create an LDP-NR (non-RDF source)::
  86. >>> uid = '/test_ldpnr01'
  87. >>> data = b'Hello. This is some dummy content.'
  88. >>> rsrc_api.create_or_replace(
  89. ... uid, stream=BytesIO(data), mimetype='text/plain')
  90. '_create_'
  91. Create or replace providing a serialized RDF byte stream::
  92. >>> uid = '/rsrc_from_rdf'
  93. >>> rdf = b'<#a1> a <http://ex.org/type#B> .'
  94. >>> rsrc_api.create_or_replace(uid, rdf_data=rdf, rdf_fmt='turtle')
  95. Relative URIs such as ``<#a1>`` will be resolved relative to the resource URI.
  96. Create under a known parent, providing a slug (POST style)::
  97. >>> rsrc_api.create('/rsrc_from_stream', 'res1')
  98. This will create ``/rsrc_from_stream/res1`` if not existing; otherwise the
  99. resource URI will have a random UUID4 instead of ``res1``.
  100. To use a random UUID by default, use ``None`` for the second argument.
  101. Retrieve Resources
  102. ~~~~~~~~~~~~~~~~~~
  103. Retrieve a resource::
  104. >>> rsrc = rsrc_api.get('/rsrc_from_stream')
  105. >>> rsrc.uid
  106. '/rsrc_from_stream'
  107. >>> rsrc.uri
  108. rdflib.term.URIRef('info:fcres/rsrc_from_stream')
  109. >>> set(rsrc.metadata)
  110. {(rdflib.term.URIRef('info:fcres/rsrc_from_stream'),
  111. rdflib.term.URIRef('http://fedora.info/definitions/v4/repository#created'),
  112. rdflib.term.Literal('2018-04-06T03:30:49.460274+00:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))),
  113. [...]
  114. Retrieve non-RDF content::
  115. >>> ldpnr = rsrc_api.get('/test_ldpnr01')
  116. >>> ldpnr.content.read()
  117. b'Hello. This is some dummy content.'
  118. See the :doc:`API docs <api>` for more details on resource methods.
  119. Update Resources
  120. ~~~~~~~~~~~~~~~~
  121. Using a SPARQL update string::
  122. >>> uid = '/test_delta_patch_wc'
  123. >>> uri = nsc['fcres'][uid]
  124. >>> init_trp = {
  125. ... (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Person),
  126. ... (URIRef(uri), nsc['foaf'].name, Literal('Joe Bob')),
  127. ... (URIRef(uri), nsc['foaf'].name, Literal('Joe Average Bob')),
  128. ... }
  129. >>> update_str = '''
  130. ... DELETE {}
  131. ... INSERT { <> foaf:name "Joe Average 12oz Bob" . }
  132. ... WHERE {}
  133. ... '''
  134. Using add/remove triple sets::
  135. >>> remove_trp = {
  136. ... (URIRef(uri), nsc['foaf'].name, None),
  137. ... }
  138. >>> add_trp = {
  139. ... (URIRef(uri), nsc['foaf'].name, Literal('Joan Knob')),
  140. ... }
  141. >>> gr = Graph()
  142. >>> gr += init_trp
  143. >>> rsrc_api.create_or_replace(uid, graph=gr)
  144. >>> rsrc_api.update_delta(uid, remove_trp, add_trp)
  145. Note above that wildcards can be used, only in the remove triple set. Wherever
  146. ``None`` is used, all matches will be removed (in this example, all values of
  147. ``foaf:name``.
  148. Generally speaking, the delta approach providing a set of remove triples and/or
  149. a set of add triples is more convenient than SPARQL, which is a better fit for
  150. complex query/update scenarios.