Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
5f029fd432 | |||
8555ebdd5c | |||
273da35b92 | |||
556e8dd568 | |||
f3573e651b |
2
go.mod
2
go.mod
@@ -2,4 +2,4 @@ module github.com/unistack-org/micro-client-http/v3
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/unistack-org/micro/v3 v3.4.8
|
||||
require github.com/unistack-org/micro/v3 v3.5.2
|
||||
|
8
go.sum
8
go.sum
@@ -1,12 +1,12 @@
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
||||
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
|
||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||
github.com/unistack-org/micro/v3 v3.4.8 h1:9+qGlNHgChC3aMuFrtTFUtG55PEAjneSvplg7phwoCI=
|
||||
github.com/unistack-org/micro/v3 v3.4.8/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk=
|
||||
github.com/unistack-org/micro/v3 v3.5.2 h1:8b9Mk4FLWRLp8SduBh5Xs6g/3xJ+ZIBOnH82eHuLWnw=
|
||||
github.com/unistack-org/micro/v3 v3.5.2/go.mod h1:1ZkwpEqpiHiVhM2hiF9DamtpsF04oFybFhEQ4zEMcro=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
22
http.go
22
http.go
@@ -85,22 +85,22 @@ func newRequest(ctx context.Context, addr string, req client.Request, ct string,
|
||||
|
||||
u, err = u.Parse(path)
|
||||
if err != nil {
|
||||
return nil, errors.BadRequest("go.micro.client_v", err.Error())
|
||||
return nil, errors.BadRequest("go.micro.client", 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())
|
||||
return nil, errors.BadRequest("go.micro.client", 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())
|
||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
b, err := cf.Marshal(nmsg)
|
||||
if err != nil {
|
||||
return nil, errors.BadRequest("go.micro.client_e", err.Error())
|
||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
var hreq *http.Request
|
||||
@@ -112,7 +112,7 @@ func newRequest(ctx context.Context, addr string, req client.Request, ct string,
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.BadRequest("go.micro.client_k", err.Error())
|
||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
header := make(http.Header)
|
||||
@@ -126,7 +126,7 @@ func newRequest(ctx context.Context, addr string, req client.Request, ct string,
|
||||
}
|
||||
|
||||
if opts.AuthToken != "" {
|
||||
hreq.Header.Set("Authorization", opts.AuthToken)
|
||||
hreq.Header.Set(metadata.HeaderAuthorization, opts.AuthToken)
|
||||
}
|
||||
|
||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||
@@ -137,14 +137,14 @@ func newRequest(ctx context.Context, addr string, req client.Request, ct string,
|
||||
|
||||
// set timeout in nanoseconds
|
||||
if opts.StreamTimeout > time.Duration(0) {
|
||||
hreq.Header.Set("Timeout", fmt.Sprintf("%d", opts.StreamTimeout))
|
||||
hreq.Header.Set(metadata.HeaderTimeout, fmt.Sprintf("%d", opts.StreamTimeout))
|
||||
}
|
||||
if opts.RequestTimeout > time.Duration(0) {
|
||||
hreq.Header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
||||
hreq.Header.Set(metadata.HeaderTimeout, fmt.Sprintf("%d", opts.RequestTimeout))
|
||||
}
|
||||
|
||||
// set the content type for the request
|
||||
hreq.Header.Set("Content-Type", ct)
|
||||
hreq.Header.Set(metadata.HeaderContentType, ct)
|
||||
|
||||
return hreq, nil
|
||||
}
|
||||
@@ -539,8 +539,8 @@ func (h *httpClient) Publish(ctx context.Context, p client.Message, opts ...clie
|
||||
if !ok {
|
||||
md = metadata.New(2)
|
||||
}
|
||||
md["Content-Type"] = p.ContentType()
|
||||
md["Micro-Topic"] = p.Topic()
|
||||
md[metadata.HeaderContentType] = p.ContentType()
|
||||
md[metadata.HeaderTopic] = p.Topic()
|
||||
|
||||
cf, err := h.newCodec(p.ContentType())
|
||||
if err != nil {
|
||||
|
19
util.go
19
util.go
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/unistack-org/micro/v3/client"
|
||||
"github.com/unistack-org/micro/v3/errors"
|
||||
"github.com/unistack-org/micro/v3/logger"
|
||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||
util "github.com/unistack-org/micro/v3/util/router"
|
||||
)
|
||||
@@ -113,7 +114,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
||||
continue
|
||||
}
|
||||
|
||||
// nolint: gocritic
|
||||
// nolint: gocritic, nestif
|
||||
if _, ok := fieldsmap[t.name]; ok {
|
||||
switch val.Type().Kind() {
|
||||
case reflect.Slice:
|
||||
@@ -124,8 +125,10 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
||||
default:
|
||||
fieldsmap[t.name] = fmt.Sprintf("%v", val.Interface())
|
||||
}
|
||||
} else if (body == "*" || body == t.name) && method != http.MethodGet && tnmsg.Field(i).CanSet() {
|
||||
} else if (body == "*" || body == t.name) && method != http.MethodGet {
|
||||
if tnmsg.Field(i).CanSet() {
|
||||
tnmsg.Field(i).Set(val)
|
||||
}
|
||||
} else {
|
||||
if val.Type().Kind() == reflect.Slice {
|
||||
for idx := 0; idx < val.Len(); idx++ {
|
||||
@@ -209,7 +212,17 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
|
||||
}
|
||||
|
||||
cf, cerr := h.newCodec(ct)
|
||||
if cerr != nil {
|
||||
if hrsp.StatusCode >= 400 && cerr != nil {
|
||||
var buf []byte
|
||||
if hrsp.Body != nil {
|
||||
buf, err = io.ReadAll(hrsp.Body)
|
||||
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
||||
h.opts.Logger.Errorf(ctx, "failed to read body: %v", err)
|
||||
}
|
||||
}
|
||||
// response like text/plain or something else, return original error
|
||||
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
||||
} else if cerr != nil {
|
||||
return errors.InternalServerError("go.micro.client", cerr.Error())
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user