Compare commits

...

7 Commits

Author SHA1 Message Date
Nurzhan Ilyassov
916d248147 update tests 2024-03-31 20:21:25 +03:00
Nurzhan Ilyassov
bc59fcb886 fix nil nmsg in case of empty request body 2024-03-31 20:21:25 +03:00
7ea55fb466 update for latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-25 21:12:26 +03:00
76a0bdd67c Merge pull request 'update for latest micro changes' (#114) from options into master
Reviewed-on: #114
2023-08-12 13:18:05 +03:00
8de525a8f8 update for latest micro changes
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-12 13:17:47 +03:00
ef36082f2e Merge pull request 'fix request/response md handling' (#112) from request-respone-md into master
Reviewed-on: #112
2023-07-11 00:48:38 +03:00
21c897be47 fix request/response md handling
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-07-11 00:47:36 +03:00
9 changed files with 113 additions and 178 deletions

2
go.mod
View File

@ -2,4 +2,4 @@ module go.unistack.org/micro-client-http/v4
go 1.19
require go.unistack.org/micro/v4 v4.0.1
require go.unistack.org/micro/v4 v4.0.18

4
go.sum
View File

@ -1,2 +1,6 @@
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
go.unistack.org/micro/v4 v4.0.6 h1:YFWvTh3VwyOd6NHYTQcf47n2TF5+p/EhpnbuBQX3qhk=
go.unistack.org/micro/v4 v4.0.6/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
go.unistack.org/micro/v4 v4.0.18 h1:b7WFwem8Nz1xBrRg5FeLnm9CE5gJseHyf9j0BhkiXW0=
go.unistack.org/micro/v4 v4.0.18/go.mod h1:5+da5r835gP0WnNZbYUJDCvWpJ9Xc3IEGyp62e8o8R4=

120
http.go
View File

@ -10,17 +10,16 @@ import (
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"go.unistack.org/micro/v4/broker"
"go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v4/codec"
"go.unistack.org/micro/v4/errors"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/selector"
rutil "go.unistack.org/micro/v4/util/reflect"
)
@ -140,17 +139,22 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client.
if opts.Context != nil {
if md, ok := opts.Context.Value(metadataKey{}).(metadata.Metadata); ok {
for k, v := range md {
header.Set(k, v)
header[k] = v
}
}
}
if opts.AuthToken != "" {
header.Set(metadata.HeaderAuthorization, opts.AuthToken)
}
if opts.RequestMetadata != nil {
for k, v := range opts.RequestMetadata {
header[k] = v
}
}
if md, ok := metadata.FromOutgoingContext(ctx); ok {
for k, v := range md {
header.Set(k, v)
header[k] = v
}
}
@ -212,7 +216,7 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client.
}
if log.V(logger.DebugLevel) {
log.Debugf(ctx, "request %s to %s with headers %v body %s", method, u.String(), hreq.Header, b)
log.Debug(ctx, fmt.Sprintf("request %s to %s with headers %v body %s", method, u.String(), hreq.Header, b))
}
return hreq, nil
@ -300,7 +304,7 @@ func (h *httpClient) newCodec(ct string) (codec.Codec, error) {
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 {
return nil
}
@ -308,9 +312,6 @@ func (h *httpClient) Init(opts ...client.Option) error {
o(&h.opts)
}
if err := h.opts.Broker.Init(); err != nil {
return err
}
if err := h.opts.Tracer.Init(); err != nil {
return err
}
@ -323,9 +324,6 @@ func (h *httpClient) Init(opts ...client.Option) error {
if err := h.opts.Meter.Init(); err != nil {
return err
}
if err := h.opts.Transport.Init(); err != nil {
return err
}
return nil
}
@ -334,15 +332,11 @@ func (h *httpClient) Options() client.Options {
return h.opts
}
func (h *httpClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
return newHTTPMessage(topic, msg, h.opts.ContentType, opts...)
}
func (h *httpClient) NewRequest(service, method string, req interface{}, opts ...client.RequestOption) client.Request {
func (h *httpClient) NewRequest(service, method string, req interface{}, opts ...options.Option) client.Request {
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
callOpts := h.opts.CallOptions
for _, opt := range opts {
@ -359,8 +353,9 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface
} else {
// got a deadline so no need to setup context
// but we need to set the timeout we pass along
opt := client.WithRequestTimeout(time.Until(d))
opt(&callOpts)
if err := options.Set(&callOpts, time.Until(d), ".RequestTimeout"); err != nil {
return errors.New("go.micro.client", fmt.Sprintf("%v", err.Error()), 400)
}
}
// should we noop right here?
@ -374,9 +369,9 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface
hcall := h.call
// wrap the call in reverse
for i := len(callOpts.CallWrappers); i > 0; i-- {
hcall = callOpts.CallWrappers[i-1](hcall)
}
//for i := len(callOpts.CallWrappers); i > 0; i-- {
// hcall = callOpts.CallWrappers[i-1](hcall)
//}
// use the router passed as a call option, or fallback to the rpc clients router
if callOpts.Router == nil {
@ -474,7 +469,7 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface
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
// make a copy of call opts
@ -493,8 +488,9 @@ func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...cli
} else {
// got a deadline so no need to setup context
// but we need to set the timeout we pass along
o := client.WithStreamTimeout(time.Until(d))
o(&callOpts)
if err = options.Set(&callOpts, time.Until(d), ".StreamTimeout"); err != nil {
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", err.Error()), 400)
}
}
// should we noop right here?
@ -614,71 +610,6 @@ func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...cli
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 {
return "http"
}
@ -687,7 +618,7 @@ func (h *httpClient) Name() string {
return h.opts.Name
}
func NewClient(opts ...client.Option) client.Client {
func NewClient(opts ...options.Option) client.Client {
options := client.NewOptions(opts...)
if len(options.ContentType) == 0 {
@ -737,10 +668,5 @@ func NewClient(opts ...client.Option) client.Client {
}
c := client.Client(rc)
// wrap in reverse
for i := len(options.Wrappers); i > 0; i-- {
c = options.Wrappers[i-1](c)
}
return c
}

View File

@ -1,44 +0,0 @@
package http
import (
"go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v4/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
}

View File

@ -4,8 +4,8 @@ import (
"net"
"net/http"
"go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/options"
)
var (
@ -29,98 +29,98 @@ var (
type poolMaxStreams struct{}
// PoolMaxStreams maximum streams on a connectioin
func PoolMaxStreams(n int) client.Option {
return client.SetOption(poolMaxStreams{}, n)
func PoolMaxStreams(n int) options.Option {
return options.ContextOption(poolMaxStreams{}, n)
}
type poolMaxIdle struct{}
// PoolMaxIdle maximum idle conns of a pool
func PoolMaxIdle(d int) client.Option {
return client.SetOption(poolMaxIdle{}, d)
func PoolMaxIdle(d int) options.Option {
return options.ContextOption(poolMaxIdle{}, d)
}
type maxRecvMsgSizeKey struct{}
// MaxRecvMsgSize set the maximum size of message that client can receive.
func MaxRecvMsgSize(s int) client.Option {
return client.SetOption(maxRecvMsgSizeKey{}, s)
func MaxRecvMsgSize(s int) options.Option {
return options.ContextOption(maxRecvMsgSizeKey{}, s)
}
type maxSendMsgSizeKey struct{}
// MaxSendMsgSize set the maximum size of message that client can send.
func MaxSendMsgSize(s int) client.Option {
return client.SetOption(maxSendMsgSizeKey{}, s)
func MaxSendMsgSize(s int) options.Option {
return options.ContextOption(maxSendMsgSizeKey{}, s)
}
type httpClientKey struct{}
// nolint: golint
// HTTPClient pass http.Client option to client Call
func HTTPClient(c *http.Client) client.Option {
return client.SetOption(httpClientKey{}, c)
func HTTPClient(c *http.Client) options.Option {
return options.ContextOption(httpClientKey{}, c)
}
type httpDialerKey struct{}
// nolint: golint
// HTTPDialer pass net.Dialer option to client
func HTTPDialer(d *net.Dialer) client.Option {
return client.SetOption(httpDialerKey{}, d)
func HTTPDialer(d *net.Dialer) options.Option {
return options.ContextOption(httpDialerKey{}, d)
}
type methodKey struct{}
// Method pass method option to client Call
func Method(m string) client.CallOption {
return client.SetCallOption(methodKey{}, m)
func Method(m string) options.Option {
return options.ContextOption(methodKey{}, m)
}
type pathKey struct{}
// Path spcecifies path option to client Call
func Path(p string) client.CallOption {
return client.SetCallOption(pathKey{}, p)
func Path(p string) options.Option {
return options.ContextOption(pathKey{}, p)
}
type bodyKey struct{}
// Body specifies body option to client Call
func Body(b string) client.CallOption {
return client.SetCallOption(bodyKey{}, b)
func Body(b string) options.Option {
return options.ContextOption(bodyKey{}, b)
}
type errorMapKey struct{}
func ErrorMap(m map[string]interface{}) client.CallOption {
return client.SetCallOption(errorMapKey{}, m)
func ErrorMap(m map[string]interface{}) options.Option {
return options.ContextOption(errorMapKey{}, m)
}
type structTagsKey struct{}
// StructTags pass tags slice option to client Call
func StructTags(tags []string) client.CallOption {
return client.SetCallOption(structTagsKey{}, tags)
func StructTags(tags []string) options.Option {
return options.ContextOption(structTagsKey{}, tags)
}
type metadataKey struct{}
// Metadata pass metadata to client Call
func Metadata(md metadata.Metadata) client.CallOption {
return client.SetCallOption(metadataKey{}, md)
func Metadata(md metadata.Metadata) options.Option {
return options.ContextOption(metadataKey{}, md)
}
type cookieKey struct{}
// Cookie pass cookie to client Call
func Cookie(cookies ...string) client.CallOption {
return client.SetCallOption(cookieKey{}, cookies)
func Cookie(cookies ...string) options.Option {
return options.ContextOption(cookieKey{}, cookies)
}
type headerKey struct{}
// Header pass cookie to client Call
func Header(headers ...string) client.CallOption {
return client.SetCallOption(headerKey{}, headers)
func Header(headers ...string) options.Option {
return options.ContextOption(headerKey{}, headers)
}

View File

@ -3,6 +3,7 @@ package http
import (
"go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v4/codec"
"go.unistack.org/micro/v4/options"
)
type httpRequest struct {
@ -13,7 +14,7 @@ type httpRequest struct {
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...)
if len(options.ContentType) == 0 {
options.ContentType = contentType

View File

@ -134,14 +134,14 @@ func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http
buf, err = io.ReadAll(hrsp.Body)
if err != nil {
if log.V(logger.ErrorLevel) {
log.Errorf(ctx, "failed to read body: %v", err)
log.Error(ctx, "failed to read body", err)
}
return errors.InternalServerError("go.micro.client", string(buf))
}
}
if log.V(logger.DebugLevel) {
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
log.Debug(ctx, fmt.Sprintf("response %s with %v", buf, hrsp.Header))
}
if hrsp.StatusCode < 400 {

24
util.go
View File

@ -13,6 +13,7 @@ import (
"go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v4/errors"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/metadata"
rutil "go.unistack.org/micro/v4/util/reflect"
)
@ -216,13 +217,21 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
_, _ = b.WriteString(values.Encode())
}
if rutil.IsZero(nmsg) {
if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) {
return b.String(), nil, nil
}
return b.String(), nmsg, nil
}
func isEmptyStruct(v interface{}) bool {
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
return val.Kind() == reflect.Struct && val.NumField() == 0
}
func newTemplate(path string) ([]string, error) {
if len(path) == 0 || path[0] != '/' {
return nil, fmt.Errorf("path must starts with /")
@ -252,6 +261,13 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
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 {
case <-ctx.Done():
err = ctx.Err()
@ -266,7 +282,7 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
buf, err = io.ReadAll(hrsp.Body)
if err != nil {
if h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(ctx, "failed to read body: %v", err)
h.opts.Logger.Error(ctx, "failed to read body", err)
}
return errors.InternalServerError("go.micro.client", string(buf))
}
@ -275,13 +291,13 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
cf, cerr := h.newCodec(ct)
if cerr != nil {
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.Debug(ctx, fmt.Sprintf("response with %v unknown content-type %s %s", hrsp.Header, ct, buf))
}
return errors.InternalServerError("go.micro.client", cerr.Error())
}
if h.opts.Logger.V(logger.DebugLevel) {
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
h.opts.Logger.Debug(ctx, fmt.Sprintf("response %s with %v", buf, hrsp.Header))
}
// succeseful response

View File

@ -59,6 +59,38 @@ func TestNewPathRequest(t *testing.T) {
}
}
func TestNewPathRequestWithEmptyBody(t *testing.T) {
val := struct{}{}
cases := []string{
"",
"*",
"{}",
"nil",
`{"type": "invalid"}`,
}
for _, body := range cases {
for _, m := range []string{"POST", "PUT", "PATCH", "GET", "DELETE"} {
path, nmsg, err := newPathRequest("/v1/test", m, body, val, []string{"protobuf", "json"}, nil)
if err != nil {
t.Fatal(err)
}
if nmsg == nil {
t.Fatalf("invalid path: nil nmsg")
}
u, err := url.Parse(path)
if err != nil {
t.Fatal(err)
}
vals := u.Query()
if len(vals) != 0 {
t.Fatalf("invalid path: %v nmsg: %v", path, nmsg)
}
}
}
}
func TestNewPathVarRequest(t *testing.T) {
type Message struct {
Name string `json:"name"`