Skip to content

Commit

Permalink
Default to json content-type in api
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Jun 24, 2019
1 parent dffbe04 commit 4f982bb
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions api/handler/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,61 +120,63 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var rsp []byte

switch {
// json codecs
case hasCodec(ct, jsonCodecs):
var request json.RawMessage
// proto codecs
case hasCodec(ct, protoCodecs):
request := &proto.Message{}
// if the extracted payload isn't empty lets use it
if len(br) > 0 {
request = json.RawMessage(br)
request = proto.NewMessage(br)
}

// create request/response
var response json.RawMessage
response := &proto.Message{}

req := c.NewRequest(
service.Name,
service.Endpoint.Name,
&request,
request,
client.WithContentType(ct),
)

// make the call
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
if err := c.Call(cx, req, response, client.WithSelectOption(so)); err != nil {
writeError(w, r, err)
return
}

// marshall response
rsp, _ = response.MarshalJSON()
// proto codecs
case hasCodec(ct, protoCodecs):
request := &proto.Message{}
rsp, _ = response.Marshal()
default:
// if json codec is not present set to json
if !hasCodec(ct, jsonCodecs) {
ct = "application/json"
}

// default to trying json
var request json.RawMessage
// if the extracted payload isn't empty lets use it
if len(br) > 0 {
request = proto.NewMessage(br)
request = json.RawMessage(br)
}

// create request/response
response := &proto.Message{}
var response json.RawMessage

req := c.NewRequest(
service.Name,
service.Endpoint.Name,
request,
&request,
client.WithContentType(ct),
)

// make the call
if err := c.Call(cx, req, response, client.WithSelectOption(so)); err != nil {
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
writeError(w, r, err)
return
}

// marshall response
rsp, _ = response.Marshal()
default:
http.Error(w, "Unsupported Content-Type", 400)
return
rsp, _ = response.MarshalJSON()
}

// write the response
Expand Down

0 comments on commit 4f982bb

Please sign in to comment.