rework newRequest
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
520dc29f89
commit
e497b5fa89
144
http.go
144
http.go
@ -38,33 +38,29 @@ type httpClient struct {
|
|||||||
init bool
|
init bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) {
|
func newRequest(ctx context.Context, addr string, req client.Request, ct string, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) {
|
||||||
hreq := &http.Request{Method: http.MethodPost}
|
|
||||||
body := "*" // as like google api http annotation
|
|
||||||
|
|
||||||
var tags []string
|
var tags []string
|
||||||
var scheme string
|
scheme := "http"
|
||||||
|
method := http.MethodPost
|
||||||
|
body := "*" // as like google api http annotation
|
||||||
|
host := addr
|
||||||
|
path := req.Endpoint()
|
||||||
|
|
||||||
u, err := url.Parse(addr)
|
u, err := url.Parse(addr)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
hreq.URL = &url.URL{
|
scheme = u.Scheme
|
||||||
Scheme: "http",
|
path = u.Path
|
||||||
Host: addr,
|
host = u.Host
|
||||||
Path: req.Endpoint(),
|
} else {
|
||||||
}
|
u = &url.URL{Scheme: scheme, Path: path, Host: host}
|
||||||
hreq.Host = addr
|
|
||||||
scheme = "http"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: nestif
|
|
||||||
if scheme == "" {
|
|
||||||
ep := req.Endpoint()
|
|
||||||
if opts.Context != nil {
|
if opts.Context != nil {
|
||||||
if m, ok := opts.Context.Value(methodKey{}).(string); ok {
|
if m, ok := opts.Context.Value(methodKey{}).(string); ok {
|
||||||
hreq.Method = m
|
method = m
|
||||||
}
|
}
|
||||||
if p, ok := opts.Context.Value(pathKey{}).(string); ok {
|
if p, ok := opts.Context.Value(pathKey{}).(string); ok {
|
||||||
ep = p
|
path += p
|
||||||
}
|
}
|
||||||
if b, ok := opts.Context.Value(bodyKey{}).(string); ok {
|
if b, ok := opts.Context.Value(bodyKey{}).(string); ok {
|
||||||
body = b
|
body = b
|
||||||
@ -72,20 +68,8 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
|
|||||||
if t, ok := opts.Context.Value(structTagsKey{}).([]string); ok && len(t) > 0 {
|
if t, ok := opts.Context.Value(structTagsKey{}).([]string); ok && len(t) > 0 {
|
||||||
tags = t
|
tags = t
|
||||||
}
|
}
|
||||||
if md, ok := opts.Context.Value(metadataKey{}).(metadata.Metadata); ok {
|
|
||||||
for k, v := range md {
|
|
||||||
hreq.Header.Set(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hreq.URL, err = u.Parse(ep)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if opts.AuthToken != "" {
|
|
||||||
hreq.Header.Set("Authorization", opts.AuthToken)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tags) == 0 {
|
if len(tags) == 0 {
|
||||||
switch ct {
|
switch ct {
|
||||||
default:
|
default:
|
||||||
@ -95,30 +79,73 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
path, nmsg, err := newPathRequest(hreq.URL.Path, hreq.Method, body, msg, tags)
|
if path == "" {
|
||||||
if err != nil {
|
path = req.Endpoint()
|
||||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if scheme != "" {
|
u, err = u.Parse(path)
|
||||||
hreq.URL, err = url.Parse(scheme + "://" + addr + path)
|
|
||||||
} else {
|
|
||||||
hreq.URL, err = url.Parse(addr + path)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
return nil, errors.BadRequest("go.micro.client_v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
path, nmsg, err := newPathRequest(u.Path, method, body, msg, tags)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.BadRequest("go.micro.client_a", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err = url.Parse(fmt.Sprintf("%s://%s%s", scheme, host, path))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.BadRequest("go.micro.client_t", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := cf.Marshal(nmsg)
|
b, err := cf.Marshal(nmsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
return nil, errors.BadRequest("go.micro.client_e", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var hreq *http.Request
|
||||||
if len(b) > 0 {
|
if len(b) > 0 {
|
||||||
hreq.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
hreq, err = http.NewRequestWithContext(ctx, method, u.String(), ioutil.NopCloser(bytes.NewBuffer(b)))
|
||||||
hreq.ContentLength = int64(len(b))
|
hreq.ContentLength = int64(len(b))
|
||||||
|
} else {
|
||||||
|
hreq, err = http.NewRequestWithContext(ctx, method, u.String(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.BadRequest("go.micro.client_k", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
header := make(http.Header)
|
||||||
|
|
||||||
|
if opts.Context != nil {
|
||||||
|
if md, ok := opts.Context.Value(metadataKey{}).(metadata.Metadata); ok {
|
||||||
|
for k, v := range md {
|
||||||
|
header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.AuthToken != "" {
|
||||||
|
hreq.Header.Set("Authorization", opts.AuthToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
|
for k, v := range md {
|
||||||
|
hreq.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set timeout in nanoseconds
|
||||||
|
if opts.StreamTimeout > time.Duration(0) {
|
||||||
|
hreq.Header.Set("Timeout", fmt.Sprintf("%d", opts.StreamTimeout))
|
||||||
|
}
|
||||||
|
if opts.RequestTimeout > time.Duration(0) {
|
||||||
|
hreq.Header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the content type for the request
|
||||||
|
hreq.Header.Set("Content-Type", ct)
|
||||||
|
|
||||||
return hreq, nil
|
return hreq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,24 +159,13 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
hreq, err := newRequest(addr, req, ct, cf, req.Body(), opts)
|
hreq, err := newRequest(ctx, addr, req, ct, cf, req.Body(), opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
|
||||||
for k, v := range md {
|
|
||||||
hreq.Header.Set(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set timeout in nanoseconds
|
|
||||||
hreq.Header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
|
||||||
// set the content type for the request
|
|
||||||
hreq.Header.Set("Content-Type", ct)
|
|
||||||
|
|
||||||
// make the request
|
// make the request
|
||||||
hrsp, err := h.httpcli.Do(hreq.WithContext(ctx))
|
hrsp, err := h.httpcli.Do(hreq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err := err.(type) {
|
switch err := err.(type) {
|
||||||
case *url.Error:
|
case *url.Error:
|
||||||
@ -170,27 +186,10 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpClient) stream(ctx context.Context, addr string, req client.Request, opts client.CallOptions) (client.Stream, error) {
|
func (h *httpClient) stream(ctx context.Context, addr string, req client.Request, opts client.CallOptions) (client.Stream, error) {
|
||||||
var header http.Header
|
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
|
||||||
header = make(http.Header, len(md)+2)
|
|
||||||
for k, v := range md {
|
|
||||||
header.Set(k, v)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
header = make(http.Header, 2)
|
|
||||||
}
|
|
||||||
|
|
||||||
ct := req.ContentType()
|
ct := req.ContentType()
|
||||||
if len(opts.ContentType) > 0 {
|
if len(opts.ContentType) > 0 {
|
||||||
ct = opts.ContentType
|
ct = opts.ContentType
|
||||||
}
|
}
|
||||||
// set timeout in nanoseconds
|
|
||||||
if opts.StreamTimeout > time.Duration(0) {
|
|
||||||
header.Set("Timeout", fmt.Sprintf("%d", opts.StreamTimeout))
|
|
||||||
}
|
|
||||||
// set the content type for the request
|
|
||||||
header.Set("Content-Type", ct)
|
|
||||||
|
|
||||||
// get codec
|
// get codec
|
||||||
cf, err := h.newCodec(ct)
|
cf, err := h.newCodec(ct)
|
||||||
@ -211,7 +210,6 @@ func (h *httpClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
conn: cc,
|
conn: cc,
|
||||||
ct: ct,
|
ct: ct,
|
||||||
cf: cf,
|
cf: cf,
|
||||||
header: header,
|
|
||||||
reader: bufio.NewReader(cc),
|
reader: bufio.NewReader(cc),
|
||||||
request: req,
|
request: req,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -21,7 +21,6 @@ type httpStream struct {
|
|||||||
cf codec.Codec
|
cf codec.Codec
|
||||||
context context.Context
|
context context.Context
|
||||||
request client.Request
|
request client.Request
|
||||||
header http.Header
|
|
||||||
closed chan bool
|
closed chan bool
|
||||||
reader *bufio.Reader
|
reader *bufio.Reader
|
||||||
address string
|
address string
|
||||||
@ -62,13 +61,11 @@ func (h *httpStream) Send(msg interface{}) error {
|
|||||||
return errShutdown
|
return errShutdown
|
||||||
}
|
}
|
||||||
|
|
||||||
hreq, err := newRequest(h.address, h.request, h.ct, h.cf, msg, h.opts)
|
hreq, err := newRequest(h.context, h.address, h.request, h.ct, h.cf, msg, h.opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
hreq.Header = h.header
|
|
||||||
|
|
||||||
return hreq.Write(h.conn)
|
return hreq.Write(h.conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user