// Request yields a Request formed from this Call func (c Call) Request() (mercury.Request, error) { req := mercury.NewRequest() req.SetService(c.Service) req.SetEndpoint(c.Endpoint) req.SetHeaders(c.Headers) if c.Context != nil { req.SetContext(c.Context) } if c.Body != nil { req.SetBody(c.Body) if err := c.marshaler().MarshalBody(req); err != nil { return nil, terrors.WrapWithCode(err, nil, terrors.ErrBadRequest) } } return req, nil }
// performCall executes a single Call, unmarshals the response (if there is a response proto), and pushes the updted // clientCall down the response channel func (c *client) performCall(call clientCall, middleware []ClientMiddleware, trans transport.Transport, timeout time.Duration, completion chan<- clientCall) { req := call.req // Ensure we have a request ID before the request middleware is executed if id := req.Id(); id == "" { _uuid, err := uuid.NewV4() if err != nil { log.Error(call.req, "[Mercury:Client] Failed to generate request uuid: %v", err) call.err = terrors.Wrap(err, nil).(*terrors.Error) completion <- call return } req.SetId(_uuid.String()) } // Apply request middleware for _, md := range middleware { req = md.ProcessClientRequest(req) } rsp_, err := trans.Send(req, timeout) if err != nil { call.err = terrors.Wrap(err, nil).(*terrors.Error) } else if rsp_ != nil { rsp := mercury.FromTyphonResponse(rsp_) // For error responses, unmarshal the error, leaving the call's response nil if rsp.IsError() { errRsp := rsp.Copy() if unmarshalErr := c.unmarshaler(rsp, &tperrors.Error{}).UnmarshalPayload(errRsp); unmarshalErr != nil { call.err = terrors.WrapWithCode(unmarshalErr, nil, terrors.ErrBadResponse).(*terrors.Error) } else { err := errRsp.Body().(*tperrors.Error) call.err = terrors.Unmarshal(err) } // Set the response Body to a nil – but typed – interface to avoid type conversion panics if Body // properties are accessed in spite of the error // Relevant: http://golang.org/doc/faq#nil_error if call.rspProto != nil { bodyT := reflect.TypeOf(call.rspProto) rsp.SetBody(reflect.New(bodyT.Elem()).Interface()) } } else if call.rspProto != nil { rsp.SetBody(call.rspProto) if err := c.unmarshaler(rsp, call.rspProto).UnmarshalPayload(rsp); err != nil { call.err = terrors.WrapWithCode(err, nil, terrors.ErrBadResponse).(*terrors.Error) } } call.rsp = rsp } // Apply response/error middleware (in reverse order) for i := len(middleware) - 1; i >= 0; i-- { mw := middleware[i] if call.err != nil { mw.ProcessClientError(call.err, call.req) } else { call.rsp = mw.ProcessClientResponse(call.rsp, call.req) } } completion <- call }