Merge pull request #2869 from owncloud/graph-api-rest

Graph api rest
This commit is contained in:
Michael Barz
2021-12-13 19:59:01 +01:00
committed by GitHub
9 changed files with 58 additions and 43 deletions
+32 -19
View File
@@ -12,7 +12,6 @@ import (
"strings"
"time"
"github.com/CiscoM31/godata"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
@@ -21,6 +20,7 @@ import (
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/graph/pkg/service/v0/errorcode"
@@ -240,7 +240,7 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
newDrive, err := cs3StorageSpaceToDrive(wdu, resp.StorageSpace)
if err != nil {
g.logger.Error().Err(err).Msg("error parsing url")
g.logger.Error().Err(err).Msg("error parsing space")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
@@ -249,22 +249,14 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
}
func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
// wildcards however addressed here is not yet supported. We want to address drives by their unique
// identifiers. Any open queries need to be implemented. Same applies for sub-entities.
// For further reading: http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_AddressingaSubsetofaCollection
// strip "/graph/v1.0/" out and parse the rest. This is how godata input is expected.
//https://github.com/CiscoM31/godata/blob/d70e191d2908191623be84401fecc40d6af4afde/url_parser_test.go#L10
sanitized := strings.TrimPrefix(r.URL.Path, "/graph/v1.0/")
req, err := godata.ParseRequest(r.Context(), sanitized, r.URL.Query())
driveID, err := url.PathUnescape(chi.URLParam(r, "driveID"))
if err != nil {
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, err.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping drive id failed")
return
}
if req.FirstSegment.Identifier.Get() == "" {
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "identifier cannot be empty")
if driveID == "" {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "missing drive id")
return
}
@@ -274,9 +266,9 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
return
}
identifierParts := strings.Split(req.FirstSegment.Identifier.Get(), "!")
identifierParts := strings.Split(driveID, "!")
if len(identifierParts) != 2 {
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("invalid resource id: %v", req.FirstSegment.Identifier.Get()))
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("invalid resource id: %v", driveID))
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -294,7 +286,7 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
// the original storage space.
StorageSpace: &provider.StorageSpace{
Id: &storageprovider.StorageSpaceId{
OpaqueId: req.FirstSegment.Identifier.Get(),
OpaqueId: driveID,
},
Root: &provider.ResourceId{
StorageId: storageID,
@@ -330,11 +322,32 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
}
if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "")
switch resp.Status.GetCode() {
case v1beta11.Code_CODE_NOT_FOUND:
errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, resp.GetStatus().GetMessage())
return
default:
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, resp.GetStatus().GetMessage())
return
}
}
wdu, err := url.Parse(g.config.Spaces.WebDavBase + g.config.Spaces.WebDavPath)
if err != nil {
g.logger.Error().Err(err).Msg("error parsing url")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
w.WriteHeader(http.StatusNoContent)
updatedDrive, err := cs3StorageSpaceToDrive(wdu, resp.StorageSpace)
if err != nil {
g.logger.Error().Err(err).Msg("error parsing space")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, updatedDrive)
}
func cs3TimestampToTime(t *types.Timestamp) time.Time {
+3 -3
View File
@@ -85,9 +85,9 @@ func NewService(opts ...Option) Service {
r.Route("/drives", func(r chi.Router) {
r.Get("/", svc.GetDrives)
r.Post("/", svc.CreateDrive)
})
r.Route("/Drive({firstSegmentIdentifier})", func(r chi.Router) {
r.Patch("/*", svc.UpdateDrive)
r.Route("/{driveID}", func(r chi.Router) {
r.Patch("/", svc.UpdateDrive)
})
})
})
})