From 7e872d1275313d8b4dd2efa58b3e2f36233c03a0 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 17 Apr 2018 12:12:51 +0100 Subject: [PATCH] support request content type and call address --- http.go | 37 ++++++++++++++++++++++++++---------- publication.go => message.go | 0 request.go | 4 ++++ 3 files changed, 31 insertions(+), 10 deletions(-) rename publication.go => message.go (100%) diff --git a/http.go b/http.go index 4911871..eee3a3f 100644 --- a/http.go +++ b/http.go @@ -33,6 +33,27 @@ func init() { cmd.DefaultClients["http"] = NewClient } +func (h *httpClient) next(request client.Request, opts client.CallOptions) (selector.Next, error) { + // return remote address + if len(opts.Address) > 0 { + return func() (*registry.Node, error) { + return ®istry.Node{ + Address: opts.Address, + }, nil + }, nil + } + + // get next nodes from the selector + next, err := h.opts.Selector.Select(request.Service(), opts.SelectOptions...) + if err != nil && err == selector.ErrNotFound { + return nil, errors.NotFound("go.micro.client", err.Error()) + } else if err != nil { + return nil, errors.InternalServerError("go.micro.client", err.Error()) + } + + return next, nil +} + func (h *httpClient) call(ctx context.Context, address string, req client.Request, rsp interface{}, opts client.CallOptions) error { header := make(http.Header) if md, ok := metadata.FromContext(ctx); ok { @@ -175,11 +196,9 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface } // get next nodes from the selector - next, err := h.opts.Selector.Select(req.Service(), callOpts.SelectOptions...) - if err != nil && err == selector.ErrNotFound { - return errors.NotFound("go.micro.client", err.Error()) - } else if err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + next, err := h.next(req, callOpts) + if err != nil { + return err } // check if we already have a deadline @@ -283,11 +302,9 @@ func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...cli } // get next nodes from the selector - next, err := h.opts.Selector.Select(req.Service(), callOpts.SelectOptions...) - if err != nil && err == selector.ErrNotFound { - return nil, errors.NotFound("go.micro.client", err.Error()) - } else if err != nil { - return nil, errors.InternalServerError("go.micro.client", err.Error()) + next, err := h.next(req, callOpts) + if err != nil { + return nil, err } // check if we already have a deadline diff --git a/publication.go b/message.go similarity index 100% rename from publication.go rename to message.go diff --git a/request.go b/request.go index 5ee3b96..740bbcf 100644 --- a/request.go +++ b/request.go @@ -18,6 +18,10 @@ func newHTTPRequest(service, method string, request interface{}, contentType str o(&opts) } + if len(opts.ContentType) > 0 { + contentType = opts.ContentType + } + return &httpRequest{ service: service, method: method,