usage.rst 5.4 KB

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