Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
d99e97090c | |||
8de7912a91 | |||
6ccb40bab0 | |||
2b16a8a7a6 | |||
|
d051256839 | ||
68b32989fc | |||
21a41a8e03 | |||
9150958044 | |||
|
e358db44ca | ||
5cdd48329e | |||
|
01b5e1db54 | ||
|
900e08458b | ||
|
f4fff1c77a | ||
4bbf97a309 | |||
02f29b0ef3 | |||
|
ba69a7dfcd | ||
|
45aee3c441 | ||
1787f44c54 |
2
go.mod
2
go.mod
@@ -2,4 +2,4 @@ module github.com/unistack-org/micro-client-http/v3
|
|||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require github.com/unistack-org/micro/v3 v3.2.26
|
require github.com/unistack-org/micro/v3 v3.3.13
|
||||||
|
7
go.sum
7
go.sum
@@ -5,10 +5,11 @@ github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
|
|||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
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/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/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||||
github.com/unistack-org/micro/v3 v3.2.26/go.mod h1:iJwCWq2PECMxigfqe6TPC5GLWvj6P94Kk+PTVZGL3w8=
|
github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68=
|
||||||
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
|
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y=
|
||||||
|
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
106
http.go
106
http.go
@@ -11,6 +11,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/broker"
|
"github.com/unistack-org/micro/v3/broker"
|
||||||
@@ -34,6 +36,8 @@ type httpClient struct {
|
|||||||
opts client.Options
|
opts client.Options
|
||||||
dialer *net.Dialer
|
dialer *net.Dialer
|
||||||
httpcli *http.Client
|
httpcli *http.Client
|
||||||
|
init bool
|
||||||
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) {
|
func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) {
|
||||||
@@ -97,13 +101,12 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
|
|||||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// marshal request is struct not empty
|
b, err := cf.Marshal(nmsg)
|
||||||
if nmsg != nil {
|
if err != nil {
|
||||||
var b []byte
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
||||||
b, err = cf.Marshal(nmsg)
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, errors.BadRequest("go.micro.client", err.Error())
|
if len(b) > 0 {
|
||||||
}
|
|
||||||
hreq.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
hreq.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
||||||
hreq.ContentLength = int64(len(b))
|
hreq.ContentLength = int64(len(b))
|
||||||
}
|
}
|
||||||
@@ -113,25 +116,26 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
|
|||||||
|
|
||||||
func (h *httpClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
func (h *httpClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
||||||
header := make(http.Header, 2)
|
header := make(http.Header, 2)
|
||||||
if md, ok := metadata.FromContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
header.Set(k, v)
|
header.Set(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ct := req.ContentType()
|
ct := req.ContentType()
|
||||||
|
if len(opts.ContentType) > 0 {
|
||||||
|
ct = opts.ContentType
|
||||||
|
}
|
||||||
|
|
||||||
// set timeout in nanoseconds
|
// set timeout in nanoseconds
|
||||||
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
||||||
// set the content type for the request
|
// set the content type for the request
|
||||||
header.Set("Content-Type", ct)
|
header.Set("Content-Type", ct)
|
||||||
|
|
||||||
// get codec
|
|
||||||
cf, err := h.newCodec(ct)
|
cf, err := h.newCodec(ct)
|
||||||
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(addr, req, ct, cf, req.Body(), opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -157,13 +161,19 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request,
|
|||||||
|
|
||||||
defer hrsp.Body.Close()
|
defer hrsp.Body.Close()
|
||||||
|
|
||||||
return parseRsp(ctx, hrsp, cf, rsp, opts)
|
if ct == "application/x-www-form-urlencoded" {
|
||||||
|
cf, err = h.newCodec(DefaultContentType)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return h.parseRsp(ctx, hrsp, rsp, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
var header http.Header
|
||||||
|
|
||||||
if md, ok := metadata.FromContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
header = make(http.Header, len(md)+2)
|
header = make(http.Header, len(md)+2)
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
header.Set(k, v)
|
header.Set(k, v)
|
||||||
@@ -173,13 +183,17 @@ func (h *httpClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
ct := req.ContentType()
|
ct := req.ContentType()
|
||||||
|
if len(opts.ContentType) > 0 {
|
||||||
|
ct = opts.ContentType
|
||||||
|
}
|
||||||
|
|
||||||
// set timeout in nanoseconds
|
// set timeout in nanoseconds
|
||||||
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
||||||
// set the content type for the request
|
// set the content type for the request
|
||||||
header.Set("Content-Type", ct)
|
header.Set("Content-Type", ct)
|
||||||
|
|
||||||
// get codec
|
// get codec
|
||||||
cf, err := h.newCodec(req.ContentType())
|
cf, err := h.newCodec(ct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
@@ -189,7 +203,7 @@ func (h *httpClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
if err == nil && u.Scheme != "" && u.Host != "" {
|
if err == nil && u.Scheme != "" && u.Host != "" {
|
||||||
dialAddr = u.Host
|
dialAddr = u.Host
|
||||||
}
|
}
|
||||||
cc, err := h.dialer.DialContext(ctx, "tcp", addr)
|
cc, err := (h.httpcli.Transport).(*http.Transport).DialContext(ctx, "tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error dialing: %v", err))
|
return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error dialing: %v", err))
|
||||||
}
|
}
|
||||||
@@ -209,6 +223,13 @@ func (h *httpClient) stream(ctx context.Context, addr string, req client.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpClient) newCodec(ct string) (codec.Codec, error) {
|
func (h *httpClient) newCodec(ct string) (codec.Codec, error) {
|
||||||
|
h.RLock()
|
||||||
|
defer h.RUnlock()
|
||||||
|
|
||||||
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
|
ct = ct[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
if c, ok := h.opts.Codecs[ct]; ok {
|
if c, ok := h.opts.Codecs[ct]; ok {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
@@ -217,9 +238,32 @@ func (h *httpClient) newCodec(ct string) (codec.Codec, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpClient) Init(opts ...client.Option) error {
|
func (h *httpClient) Init(opts ...client.Option) error {
|
||||||
|
if len(opts) == 0 && h.init {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&h.opts)
|
o(&h.opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := h.opts.Broker.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := h.opts.Tracer.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := h.opts.Router.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := h.opts.Logger.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := h.opts.Meter.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := h.opts.Transport.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,8 +275,8 @@ func (h *httpClient) NewMessage(topic string, msg interface{}, opts ...client.Me
|
|||||||
return newHTTPMessage(topic, msg, h.opts.ContentType, opts...)
|
return newHTTPMessage(topic, msg, h.opts.ContentType, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpClient) NewRequest(service, method string, req interface{}, reqOpts ...client.RequestOption) client.Request {
|
func (h *httpClient) NewRequest(service, method string, req interface{}, opts ...client.RequestOption) client.Request {
|
||||||
return newHTTPRequest(service, method, req, h.opts.ContentType, reqOpts...)
|
return newHTTPRequest(service, method, req, h.opts.ContentType, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||||
@@ -501,7 +545,7 @@ func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
|||||||
func (h *httpClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
func (h *httpClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
||||||
options := client.NewPublishOptions(opts...)
|
options := client.NewPublishOptions(opts...)
|
||||||
|
|
||||||
md, ok := metadata.FromContext(ctx)
|
md, ok := metadata.FromOutgoingContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
md = metadata.New(2)
|
md = metadata.New(2)
|
||||||
}
|
}
|
||||||
@@ -563,17 +607,31 @@ func NewClient(opts ...client.Option) client.Client {
|
|||||||
opts: options,
|
opts: options,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dialer, ok := options.Context.Value(httpDialerKey{}).(*net.Dialer)
|
||||||
|
if !ok {
|
||||||
|
dialer = &net.Dialer{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if httpcli, ok := options.Context.Value(httpClientKey{}).(*http.Client); ok {
|
if httpcli, ok := options.Context.Value(httpClientKey{}).(*http.Client); ok {
|
||||||
rc.httpcli = httpcli
|
rc.httpcli = httpcli
|
||||||
} else {
|
} else {
|
||||||
rc.httpcli = http.DefaultClient
|
// TODO customTransport := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
|
tr := &http.Transport{
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
DialContext: dialer.DialContext,
|
||||||
|
ForceAttemptHTTP2: true,
|
||||||
|
MaxConnsPerHost: 100,
|
||||||
|
MaxIdleConns: 20,
|
||||||
|
IdleConnTimeout: 60 * time.Second,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
|
TLSClientConfig: options.TLSConfig,
|
||||||
|
}
|
||||||
|
rc.httpcli = &http.Client{Transport: tr}
|
||||||
}
|
}
|
||||||
if dialer, ok := options.Context.Value(httpDialerKey{}).(*net.Dialer); ok {
|
|
||||||
rc.dialer = dialer
|
|
||||||
} else {
|
|
||||||
rc.dialer = &net.Dialer{}
|
|
||||||
}
|
|
||||||
|
|
||||||
c := client.Client(rc)
|
c := client.Client(rc)
|
||||||
|
|
||||||
// wrap in reverse
|
// wrap in reverse
|
||||||
|
12
options.go
12
options.go
@@ -1,7 +1,6 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -33,28 +32,21 @@ type tlsAuth struct{}
|
|||||||
type maxRecvMsgSizeKey struct{}
|
type maxRecvMsgSizeKey struct{}
|
||||||
type maxSendMsgSizeKey struct{}
|
type maxSendMsgSizeKey struct{}
|
||||||
|
|
||||||
// maximum streams on a connectioin
|
// PoolMaxStreams maximum streams on a connectioin
|
||||||
func PoolMaxStreams(n int) client.Option {
|
func PoolMaxStreams(n int) client.Option {
|
||||||
return client.SetOption(poolMaxStreams{}, n)
|
return client.SetOption(poolMaxStreams{}, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// maximum idle conns of a pool
|
// PoolMaxIdle maximum idle conns of a pool
|
||||||
func PoolMaxIdle(d int) client.Option {
|
func PoolMaxIdle(d int) client.Option {
|
||||||
return client.SetOption(poolMaxIdle{}, d)
|
return client.SetOption(poolMaxIdle{}, d)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthTLS should be used to setup a secure authentication using TLS
|
|
||||||
func AuthTLS(t *tls.Config) client.Option {
|
|
||||||
return client.SetOption(tlsAuth{}, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// MaxRecvMsgSize set the maximum size of message that client can receive.
|
// MaxRecvMsgSize set the maximum size of message that client can receive.
|
||||||
func MaxRecvMsgSize(s int) client.Option {
|
func MaxRecvMsgSize(s int) client.Option {
|
||||||
return client.SetOption(maxRecvMsgSizeKey{}, s)
|
return client.SetOption(maxRecvMsgSizeKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// MaxSendMsgSize set the maximum size of message that client can send.
|
// MaxSendMsgSize set the maximum size of message that client can send.
|
||||||
func MaxSendMsgSize(s int) client.Option {
|
func MaxSendMsgSize(s int) client.Option {
|
||||||
return client.SetOption(maxSendMsgSizeKey{}, s)
|
return client.SetOption(maxSendMsgSizeKey{}, s)
|
||||||
|
@@ -15,7 +15,6 @@ type httpRequest struct {
|
|||||||
|
|
||||||
func newHTTPRequest(service, method string, request interface{}, contentType string, opts ...client.RequestOption) client.Request {
|
func newHTTPRequest(service, method string, request interface{}, contentType string, opts ...client.RequestOption) client.Request {
|
||||||
options := client.NewRequestOptions(opts...)
|
options := client.NewRequestOptions(opts...)
|
||||||
|
|
||||||
if len(options.ContentType) == 0 {
|
if len(options.ContentType) == 0 {
|
||||||
options.ContentType = contentType
|
options.ContentType = contentType
|
||||||
}
|
}
|
||||||
|
39
stream.go
39
stream.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -89,7 +90,7 @@ func (h *httpStream) Recv(msg interface{}) error {
|
|||||||
}
|
}
|
||||||
defer hrsp.Body.Close()
|
defer hrsp.Body.Close()
|
||||||
|
|
||||||
return parseRsp(h.context, hrsp, h.cf, msg, h.opts)
|
return h.parseRsp(h.context, hrsp, h.cf, msg, h.opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpStream) Error() error {
|
func (h *httpStream) Error() error {
|
||||||
@@ -107,3 +108,39 @@ func (h *httpStream) Close() error {
|
|||||||
return h.conn.Close()
|
return h.conn.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *httpStream) parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// fast path return
|
||||||
|
if hrsp.StatusCode == http.StatusNoContent {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if hrsp.StatusCode < 400 {
|
||||||
|
if err = cf.ReadBody(hrsp.Body, rsp); err != nil {
|
||||||
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
||||||
|
if ok && errmap != nil {
|
||||||
|
if err, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
||||||
|
err, ok = errmap["default"].(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
buf, err := io.ReadAll(hrsp.Body)
|
||||||
|
if err != nil {
|
||||||
|
errors.InternalServerError("go.micro.client", err.Error())
|
||||||
|
}
|
||||||
|
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
||||||
|
}
|
||||||
|
|
||||||
|
if cerr := cf.ReadBody(hrsp.Body, err); cerr != nil {
|
||||||
|
err = errors.InternalServerError("go.micro.client", cerr.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
93
util.go
93
util.go
@@ -3,14 +3,14 @@ package http
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/client"
|
"github.com/unistack-org/micro/v3/client"
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
|
||||||
"github.com/unistack-org/micro/v3/errors"
|
"github.com/unistack-org/micro/v3/errors"
|
||||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||||
util "github.com/unistack-org/micro/v3/util/router"
|
util "github.com/unistack-org/micro/v3/util/router"
|
||||||
@@ -32,6 +32,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
|||||||
return "", nil, fmt.Errorf("nil message but path params requested: %v", path)
|
return "", nil, fmt.Errorf("nil message but path params requested: %v", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fieldsmapskip := make(map[string]struct{})
|
||||||
fieldsmap := make(map[string]string, len(tpl.Fields))
|
fieldsmap := make(map[string]string, len(tpl.Fields))
|
||||||
for _, v := range tpl.Fields {
|
for _, v := range tpl.Fields {
|
||||||
fieldsmap[v] = ""
|
fieldsmap[v] = ""
|
||||||
@@ -53,7 +54,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
|||||||
tnmsg = tnmsg.Elem()
|
tnmsg = tnmsg.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
values := make(map[string]string)
|
values := url.Values{}
|
||||||
// copy cycle
|
// copy cycle
|
||||||
for i := 0; i < tmsg.NumField(); i++ {
|
for i := 0; i < tmsg.NumField(); i++ {
|
||||||
val := tmsg.Field(i)
|
val := tmsg.Field(i)
|
||||||
@@ -87,18 +88,36 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
|||||||
t.name = strings.ToLower(fld.Name)
|
t.name = strings.ToLower(fld.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !val.IsValid() || val.IsZero() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := fieldsmap[t.name]; ok {
|
if _, ok := fieldsmap[t.name]; ok {
|
||||||
fieldsmap[t.name] = fmt.Sprintf("%v", val.Interface())
|
if val.Type().Kind() == reflect.Slice {
|
||||||
|
for idx := 0; idx < val.Len(); idx++ {
|
||||||
|
values.Add(t.name, fmt.Sprintf("%v", val.Index(idx).Interface()))
|
||||||
|
}
|
||||||
|
fieldsmapskip[t.name] = struct{}{}
|
||||||
|
} else {
|
||||||
|
fieldsmap[t.name] = fmt.Sprintf("%v", val.Interface())
|
||||||
|
}
|
||||||
} else if (body == "*" || body == t.name) && method != http.MethodGet {
|
} else if (body == "*" || body == t.name) && method != http.MethodGet {
|
||||||
tnmsg.Field(i).Set(val)
|
tnmsg.Field(i).Set(val)
|
||||||
} else {
|
} else {
|
||||||
values[t.name] = fmt.Sprintf("%v", val.Interface())
|
if val.Type().Kind() == reflect.Slice {
|
||||||
|
for idx := 0; idx < val.Len(); idx++ {
|
||||||
|
values.Add(t.name, fmt.Sprintf("%v", val.Index(idx).Interface()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
values.Add(t.name, fmt.Sprintf("%v", val.Interface()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check not filled stuff
|
// check not filled stuff
|
||||||
for k, v := range fieldsmap {
|
for k, v := range fieldsmap {
|
||||||
if v == "" {
|
_, ok := fieldsmapskip[k]
|
||||||
|
if !ok && v == "" {
|
||||||
return "", nil, fmt.Errorf("path param %s not filled", k)
|
return "", nil, fmt.Errorf("path param %s not filled", k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,23 +126,17 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
|
|||||||
for _, fld := range tpl.Pool {
|
for _, fld := range tpl.Pool {
|
||||||
_, _ = b.WriteRune('/')
|
_, _ = b.WriteRune('/')
|
||||||
if v, ok := fieldsmap[fld]; ok {
|
if v, ok := fieldsmap[fld]; ok {
|
||||||
_, _ = b.WriteString(v)
|
if v != "" {
|
||||||
|
_, _ = b.WriteString(v)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_, _ = b.WriteString(fld)
|
_, _ = b.WriteString(fld)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
idx := 0
|
if len(values) > 0 {
|
||||||
for k, v := range values {
|
_, _ = b.WriteRune('?')
|
||||||
if idx == 0 {
|
_, _ = b.WriteString(values.Encode())
|
||||||
_, _ = b.WriteRune('?')
|
|
||||||
} else {
|
|
||||||
_, _ = b.WriteRune('&')
|
|
||||||
}
|
|
||||||
_, _ = b.WriteString(k)
|
|
||||||
_, _ = b.WriteRune('=')
|
|
||||||
_, _ = b.WriteString(v)
|
|
||||||
idx++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if rutil.IsZero(nmsg) {
|
if rutil.IsZero(nmsg) {
|
||||||
@@ -155,36 +168,46 @@ func newTemplate(path string) (util.Template, error) {
|
|||||||
return tpl, nil
|
return tpl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
|
func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp interface{}, opts client.CallOptions) error {
|
||||||
b, err := ioutil.ReadAll(hrsp.Body)
|
// fast path return
|
||||||
|
if hrsp.StatusCode == http.StatusNoContent {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ct := DefaultContentType
|
||||||
|
|
||||||
|
if htype := hrsp.Header.Get("Content-Type"); htype != "" {
|
||||||
|
ct = htype
|
||||||
|
}
|
||||||
|
|
||||||
|
cf, err := h.newCodec(ct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if hrsp.StatusCode < 400 {
|
if hrsp.StatusCode < 400 {
|
||||||
// unmarshal
|
if err := cf.ReadBody(hrsp.Body, rsp); err != nil {
|
||||||
if err := cf.Unmarshal(b, rsp); err != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
||||||
if !ok || errmap == nil {
|
if ok && errmap != nil {
|
||||||
// user not provide map of errors
|
if err, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
||||||
// id: req.Service() ??
|
err, ok = errmap["default"].(error)
|
||||||
return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
|
}
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
buf, err := io.ReadAll(hrsp.Body)
|
||||||
|
if err != nil {
|
||||||
|
errors.InternalServerError("go.micro.client", err.Error())
|
||||||
|
}
|
||||||
|
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
if cerr := cf.ReadBody(hrsp.Body, err); cerr != nil {
|
||||||
err, ok = errmap["default"].(error)
|
err = errors.InternalServerError("go.micro.client", cerr.Error())
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
|
|
||||||
}
|
|
||||||
|
|
||||||
if cerr := cf.Unmarshal(b, err); cerr != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", cerr.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user