From 533ef7adff29bc4b9b7a45ccab2c1c49370757ec Mon Sep 17 00:00:00 2001 From: Alex Tsankov Date: Mon, 29 Jun 2015 14:57:09 -0600 Subject: [PATCH 1/2] Url encoded compatibility. --- rest/handler.go | 92 ++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/rest/handler.go b/rest/handler.go index 96c5a11..b6e7916 100644 --- a/rest/handler.go +++ b/rest/handler.go @@ -17,6 +17,7 @@ limitations under the License. package rest import ( + "bytes" "encoding/json" "fmt" "io" @@ -126,6 +127,13 @@ type requestHandler struct { API } +type myReader struct { + *bytes.Buffer +} + +// So that it implements the io.ReadCloser interface +func (m myReader) Close() error { return nil } + // handleCreate returns a HandlerFunc which will deserialize the request payload, pass // it to the provided create function, and then serialize and dispatch the response. // The serialization mechanism used is specified by the "format" query parameter. @@ -135,34 +143,40 @@ func (h requestHandler) handleCreate(handler ResourceHandler) http.Handler { version := ctx.Version() rules := handler.Rules() - data, err := decodePayload(payloadString(r.Body)) - if err != nil { - // Payload decoding failed. - ctx = ctx.setError(BadRequest(err.Error())) + if r.Header["Content-Type"][0] == "application/x-www-form-urlencoded" { + var data Payload + resource, _ := handler.CreateResource(ctx, data, ctx.Version()) + resource = applyOutboundRules(resource, rules, version) + ctx = ctx.setResult(resource) + ctx = ctx.setStatus(http.StatusCreated) } else { - data, err := applyInboundRules(data, rules, version) + data, err := decodePayload(payloadString(r.Body)) if err != nil { - // Type coercion failed. - ctx = ctx.setError(UnprocessableRequest(err.Error())) + ctx = ctx.setError(BadRequest(err.Error())) } else { - resource, err := handler.CreateResource(ctx, data, ctx.Version()) - if err == nil { - resource = applyOutboundRules(resource, rules, version) - } - - if resource != nil { - ctx = ctx.setResult(resource) - ctx = ctx.setStatus(http.StatusCreated) + data, err := applyInboundRules(data, rules, version) + if err != nil { + // Type coercion failed. + ctx = ctx.setError(UnprocessableRequest(err.Error())) } else { - ctx = ctx.setStatus(http.StatusNoContent) - } + resource, err := handler.CreateResource(ctx, data, ctx.Version()) + if err == nil { + resource = applyOutboundRules(resource, rules, version) + } - if err != nil { - ctx = ctx.setError(err) + if resource != nil { + ctx = ctx.setResult(resource) + ctx = ctx.setStatus(http.StatusCreated) + } else { + ctx = ctx.setStatus(http.StatusNoContent) + } + + if err != nil { + ctx = ctx.setError(err) + } } } } - h.sendResponse(w, ctx) }) } @@ -276,25 +290,33 @@ func (h requestHandler) handleUpdate(handler ResourceHandler) http.Handler { version := ctx.Version() rules := handler.Rules() - data, err := decodePayload(payloadString(r.Body)) - if err != nil { - // Payload decoding failed. - ctx = ctx.setError(BadRequest(err.Error())) + if r.Header["Content-Type"][0] == "application/x-www-form-urlencoded" { + var data Payload + resource, _ := handler.CreateResource(ctx, data, ctx.Version()) + resource = applyOutboundRules(resource, rules, version) + ctx = ctx.setResult(resource) + ctx = ctx.setStatus(http.StatusCreated) } else { - data, err := applyInboundRules(data, rules, version) + data, err := decodePayload(payloadString(r.Body)) if err != nil { - // Type coercion failed. - ctx = ctx.setError(UnprocessableRequest(err.Error())) + // Payload decoding failed. + ctx = ctx.setError(BadRequest(err.Error())) } else { - resource, err := handler.UpdateResource( - ctx, ctx.ResourceID(), data, version) - if err == nil { - resource = applyOutboundRules(resource, rules, version) - } + data, err := applyInboundRules(data, rules, version) + if err != nil { + // Type coercion failed. + ctx = ctx.setError(UnprocessableRequest(err.Error())) + } else { + resource, err := handler.UpdateResource( + ctx, ctx.ResourceID(), data, version) + if err == nil { + resource = applyOutboundRules(resource, rules, version) + } - ctx = ctx.setResult(resource) - ctx = ctx.setError(err) - ctx = ctx.setStatus(http.StatusOK) + ctx = ctx.setResult(resource) + ctx = ctx.setError(err) + ctx = ctx.setStatus(http.StatusOK) + } } } From b184db4b001ec56a98098a21554b8dd677d096d7 Mon Sep 17 00:00:00 2001 From: Alex Tsankov Date: Mon, 29 Jun 2015 15:07:40 -0600 Subject: [PATCH 2/2] Unnecessary Import --- rest/handler.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/rest/handler.go b/rest/handler.go index b6e7916..8624fea 100644 --- a/rest/handler.go +++ b/rest/handler.go @@ -17,7 +17,6 @@ limitations under the License. package rest import ( - "bytes" "encoding/json" "fmt" "io" @@ -127,13 +126,6 @@ type requestHandler struct { API } -type myReader struct { - *bytes.Buffer -} - -// So that it implements the io.ReadCloser interface -func (m myReader) Close() error { return nil } - // handleCreate returns a HandlerFunc which will deserialize the request payload, pass // it to the provided create function, and then serialize and dispatch the response. // The serialization mechanism used is specified by the "format" query parameter.