Compare commits

...

5 Commits

Author SHA1 Message Date
5cdd48329e metadata.FromContext => metadata.FromOutgoingContext
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-31 10:43:38 +03:00
Renovate Bot
01b5e1db54 Update module github.com/unistack-org/micro/v3 to v3.3.9 2021-03-30 01:17:00 +00:00
Renovate Bot
900e08458b Update module github.com/unistack-org/micro/v3 to v3.3.8 2021-03-29 01:30:57 +00:00
Renovate Bot
f4fff1c77a Update module github.com/unistack-org/micro/v3 to v3.3.4 2021-03-26 16:34:13 +00:00
4bbf97a309 add minor dev comment
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-26 15:52:52 +03:00
3 changed files with 26 additions and 13 deletions

2
go.mod
View File

@@ -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.3.3 require github.com/unistack-org/micro/v3 v3.3.9

5
go.sum
View File

@@ -5,9 +5,8 @@ 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.3.3 h1:Igkzl8tWPlIacEK9z8hHVIzhdyzi8drQPt0Am2iHAcA= github.com/unistack-org/micro/v3 v3.3.9/go.mod h1:M3RGgKZX1OlTn0QB+3R1tlSoGwmEg6rb/Isr0lLYmjQ=
github.com/unistack-org/micro/v3 v3.3.3/go.mod h1:tX95c0Qx4w6oqU7qKThs9lya9P507BdZ29MsTVDmU6w= golang.org/x/net v0.0.0-20210326220855-61e056675ecf/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20210324205630-d1beb07c2056/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
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-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210324051608-47abb6519492/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=

30
http.go
View File

@@ -98,13 +98,18 @@ 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
if nmsg != nil {
var b []byte var b []byte
if ct == "x-www-form-urlencoded" {
fmt.Printf("XXXXX %#+v\n", nmsg)
} else if nmsg != nil {
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", 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))
} }
@@ -114,7 +119,7 @@ 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)
} }
@@ -127,8 +132,16 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request,
// set the content type for the request // set the content type for the request
header.Set("Content-Type", ct) header.Set("Content-Type", ct)
var cf codec.Codec
var err error
// get codec // get codec
cf, err := h.newCodec(ct) switch ct {
case "x-www-form-urlencoded":
cf, err = h.newCodec(DefaultContentType)
default:
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())
} }
@@ -164,7 +177,7 @@ 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 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)
@@ -255,8 +268,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 {
@@ -525,7 +538,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)
} }
@@ -598,6 +611,7 @@ func NewClient(opts ...client.Option) client.Client {
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 {
// TODO customTransport := http.DefaultTransport.(*http.Transport).Clone()
tr := &http.Transport{ tr := &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext, DialContext: dialer.DialContext,