Compare commits
	
		
			6 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 76a0bdd67c | |||
| 8de525a8f8 | |||
| ef36082f2e | |||
| 21c897be47 | |||
| 0b21dd6660 | |||
| 18eb0d9e5c | 
@@ -5,14 +5,14 @@ This plugin is a http client for micro.
 | 
				
			|||||||
## Overview
 | 
					## Overview
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The http client wraps `net/http` to provide a robust micro client with service discovery, load balancing and streaming. 
 | 
					The http client wraps `net/http` to provide a robust micro client with service discovery, load balancing and streaming. 
 | 
				
			||||||
It complies with the [micro.Client](https://godoc.org/go.unistack.org/micro-client-http/v3#Client) interface.
 | 
					It complies with the [micro.Client](https://godoc.org/go.unistack.org/micro-client-http/v4#Client) interface.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Usage
 | 
					## Usage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Use directly
 | 
					### Use directly
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```go
 | 
					```go
 | 
				
			||||||
import "go.unistack.org/micro-client-http/v3"
 | 
					import "go.unistack.org/micro-client-http/v4"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
service := micro.NewService(
 | 
					service := micro.NewService(
 | 
				
			||||||
	micro.Name("my.service"),
 | 
						micro.Name("my.service"),
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										6
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
				
			|||||||
module go.unistack.org/micro-client-http/v3
 | 
					module go.unistack.org/micro-client-http/v4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.18
 | 
					go 1.19
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require go.unistack.org/micro/v3 v3.10.16
 | 
					require go.unistack.org/micro/v4 v4.0.6
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,8 +1,4 @@
 | 
				
			|||||||
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
 | 
					go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
 | 
				
			||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
 | 
					go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
 | 
				
			||||||
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
 | 
					go.unistack.org/micro/v4 v4.0.6 h1:YFWvTh3VwyOd6NHYTQcf47n2TF5+p/EhpnbuBQX3qhk=
 | 
				
			||||||
go.unistack.org/micro/v3 v3.10.16 h1:2er/SKKYbV60M+UuJM4eYCF0MZYAIq/yNUrAbTfgq8Q=
 | 
					go.unistack.org/micro/v4 v4.0.6/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
 | 
				
			||||||
go.unistack.org/micro/v3 v3.10.16/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
 | 
					 | 
				
			||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
					 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
					 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										130
									
								
								http.go
									
									
									
									
									
								
							
							
						
						
									
										130
									
								
								http.go
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
				
			|||||||
// Package http provides a http client
 | 
					// Package http provides a http client
 | 
				
			||||||
package http // import "go.unistack.org/micro-client-http/v3"
 | 
					package http // import "go.unistack.org/micro-client-http/v4"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bufio"
 | 
						"bufio"
 | 
				
			||||||
@@ -10,19 +10,18 @@ import (
 | 
				
			|||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.unistack.org/micro/v3/broker"
 | 
						"go.unistack.org/micro/v4/client"
 | 
				
			||||||
	"go.unistack.org/micro/v3/client"
 | 
						"go.unistack.org/micro/v4/codec"
 | 
				
			||||||
	"go.unistack.org/micro/v3/codec"
 | 
						"go.unistack.org/micro/v4/errors"
 | 
				
			||||||
	"go.unistack.org/micro/v3/errors"
 | 
						"go.unistack.org/micro/v4/logger"
 | 
				
			||||||
	"go.unistack.org/micro/v3/logger"
 | 
						"go.unistack.org/micro/v4/metadata"
 | 
				
			||||||
	"go.unistack.org/micro/v3/metadata"
 | 
						"go.unistack.org/micro/v4/options"
 | 
				
			||||||
	"go.unistack.org/micro/v3/selector"
 | 
						"go.unistack.org/micro/v4/selector"
 | 
				
			||||||
	rutil "go.unistack.org/micro/v3/util/reflect"
 | 
						rutil "go.unistack.org/micro/v4/util/reflect"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var DefaultContentType = "application/json"
 | 
					var DefaultContentType = "application/json"
 | 
				
			||||||
@@ -147,6 +146,11 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client.
 | 
				
			|||||||
	if opts.AuthToken != "" {
 | 
						if opts.AuthToken != "" {
 | 
				
			||||||
		header.Set(metadata.HeaderAuthorization, opts.AuthToken)
 | 
							header.Set(metadata.HeaderAuthorization, opts.AuthToken)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if opts.RequestMetadata != nil {
 | 
				
			||||||
 | 
							for k, v := range opts.RequestMetadata {
 | 
				
			||||||
 | 
								header.Set(k, v)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if md, ok := metadata.FromOutgoingContext(ctx); ok {
 | 
						if md, ok := metadata.FromOutgoingContext(ctx); ok {
 | 
				
			||||||
		for k, v := range md {
 | 
							for k, v := range md {
 | 
				
			||||||
@@ -300,7 +304,7 @@ func (h *httpClient) newCodec(ct string) (codec.Codec, error) {
 | 
				
			|||||||
	return nil, codec.ErrUnknownContentType
 | 
						return nil, codec.ErrUnknownContentType
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (h *httpClient) Init(opts ...client.Option) error {
 | 
					func (h *httpClient) Init(opts ...options.Option) error {
 | 
				
			||||||
	if len(opts) == 0 && h.init {
 | 
						if len(opts) == 0 && h.init {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -308,9 +312,6 @@ func (h *httpClient) Init(opts ...client.Option) error {
 | 
				
			|||||||
		o(&h.opts)
 | 
							o(&h.opts)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err := h.opts.Broker.Init(); err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if err := h.opts.Tracer.Init(); err != nil {
 | 
						if err := h.opts.Tracer.Init(); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -323,9 +324,6 @@ func (h *httpClient) Init(opts ...client.Option) error {
 | 
				
			|||||||
	if err := h.opts.Meter.Init(); err != nil {
 | 
						if err := h.opts.Meter.Init(); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err := h.opts.Transport.Init(); err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -334,15 +332,11 @@ func (h *httpClient) Options() client.Options {
 | 
				
			|||||||
	return h.opts
 | 
						return h.opts
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (h *httpClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
 | 
					func (h *httpClient) NewRequest(service, method string, req interface{}, opts ...options.Option) client.Request {
 | 
				
			||||||
	return newHTTPMessage(topic, msg, h.opts.ContentType, opts...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpClient) NewRequest(service, method string, req interface{}, opts ...client.RequestOption) client.Request {
 | 
					 | 
				
			||||||
	return newHTTPRequest(service, method, req, h.opts.ContentType, opts...)
 | 
						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 ...options.Option) error {
 | 
				
			||||||
	// make a copy of call opts
 | 
						// make a copy of call opts
 | 
				
			||||||
	callOpts := h.opts.CallOptions
 | 
						callOpts := h.opts.CallOptions
 | 
				
			||||||
	for _, opt := range opts {
 | 
						for _, opt := range opts {
 | 
				
			||||||
@@ -359,8 +353,9 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface
 | 
				
			|||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// got a deadline so no need to setup context
 | 
							// got a deadline so no need to setup context
 | 
				
			||||||
		// but we need to set the timeout we pass along
 | 
							// but we need to set the timeout we pass along
 | 
				
			||||||
		opt := client.WithRequestTimeout(time.Until(d))
 | 
							if err := options.Set(&callOpts, time.Until(d), ".RequestTimeout"); err != nil {
 | 
				
			||||||
		opt(&callOpts)
 | 
								return errors.New("go.micro.client", fmt.Sprintf("%v", err.Error()), 400)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// should we noop right here?
 | 
						// should we noop right here?
 | 
				
			||||||
@@ -374,9 +369,9 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface
 | 
				
			|||||||
	hcall := h.call
 | 
						hcall := h.call
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// wrap the call in reverse
 | 
						// wrap the call in reverse
 | 
				
			||||||
	for i := len(callOpts.CallWrappers); i > 0; i-- {
 | 
						//for i := len(callOpts.CallWrappers); i > 0; i-- {
 | 
				
			||||||
		hcall = callOpts.CallWrappers[i-1](hcall)
 | 
						//	hcall = callOpts.CallWrappers[i-1](hcall)
 | 
				
			||||||
	}
 | 
						//}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// use the router passed as a call option, or fallback to the rpc clients router
 | 
						// use the router passed as a call option, or fallback to the rpc clients router
 | 
				
			||||||
	if callOpts.Router == nil {
 | 
						if callOpts.Router == nil {
 | 
				
			||||||
@@ -474,7 +469,7 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface
 | 
				
			|||||||
	return gerr
 | 
						return gerr
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
 | 
					func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// make a copy of call opts
 | 
						// make a copy of call opts
 | 
				
			||||||
@@ -493,8 +488,9 @@ func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...cli
 | 
				
			|||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// got a deadline so no need to setup context
 | 
							// got a deadline so no need to setup context
 | 
				
			||||||
		// but we need to set the timeout we pass along
 | 
							// but we need to set the timeout we pass along
 | 
				
			||||||
		o := client.WithStreamTimeout(time.Until(d))
 | 
							if err = options.Set(&callOpts, time.Until(d), ".StreamTimeout"); err != nil {
 | 
				
			||||||
		o(&callOpts)
 | 
								return nil, errors.New("go.micro.client", fmt.Sprintf("%v", err.Error()), 400)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// should we noop right here?
 | 
						// should we noop right here?
 | 
				
			||||||
@@ -614,71 +610,6 @@ func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...cli
 | 
				
			|||||||
	return nil, grr
 | 
						return nil, grr
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (h *httpClient) BatchPublish(ctx context.Context, p []client.Message, opts ...client.PublishOption) error {
 | 
					 | 
				
			||||||
	return h.publish(ctx, p, opts...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
 | 
					 | 
				
			||||||
	return h.publish(ctx, []client.Message{p}, opts...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpClient) publish(ctx context.Context, ps []client.Message, opts ...client.PublishOption) error {
 | 
					 | 
				
			||||||
	var body []byte
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	options := client.NewPublishOptions(opts...)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// get proxy
 | 
					 | 
				
			||||||
	exchange := ""
 | 
					 | 
				
			||||||
	if v, ok := os.LookupEnv("MICRO_PROXY"); ok {
 | 
					 | 
				
			||||||
		exchange = v
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	omd, ok := metadata.FromOutgoingContext(ctx)
 | 
					 | 
				
			||||||
	if !ok {
 | 
					 | 
				
			||||||
		omd = metadata.New(2)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	msgs := make([]*broker.Message, 0, len(ps))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for _, p := range ps {
 | 
					 | 
				
			||||||
		md := metadata.Copy(omd)
 | 
					 | 
				
			||||||
		md[metadata.HeaderContentType] = p.ContentType()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// passed in raw data
 | 
					 | 
				
			||||||
		if d, ok := p.Payload().(*codec.Frame); ok {
 | 
					 | 
				
			||||||
			body = d.Data
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			// use codec for payload
 | 
					 | 
				
			||||||
			cf, err := h.newCodec(p.ContentType())
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				return errors.InternalServerError("go.micro.client", err.Error())
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			// set the body
 | 
					 | 
				
			||||||
			b, err := cf.Marshal(p.Payload())
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				return errors.InternalServerError("go.micro.client", err.Error())
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			body = b
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		topic := p.Topic()
 | 
					 | 
				
			||||||
		if len(exchange) > 0 {
 | 
					 | 
				
			||||||
			topic = exchange
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		for k, v := range p.Metadata() {
 | 
					 | 
				
			||||||
			md.Set(k, v)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		md.Set(metadata.HeaderTopic, topic)
 | 
					 | 
				
			||||||
		msgs = append(msgs, &broker.Message{Header: md, Body: body})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return h.opts.Broker.BatchPublish(ctx, msgs,
 | 
					 | 
				
			||||||
		broker.PublishContext(ctx),
 | 
					 | 
				
			||||||
		broker.PublishBodyOnly(options.BodyOnly),
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpClient) String() string {
 | 
					func (h *httpClient) String() string {
 | 
				
			||||||
	return "http"
 | 
						return "http"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -687,7 +618,7 @@ func (h *httpClient) Name() string {
 | 
				
			|||||||
	return h.opts.Name
 | 
						return h.opts.Name
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewClient(opts ...client.Option) client.Client {
 | 
					func NewClient(opts ...options.Option) client.Client {
 | 
				
			||||||
	options := client.NewOptions(opts...)
 | 
						options := client.NewOptions(opts...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(options.ContentType) == 0 {
 | 
						if len(options.ContentType) == 0 {
 | 
				
			||||||
@@ -737,10 +668,5 @@ func NewClient(opts ...client.Option) client.Client {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	c := client.Client(rc)
 | 
						c := client.Client(rc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// wrap in reverse
 | 
					 | 
				
			||||||
	for i := len(options.Wrappers); i > 0; i-- {
 | 
					 | 
				
			||||||
		c = options.Wrappers[i-1](c)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return c
 | 
						return c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										44
									
								
								message.go
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								message.go
									
									
									
									
									
								
							@@ -1,44 +0,0 @@
 | 
				
			|||||||
package http
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"go.unistack.org/micro/v3/client"
 | 
					 | 
				
			||||||
	"go.unistack.org/micro/v3/metadata"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type httpMessage struct {
 | 
					 | 
				
			||||||
	payload     interface{}
 | 
					 | 
				
			||||||
	topic       string
 | 
					 | 
				
			||||||
	contentType string
 | 
					 | 
				
			||||||
	opts        client.MessageOptions
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func newHTTPMessage(topic string, payload interface{}, contentType string, opts ...client.MessageOption) client.Message {
 | 
					 | 
				
			||||||
	options := client.NewMessageOptions(opts...)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(options.ContentType) > 0 {
 | 
					 | 
				
			||||||
		contentType = options.ContentType
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return &httpMessage{
 | 
					 | 
				
			||||||
		payload:     payload,
 | 
					 | 
				
			||||||
		topic:       topic,
 | 
					 | 
				
			||||||
		contentType: contentType,
 | 
					 | 
				
			||||||
		opts:        options,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpMessage) ContentType() string {
 | 
					 | 
				
			||||||
	return h.contentType
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpMessage) Topic() string {
 | 
					 | 
				
			||||||
	return h.topic
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpMessage) Payload() interface{} {
 | 
					 | 
				
			||||||
	return h.payload
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (h *httpMessage) Metadata() metadata.Metadata {
 | 
					 | 
				
			||||||
	return h.opts.Metadata
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										60
									
								
								options.go
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								options.go
									
									
									
									
									
								
							@@ -4,8 +4,8 @@ import (
 | 
				
			|||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.unistack.org/micro/v3/client"
 | 
						"go.unistack.org/micro/v4/metadata"
 | 
				
			||||||
	"go.unistack.org/micro/v3/metadata"
 | 
						"go.unistack.org/micro/v4/options"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
@@ -29,98 +29,98 @@ var (
 | 
				
			|||||||
type poolMaxStreams struct{}
 | 
					type poolMaxStreams struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// PoolMaxStreams maximum streams on a connectioin
 | 
					// PoolMaxStreams maximum streams on a connectioin
 | 
				
			||||||
func PoolMaxStreams(n int) client.Option {
 | 
					func PoolMaxStreams(n int) options.Option {
 | 
				
			||||||
	return client.SetOption(poolMaxStreams{}, n)
 | 
						return options.ContextOption(poolMaxStreams{}, n)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type poolMaxIdle struct{}
 | 
					type poolMaxIdle struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// PoolMaxIdle maximum idle conns of a pool
 | 
					// PoolMaxIdle maximum idle conns of a pool
 | 
				
			||||||
func PoolMaxIdle(d int) client.Option {
 | 
					func PoolMaxIdle(d int) options.Option {
 | 
				
			||||||
	return client.SetOption(poolMaxIdle{}, d)
 | 
						return options.ContextOption(poolMaxIdle{}, d)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type maxRecvMsgSizeKey struct{}
 | 
					type maxRecvMsgSizeKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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) options.Option {
 | 
				
			||||||
	return client.SetOption(maxRecvMsgSizeKey{}, s)
 | 
						return options.ContextOption(maxRecvMsgSizeKey{}, s)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type maxSendMsgSizeKey struct{}
 | 
					type maxSendMsgSizeKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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) options.Option {
 | 
				
			||||||
	return client.SetOption(maxSendMsgSizeKey{}, s)
 | 
						return options.ContextOption(maxSendMsgSizeKey{}, s)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type httpClientKey struct{}
 | 
					type httpClientKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// nolint: golint
 | 
					// nolint: golint
 | 
				
			||||||
// HTTPClient pass http.Client option to client Call
 | 
					// HTTPClient pass http.Client option to client Call
 | 
				
			||||||
func HTTPClient(c *http.Client) client.Option {
 | 
					func HTTPClient(c *http.Client) options.Option {
 | 
				
			||||||
	return client.SetOption(httpClientKey{}, c)
 | 
						return options.ContextOption(httpClientKey{}, c)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type httpDialerKey struct{}
 | 
					type httpDialerKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// nolint: golint
 | 
					// nolint: golint
 | 
				
			||||||
// HTTPDialer pass net.Dialer option to client
 | 
					// HTTPDialer pass net.Dialer option to client
 | 
				
			||||||
func HTTPDialer(d *net.Dialer) client.Option {
 | 
					func HTTPDialer(d *net.Dialer) options.Option {
 | 
				
			||||||
	return client.SetOption(httpDialerKey{}, d)
 | 
						return options.ContextOption(httpDialerKey{}, d)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type methodKey struct{}
 | 
					type methodKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Method pass method option to client Call
 | 
					// Method pass method option to client Call
 | 
				
			||||||
func Method(m string) client.CallOption {
 | 
					func Method(m string) options.Option {
 | 
				
			||||||
	return client.SetCallOption(methodKey{}, m)
 | 
						return options.ContextOption(methodKey{}, m)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type pathKey struct{}
 | 
					type pathKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Path spcecifies path option to client Call
 | 
					// Path spcecifies path option to client Call
 | 
				
			||||||
func Path(p string) client.CallOption {
 | 
					func Path(p string) options.Option {
 | 
				
			||||||
	return client.SetCallOption(pathKey{}, p)
 | 
						return options.ContextOption(pathKey{}, p)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type bodyKey struct{}
 | 
					type bodyKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Body specifies body option to client Call
 | 
					// Body specifies body option to client Call
 | 
				
			||||||
func Body(b string) client.CallOption {
 | 
					func Body(b string) options.Option {
 | 
				
			||||||
	return client.SetCallOption(bodyKey{}, b)
 | 
						return options.ContextOption(bodyKey{}, b)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type errorMapKey struct{}
 | 
					type errorMapKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ErrorMap(m map[string]interface{}) client.CallOption {
 | 
					func ErrorMap(m map[string]interface{}) options.Option {
 | 
				
			||||||
	return client.SetCallOption(errorMapKey{}, m)
 | 
						return options.ContextOption(errorMapKey{}, m)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type structTagsKey struct{}
 | 
					type structTagsKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// StructTags pass tags slice option to client Call
 | 
					// StructTags pass tags slice option to client Call
 | 
				
			||||||
func StructTags(tags []string) client.CallOption {
 | 
					func StructTags(tags []string) options.Option {
 | 
				
			||||||
	return client.SetCallOption(structTagsKey{}, tags)
 | 
						return options.ContextOption(structTagsKey{}, tags)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type metadataKey struct{}
 | 
					type metadataKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Metadata pass metadata to client Call
 | 
					// Metadata pass metadata to client Call
 | 
				
			||||||
func Metadata(md metadata.Metadata) client.CallOption {
 | 
					func Metadata(md metadata.Metadata) options.Option {
 | 
				
			||||||
	return client.SetCallOption(metadataKey{}, md)
 | 
						return options.ContextOption(metadataKey{}, md)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type cookieKey struct{}
 | 
					type cookieKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Cookie pass cookie to client Call
 | 
					// Cookie pass cookie to client Call
 | 
				
			||||||
func Cookie(cookies ...string) client.CallOption {
 | 
					func Cookie(cookies ...string) options.Option {
 | 
				
			||||||
	return client.SetCallOption(cookieKey{}, cookies)
 | 
						return options.ContextOption(cookieKey{}, cookies)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type headerKey struct{}
 | 
					type headerKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Header pass cookie to client Call
 | 
					// Header pass cookie to client Call
 | 
				
			||||||
func Header(headers ...string) client.CallOption {
 | 
					func Header(headers ...string) options.Option {
 | 
				
			||||||
	return client.SetCallOption(headerKey{}, headers)
 | 
						return options.ContextOption(headerKey{}, headers)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,9 @@
 | 
				
			|||||||
package http
 | 
					package http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"go.unistack.org/micro/v3/client"
 | 
						"go.unistack.org/micro/v4/client"
 | 
				
			||||||
	"go.unistack.org/micro/v3/codec"
 | 
						"go.unistack.org/micro/v4/codec"
 | 
				
			||||||
 | 
						"go.unistack.org/micro/v4/options"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type httpRequest struct {
 | 
					type httpRequest struct {
 | 
				
			||||||
@@ -13,7 +14,7 @@ type httpRequest struct {
 | 
				
			|||||||
	opts        client.RequestOptions
 | 
						opts        client.RequestOptions
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func newHTTPRequest(service, method string, request interface{}, contentType string, opts ...client.RequestOption) client.Request {
 | 
					func newHTTPRequest(service, method string, request interface{}, contentType string, opts ...options.Option) 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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,10 +9,10 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.unistack.org/micro/v3/client"
 | 
						"go.unistack.org/micro/v4/client"
 | 
				
			||||||
	"go.unistack.org/micro/v3/codec"
 | 
						"go.unistack.org/micro/v4/codec"
 | 
				
			||||||
	"go.unistack.org/micro/v3/errors"
 | 
						"go.unistack.org/micro/v4/errors"
 | 
				
			||||||
	"go.unistack.org/micro/v3/logger"
 | 
						"go.unistack.org/micro/v4/logger"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Implements the streamer interface
 | 
					// Implements the streamer interface
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										18
									
								
								util.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								util.go
									
									
									
									
									
								
							@@ -10,10 +10,11 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.unistack.org/micro/v3/client"
 | 
						"go.unistack.org/micro/v4/client"
 | 
				
			||||||
	"go.unistack.org/micro/v3/errors"
 | 
						"go.unistack.org/micro/v4/errors"
 | 
				
			||||||
	"go.unistack.org/micro/v3/logger"
 | 
						"go.unistack.org/micro/v4/logger"
 | 
				
			||||||
	rutil "go.unistack.org/micro/v3/util/reflect"
 | 
						"go.unistack.org/micro/v4/metadata"
 | 
				
			||||||
 | 
						rutil "go.unistack.org/micro/v4/util/reflect"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
@@ -252,6 +253,13 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
 | 
				
			|||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if opts.ResponseMetadata != nil {
 | 
				
			||||||
 | 
							*opts.ResponseMetadata = metadata.New(len(hrsp.Header))
 | 
				
			||||||
 | 
							for k, v := range hrsp.Header {
 | 
				
			||||||
 | 
								opts.ResponseMetadata.Set(k, strings.Join(v, ","))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	select {
 | 
						select {
 | 
				
			||||||
	case <-ctx.Done():
 | 
						case <-ctx.Done():
 | 
				
			||||||
		err = ctx.Err()
 | 
							err = ctx.Err()
 | 
				
			||||||
@@ -275,7 +283,7 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
 | 
				
			|||||||
		cf, cerr := h.newCodec(ct)
 | 
							cf, cerr := h.newCodec(ct)
 | 
				
			||||||
		if cerr != nil {
 | 
							if cerr != nil {
 | 
				
			||||||
			if h.opts.Logger.V(logger.DebugLevel) {
 | 
								if h.opts.Logger.V(logger.DebugLevel) {
 | 
				
			||||||
				h.opts.Logger.Debugf(ctx, "response with %v unknown content-type %s", hrsp.Header, ct, buf)
 | 
									h.opts.Logger.Debugf(ctx, "response with %v unknown content-type %s %s", hrsp.Header, ct, buf)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return errors.InternalServerError("go.micro.client", cerr.Error())
 | 
								return errors.InternalServerError("go.micro.client", cerr.Error())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user