server.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import io
  2. import json
  3. import os.path
  4. import pickle
  5. import arrow
  6. from hashlib import sha1
  7. from uuid import uuid4
  8. from flask import Flask, request, url_for
  9. from lakesuperior.config_parser import config
  10. from lakesuperior.ldp.ldpr import Ldpr, Ldpc, LdpNr, \
  11. InvalidResourceError, ResourceNotExistsError
  12. app = Flask(__name__)
  13. app.config.update(config['flask'])
  14. ## ROUTES ##
  15. @app.route('/', methods=['GET'])
  16. def index():
  17. '''
  18. Homepage.
  19. '''
  20. return 'Hello. This is LAKEsuperior.'
  21. @app.route('/debug', methods=['GET'])
  22. def debug():
  23. '''
  24. Debug page.
  25. '''
  26. raise RuntimeError()
  27. ## REST SERVICES ##
  28. @app.route('/rest/<path:uuid>', methods=['GET'])
  29. @app.route('/rest/', defaults={'uuid': None}, methods=['GET'],
  30. strict_slashes=False)
  31. def get_resource(uuid):
  32. '''
  33. Retrieve RDF or binary content.
  34. '''
  35. # @TODO Add conditions for LDP-NR
  36. rsrc = Ldpc(uuid)
  37. try:
  38. out = rsrc.get()
  39. except ResourceNotExistsError:
  40. return 'Resource #{} not found.'.format(rsrc.uuid), 404
  41. else:
  42. headers = rsrc.head()
  43. return (out.graph.serialize(format='turtle'), headers)
  44. @app.route('/rest/<path:parent>', methods=['POST'])
  45. @app.route('/rest/', defaults={'parent': None}, methods=['POST'],
  46. strict_slashes=False)
  47. def post_resource(parent):
  48. '''
  49. Add a new resource in a new URI.
  50. '''
  51. try:
  52. slug = request.headers['Slug']
  53. except KeyError:
  54. slug = None
  55. try:
  56. rsrc = Ldpc.inst_for_post(parent, slug)
  57. except ResourceNotExistsError as e:
  58. return str(e), 404
  59. except InvalidResourceError as e:
  60. return str(e), 409
  61. rsrc.post(request.get_data().decode('utf-8'))
  62. headers = {
  63. 'Location' : rsrc.uri
  64. }
  65. return rsrc.uri, headers, 201
  66. @app.route('/rest/<path:uuid>', methods=['PUT'])
  67. def put_resource(uuid):
  68. '''
  69. Add a new resource at a specified URI.
  70. '''
  71. rsrc = Ldpc(uuid)
  72. rsrc.put(request.get_data().decode('utf-8'))
  73. return '', 204
  74. @app.route('/rest/<path:uuid>', methods=['PATCH'])
  75. def patch_resource(uuid):
  76. '''
  77. Update an existing resource with a SPARQL-UPDATE payload.
  78. '''
  79. rsrc = Ldpc(uuid)
  80. try:
  81. rsrc.patch(request.get_data().decode('utf-8'))
  82. except ResourceNotExistsError:
  83. return 'Resource #{} not found.'.format(rsrc.uuid), 404
  84. return '', 204
  85. @app.route('/rest/<path:uuid>', methods=['DELETE'])
  86. def delete_resource(uuid):
  87. '''
  88. Delete a resource.
  89. '''
  90. rsrc = Ldpc(uuid)
  91. try:
  92. rsrc.delete()
  93. except ResourceNotExistsError:
  94. return 'Resource #{} not found.'.format(rsrc.uuid), 404
  95. return '', 204