Deliverable for D3.2

data.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package api
  2. import (
  3. "net/http"
  4. validator "gopkg.in/validator.v2"
  5. restful "github.com/emicklei/go-restful"
  6. restfulspec "github.com/emicklei/go-restful-openapi"
  7. storageclient "gogs.dyne.org/DECODE/decode-prototype-da/client/storage"
  8. "gogs.dyne.org/DECODE/decode-prototype-da/utils"
  9. )
  10. type dataResource struct {
  11. store *EntitlementStore
  12. metaStore *MetadataStore
  13. sClient *storageclient.DataApi
  14. }
  15. type DataRequest struct {
  16. Key string `json:"key" description:"unique name for the data"`
  17. }
  18. type DataResponse struct {
  19. Data interface{} `json:"data" description:"data returned type="object"`
  20. Metadata Metadata `json:"metadata" description:"metadata for the data e.g. description"`
  21. }
  22. type MetadataRequest struct {
  23. Description string `json:"description" description:"human readable description of the data"`
  24. }
  25. func NewDataService(store *EntitlementStore, sClient *storageclient.DataApi, metaStore *MetadataStore) dataResource {
  26. return dataResource{
  27. store: store,
  28. metaStore: metaStore,
  29. sClient: sClient,
  30. }
  31. }
  32. func (e dataResource) WebService() *restful.WebService {
  33. ws := new(restful.WebService)
  34. ws.
  35. Path("/data").
  36. Consumes(restful.MIME_JSON).
  37. Produces(restful.MIME_JSON)
  38. tags := []string{"data"}
  39. ws.Route(ws.POST("/").To(e.getData).
  40. Doc("retrieve some data").
  41. Metadata(restfulspec.KeyOpenAPITags, tags).
  42. Reads(DataRequest{}).
  43. Writes(DataResponse{}).
  44. Returns(http.StatusOK, "OK", DataResponse{}).
  45. Returns(http.StatusInternalServerError, "Something bad happened", ErrorResponse{}).
  46. Returns(http.StatusForbidden, "Forbidden", nil).
  47. Returns(http.StatusNotFound, "Not Found", nil))
  48. ws.Route(ws.GET("/meta").To(e.getMetaData).
  49. Doc("retrieve some data").
  50. Metadata(restfulspec.KeyOpenAPITags, tags).
  51. Writes([]Metadata{}).
  52. Returns(http.StatusOK, "OK", []Metadata{}).
  53. Returns(http.StatusInternalServerError, "Something bad happened", ErrorResponse{}))
  54. return ws
  55. }
  56. func (e dataResource) getMetaData(request *restful.Request, response *restful.Response) {
  57. response.WriteEntity(e.metaStore.All())
  58. }
  59. func (e dataResource) getData(request *restful.Request, response *restful.Response) {
  60. req := DataRequest{}
  61. if err := request.ReadEntity(&req); err != nil {
  62. response.WriteHeaderAndEntity(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
  63. return
  64. }
  65. if errs := validator.Validate(req); errs != nil {
  66. response.WriteHeaderAndEntity(http.StatusBadRequest, ErrorResponse{Error: errs.Error()})
  67. return
  68. }
  69. // find entitlement for key
  70. subject, err := utils.ParseSubject(req.Key)
  71. if err != nil {
  72. response.WriteHeaderAndEntity(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
  73. return
  74. }
  75. ent, found := e.store.Accepted.FindForSubject(subject)
  76. if !found {
  77. response.WriteHeader(http.StatusForbidden)
  78. return
  79. }
  80. if !ent.IsAccessible() {
  81. response.WriteHeader(http.StatusForbidden)
  82. return
  83. }
  84. data, _, err := e.sClient.GetAll(req.Key)
  85. if err != nil {
  86. response.WriteHeaderAndEntity(http.StatusInternalServerError, ErrorResponse{Error: err.Error()})
  87. return
  88. }
  89. meta, found := e.metaStore.FindBySubject(subject)
  90. if !found {
  91. response.WriteHeaderAndEntity(http.StatusInternalServerError, ErrorResponse{Error: "metadata not found"})
  92. return
  93. }
  94. resp := DataResponse{
  95. Data: data,
  96. Metadata: meta,
  97. }
  98. response.WriteEntity(resp)
  99. }