fix ocs resonse format
Signed-off-by: David Christofas <dchristofas@owncloud.com>
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
Bugfix: Add the top level response structure to json responses
|
||||||
|
|
||||||
|
Probably during moving the ocs code into the ocis-ocs repo the response format was changed.
|
||||||
|
This change adds the top level response to json responses. Doing that the reponse should be compatible to the responses from OC10.
|
||||||
|
|
||||||
|
https://github.com/owncloud/product/issues/181
|
||||||
|
https://github.com/owncloud/product/issues/181#issuecomment-683604168
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
// Response is the top level response structure
|
// Response is the top level response structure
|
||||||
type Response struct {
|
type Response struct {
|
||||||
OCS *Payload `json:"ocs"`
|
OCS *Payload `json:"ocs" xml:"ocs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -23,13 +23,12 @@ var (
|
|||||||
|
|
||||||
// Payload combines response metadata and data
|
// Payload combines response metadata and data
|
||||||
type Payload struct {
|
type Payload struct {
|
||||||
XMLName struct{} `json:"-" xml:"ocs"`
|
Meta data.Meta `json:"meta" xml:"meta"`
|
||||||
Meta data.Meta `json:"meta" xml:"meta"`
|
Data interface{} `json:"data,omitempty" xml:"data,omitempty"`
|
||||||
Data interface{} `json:"data,omitempty" xml:"data,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalXML handles ocs specific wrapping of array members in 'element' tags for the data
|
// MarshalXML handles ocs specific wrapping of array members in 'element' tags for the data
|
||||||
func (p Payload) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
|
func (rsp Response) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
|
||||||
// first the easy part
|
// first the easy part
|
||||||
// use ocs as the surrounding tag
|
// use ocs as the surrounding tag
|
||||||
start.Name = ocsName
|
start.Name = ocsName
|
||||||
@@ -38,15 +37,15 @@ func (p Payload) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// encode the meta tag
|
// encode the meta tag
|
||||||
if err = e.EncodeElement(p.Meta, metaStartElement); err != nil {
|
if err = e.EncodeElement(rsp.OCS.Meta, metaStartElement); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// we need to use reflection to determine if p.Data is an array or a slice
|
// we need to use reflection to determine if p.Data is an array or a slice
|
||||||
rt := reflect.TypeOf(p.Data)
|
rt := reflect.TypeOf(rsp.OCS.Data)
|
||||||
if rt != nil && (rt.Kind() == reflect.Array || rt.Kind() == reflect.Slice) {
|
if rt != nil && (rt.Kind() == reflect.Array || rt.Kind() == reflect.Slice) {
|
||||||
// this is how to wrap the data elements in their own <element> tag
|
// this is how to wrap the data elements in their own <element> tag
|
||||||
v := reflect.ValueOf(p.Data)
|
v := reflect.ValueOf(rsp.OCS.Data)
|
||||||
if err = e.EncodeToken(xml.StartElement{Name: dataName}); err != nil {
|
if err = e.EncodeToken(xml.StartElement{Name: dataName}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -58,7 +57,7 @@ func (p Payload) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error)
|
|||||||
if err = e.EncodeToken(xml.EndElement{Name: dataName}); err != nil {
|
if err = e.EncodeToken(xml.EndElement{Name: dataName}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if err = e.EncodeElement(p.Data, xml.StartElement{Name: dataName}); err != nil {
|
} else if err = e.EncodeElement(rsp.OCS.Data, xml.StartElement{Name: dataName}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,30 +69,34 @@ func (p Payload) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render sets the status code of the http response, taking the ocs version into account
|
// Render sets the status code of the http response, taking the ocs version into account
|
||||||
func (p *Payload) Render(w http.ResponseWriter, r *http.Request) error {
|
func (rsp *Response) Render(w http.ResponseWriter, r *http.Request) error {
|
||||||
version := APIVersion(r.Context())
|
version := APIVersion(r.Context())
|
||||||
m := statusCodeMapper(version)
|
m := statusCodeMapper(version)
|
||||||
statusCode := m(p.Meta)
|
statusCode := m(rsp.OCS.Meta)
|
||||||
render.Status(r, statusCode)
|
render.Status(r, statusCode)
|
||||||
if version == ocsVersion2 && statusCode == http.StatusOK {
|
if version == ocsVersion2 && statusCode == http.StatusOK {
|
||||||
p.Meta.StatusCode = statusCode
|
rsp.OCS.Meta.StatusCode = statusCode
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DataRender creates an OK Payload for the given data
|
// DataRender creates an OK Payload for the given data
|
||||||
func DataRender(d interface{}) render.Renderer {
|
func DataRender(d interface{}) render.Renderer {
|
||||||
return &Payload{
|
return &Response{
|
||||||
Meta: data.MetaOK,
|
&Payload{
|
||||||
Data: d,
|
Meta: data.MetaOK,
|
||||||
|
Data: d,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrRender creates an Error Paylod with the given OCS error code and message
|
// ErrRender creates an Error Paylod with the given OCS error code and message
|
||||||
// The httpcode will be determined using the API version stored in the context
|
// The httpcode will be determined using the API version stored in the context
|
||||||
func ErrRender(c int, m string) render.Renderer {
|
func ErrRender(c int, m string) render.Renderer {
|
||||||
return &Payload{
|
return &Response{
|
||||||
Meta: data.Meta{Status: "error", StatusCode: c, Message: m},
|
&Payload{
|
||||||
|
Meta: data.Meta{Status: "error", StatusCode: c, Message: m},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user