support request content type and call address

This commit is contained in:
Asim Aslam 2018-04-17 12:12:51 +01:00 committed by Vasiliy Tolstov
parent 3d3d9bb1e7
commit 7e872d1275
3 changed files with 31 additions and 10 deletions

37
http.go
View File

@ -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 &registry.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

View File

@ -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,