2017-01-01 20:30:40 +00:00
|
|
|
// Package http provides a http client
|
2017-01-01 18:39:05 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-03-03 12:28:44 +00:00
|
|
|
"context"
|
2017-01-01 18:39:05 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-11-22 22:09:18 +00:00
|
|
|
"os"
|
2017-01-01 18:39:05 +00:00
|
|
|
"time"
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2021-01-16 01:58:43 +03:00
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2021-01-10 14:48:10 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
|
|
|
"github.com/unistack-org/micro/v3/router"
|
2017-01-01 18:39:05 +00:00
|
|
|
)
|
|
|
|
|
2021-02-06 18:54:18 +03:00
|
|
|
var (
|
|
|
|
DefaultContentType = "application/json"
|
|
|
|
)
|
|
|
|
|
2020-07-17 01:29:28 +03:00
|
|
|
func filterLabel(r []router.Route) []router.Route {
|
|
|
|
// selector.FilterLabel("protocol", "http")
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-01-01 18:39:05 +00:00
|
|
|
type httpClient struct {
|
2021-01-10 14:48:10 +03:00
|
|
|
opts client.Options
|
|
|
|
dialer *net.Dialer
|
|
|
|
httpcli *http.Client
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
func newRequest(addr string, req client.Request, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) {
|
2021-01-10 14:48:10 +03:00
|
|
|
hreq := &http.Request{Method: http.MethodPost}
|
2021-01-19 04:12:31 +03:00
|
|
|
body := "*" // as like google api http annotation
|
2021-01-10 14:48:10 +03:00
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
hreq.URL = &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: addr,
|
|
|
|
Path: req.Endpoint(),
|
|
|
|
}
|
|
|
|
hreq.Host = addr
|
|
|
|
} else {
|
|
|
|
ep := req.Endpoint()
|
|
|
|
if opts.Context != nil {
|
|
|
|
if m, ok := opts.Context.Value(methodKey{}).(string); ok {
|
|
|
|
hreq.Method = m
|
|
|
|
}
|
|
|
|
if p, ok := opts.Context.Value(pathKey{}).(string); ok {
|
|
|
|
ep = p
|
|
|
|
}
|
2021-01-19 04:12:31 +03:00
|
|
|
if b, ok := opts.Context.Value(bodyKey{}).(string); ok {
|
|
|
|
body = b
|
|
|
|
}
|
2021-01-10 14:48:10 +03:00
|
|
|
}
|
|
|
|
hreq.URL, err = u.Parse(ep)
|
|
|
|
if err != nil {
|
2021-01-19 04:12:31 +03:00
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
2021-01-10 14:48:10 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 04:12:31 +03:00
|
|
|
|
|
|
|
path, nmsg, err := newPathRequest(hreq.URL.Path, hreq.Method, body, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
hreq.URL.Path = path
|
|
|
|
// marshal request
|
|
|
|
b, err := cf.Marshal(nmsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
hreq.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
|
|
|
hreq.ContentLength = int64(len(b))
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
return hreq, nil
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func (h *httpClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
2021-01-19 04:12:31 +03:00
|
|
|
header := make(http.Header, 2)
|
2017-01-01 18:39:05 +00:00
|
|
|
if md, ok := metadata.FromContext(ctx); ok {
|
|
|
|
for k, v := range md {
|
|
|
|
header.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set timeout in nanoseconds
|
|
|
|
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
|
|
|
// set the content type for the request
|
|
|
|
header.Set("Content-Type", req.ContentType())
|
|
|
|
|
|
|
|
// get codec
|
2021-01-10 14:48:10 +03:00
|
|
|
cf, err := h.newCodec(req.ContentType())
|
2017-01-01 18:39:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
hreq, err := newRequest(addr, req, cf, req.Body(), opts)
|
2017-01-01 18:39:05 +00:00
|
|
|
if err != nil {
|
2021-01-19 04:12:31 +03:00
|
|
|
return err
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
hreq.Header = header
|
2021-01-19 04:12:31 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// make the request
|
|
|
|
hrsp, err := h.httpcli.Do(hreq.WithContext(ctx))
|
2017-01-01 18:39:05 +00:00
|
|
|
if err != nil {
|
2021-01-19 22:55:24 +03:00
|
|
|
switch err := err.(type) {
|
|
|
|
case net.Error:
|
|
|
|
if err.Timeout() {
|
|
|
|
return errors.Timeout("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
case *url.Error:
|
|
|
|
if err, ok := err.Err.(net.Error); ok && err.Timeout() {
|
|
|
|
return errors.Timeout("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2017-01-01 18:39:05 +00:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
2021-01-19 22:55:24 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
defer hrsp.Body.Close()
|
2017-01-01 18:39:05 +00:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
return parseRsp(ctx, hrsp, cf, rsp, opts)
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func (h *httpClient) stream(ctx context.Context, addr string, req client.Request, opts client.CallOptions) (client.Stream, error) {
|
|
|
|
var header http.Header
|
|
|
|
|
2017-01-01 18:39:05 +00:00
|
|
|
if md, ok := metadata.FromContext(ctx); ok {
|
2021-01-10 14:48:10 +03:00
|
|
|
header = make(http.Header, len(md)+2)
|
2017-01-01 18:39:05 +00:00
|
|
|
for k, v := range md {
|
|
|
|
header.Set(k, v)
|
|
|
|
}
|
2021-01-10 14:48:10 +03:00
|
|
|
} else {
|
|
|
|
header = make(http.Header, 2)
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set timeout in nanoseconds
|
|
|
|
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
|
|
|
|
// set the content type for the request
|
|
|
|
header.Set("Content-Type", req.ContentType())
|
|
|
|
|
|
|
|
// get codec
|
2021-01-10 14:48:10 +03:00
|
|
|
cf, err := h.newCodec(req.ContentType())
|
2017-01-01 18:39:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
dialAddr := addr
|
|
|
|
u, err := url.Parse(dialAddr)
|
|
|
|
if err == nil && u.Scheme != "" && u.Host != "" {
|
|
|
|
dialAddr = u.Host
|
|
|
|
}
|
|
|
|
cc, err := h.dialer.DialContext(ctx, "tcp", addr)
|
2017-01-01 18:39:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error dialing: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httpStream{
|
2021-01-10 14:48:10 +03:00
|
|
|
address: addr,
|
2017-01-01 18:39:05 +00:00
|
|
|
context: ctx,
|
|
|
|
closed: make(chan bool),
|
2021-01-10 14:48:10 +03:00
|
|
|
opts: opts,
|
2017-01-01 18:39:05 +00:00
|
|
|
conn: cc,
|
|
|
|
codec: cf,
|
|
|
|
header: header,
|
|
|
|
reader: bufio.NewReader(cc),
|
|
|
|
request: req,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func (h *httpClient) newCodec(ct string) (codec.Codec, error) {
|
|
|
|
if c, ok := h.opts.Codecs[ct]; ok {
|
2017-01-01 18:39:05 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
return nil, codec.ErrUnknownContentType
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) Init(opts ...client.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&h.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) Options() client.Options {
|
|
|
|
return h.opts
|
|
|
|
}
|
|
|
|
|
2018-05-10 17:38:14 +01:00
|
|
|
func (h *httpClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
|
2021-01-10 14:48:10 +03:00
|
|
|
return newHTTPMessage(topic, msg, h.opts.ContentType, opts...)
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) NewRequest(service, method string, req interface{}, reqOpts ...client.RequestOption) client.Request {
|
|
|
|
return newHTTPRequest(service, method, req, h.opts.ContentType, reqOpts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := h.opts.CallOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already have a deadline
|
|
|
|
d, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
2021-01-10 14:48:10 +03:00
|
|
|
var cancel context.CancelFunc
|
2017-01-01 18:39:05 +00:00
|
|
|
// no deadline so we create a new one
|
2021-01-10 14:48:10 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, callOpts.RequestTimeout)
|
|
|
|
defer cancel()
|
2017-01-01 18:39:05 +00:00
|
|
|
} else {
|
|
|
|
// got a deadline so no need to setup context
|
|
|
|
// but we need to set the timeout we pass along
|
|
|
|
opt := client.WithRequestTimeout(d.Sub(time.Now()))
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should we noop right here?
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// make copy of call method
|
|
|
|
hcall := h.call
|
|
|
|
|
|
|
|
// wrap the call in reverse
|
|
|
|
for i := len(callOpts.CallWrappers); i > 0; i-- {
|
|
|
|
hcall = callOpts.CallWrappers[i-1](hcall)
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// use the router passed as a call option, or fallback to the rpc clients router
|
|
|
|
if callOpts.Router == nil {
|
|
|
|
callOpts.Router = h.opts.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
if callOpts.Selector == nil {
|
|
|
|
callOpts.Selector = h.opts.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject proxy address
|
|
|
|
// TODO: don't even bother using Lookup/Select in this case
|
|
|
|
if len(h.opts.Proxy) > 0 {
|
|
|
|
callOpts.Address = []string{h.opts.Proxy}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup the route to send the reques to
|
|
|
|
// TODO apply any filtering here
|
|
|
|
routes, err := h.opts.Lookup(ctx, req, callOpts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
next, err := callOpts.Selector.Select(routes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-01 18:39:05 +00:00
|
|
|
// return errors.New("go.micro.client", "request timeout", 408)
|
|
|
|
call := func(i int) error {
|
|
|
|
// call backoff first. Someone may want an initial start delay
|
|
|
|
t, err := callOpts.Backoff(ctx, req, i)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
node := next()
|
2017-01-01 18:39:05 +00:00
|
|
|
|
|
|
|
// make the call
|
2019-01-18 15:47:50 +00:00
|
|
|
err = hcall(ctx, node, req, rsp, callOpts)
|
2021-01-10 14:48:10 +03:00
|
|
|
// record the result of the call to inform future routing decisions
|
|
|
|
if verr := h.opts.Selector.Record(node, err); verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
|
|
|
|
// try and transform the error to a go-micro error
|
|
|
|
if verr, ok := err.(*errors.Error); ok {
|
|
|
|
return verr
|
|
|
|
}
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2017-01-01 18:39:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan error, callOpts.Retries)
|
|
|
|
var gerr error
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
2017-01-01 18:39:05 +00:00
|
|
|
go func() {
|
|
|
|
ch <- call(i)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
case err := <-ch:
|
|
|
|
// if the call succeeded lets bail early
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retry, rerr := callOpts.Retry(ctx, req, i, err)
|
|
|
|
if rerr != nil {
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gerr
|
|
|
|
}
|
|
|
|
|
2018-04-17 11:26:18 +01:00
|
|
|
func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
2017-01-01 18:39:05 +00:00
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := h.opts.CallOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already have a deadline
|
|
|
|
d, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
2021-01-10 14:48:10 +03:00
|
|
|
var cancel context.CancelFunc
|
2017-01-01 18:39:05 +00:00
|
|
|
// no deadline so we create a new one
|
2021-01-10 14:48:10 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, callOpts.RequestTimeout)
|
|
|
|
defer cancel()
|
2017-01-01 18:39:05 +00:00
|
|
|
} else {
|
|
|
|
// got a deadline so no need to setup context
|
|
|
|
// but we need to set the timeout we pass along
|
|
|
|
opt := client.WithRequestTimeout(d.Sub(time.Now()))
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should we noop right here?
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
/*
|
|
|
|
// make copy of call method
|
|
|
|
hstream, err := h.stream()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// wrap the call in reverse
|
|
|
|
for i := len(callOpts.CallWrappers); i > 0; i-- {
|
|
|
|
hstream = callOpts.CallWrappers[i-1](hstream)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// use the router passed as a call option, or fallback to the rpc clients router
|
|
|
|
if callOpts.Router == nil {
|
|
|
|
callOpts.Router = h.opts.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
if callOpts.Selector == nil {
|
|
|
|
callOpts.Selector = h.opts.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject proxy address
|
|
|
|
// TODO: don't even bother using Lookup/Select in this case
|
|
|
|
if len(h.opts.Proxy) > 0 {
|
|
|
|
callOpts.Address = []string{h.opts.Proxy}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup the route to send the reques to
|
|
|
|
// TODO apply any filtering here
|
|
|
|
routes, err := h.opts.Lookup(ctx, req, callOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
next, err := callOpts.Selector.Select(routes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-17 11:26:18 +01:00
|
|
|
call := func(i int) (client.Stream, error) {
|
2017-01-01 18:39:05 +00:00
|
|
|
// call backoff first. Someone may want an initial start delay
|
|
|
|
t, err := callOpts.Backoff(ctx, req, i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
node := next()
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
stream, err := h.stream(ctx, node, req, callOpts)
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// record the result of the call to inform future routing decisions
|
|
|
|
if verr := h.opts.Selector.Record(node, err); verr != nil {
|
|
|
|
return nil, verr
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// try and transform the error to a go-micro error
|
|
|
|
if verr, ok := err.(*errors.Error); ok {
|
|
|
|
return nil, verr
|
|
|
|
}
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2017-01-01 18:39:05 +00:00
|
|
|
return stream, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
2018-04-17 11:26:18 +01:00
|
|
|
stream client.Stream
|
2017-01-01 18:39:05 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan response, callOpts.Retries)
|
|
|
|
var grr error
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
2017-01-01 18:39:05 +00:00
|
|
|
go func() {
|
|
|
|
s, err := call(i)
|
|
|
|
ch <- response{s, err}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
case rsp := <-ch:
|
|
|
|
// if the call succeeded lets bail early
|
|
|
|
if rsp.err == nil {
|
|
|
|
return rsp.stream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retry, rerr := callOpts.Retry(ctx, req, i, err)
|
|
|
|
if rerr != nil {
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return nil, rsp.err
|
|
|
|
}
|
|
|
|
|
|
|
|
grr = rsp.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, grr
|
|
|
|
}
|
|
|
|
|
2018-04-17 11:26:18 +01:00
|
|
|
func (h *httpClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
2021-01-10 14:48:10 +03:00
|
|
|
options := client.NewPublishOptions(opts...)
|
2019-11-22 22:09:18 +00:00
|
|
|
|
2017-01-01 18:39:05 +00:00
|
|
|
md, ok := metadata.FromContext(ctx)
|
|
|
|
if !ok {
|
2021-01-10 14:48:10 +03:00
|
|
|
md = metadata.New(2)
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
md["Content-Type"] = p.ContentType()
|
2019-11-22 22:09:18 +00:00
|
|
|
md["Micro-Topic"] = p.Topic()
|
2017-01-01 18:39:05 +00:00
|
|
|
|
|
|
|
cf, err := h.newCodec(p.ContentType())
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-11-22 22:09:18 +00:00
|
|
|
var body []byte
|
|
|
|
|
|
|
|
// passed in raw data
|
2021-01-10 14:48:10 +03:00
|
|
|
if d, ok := p.Payload().(*codec.Frame); ok {
|
2019-11-22 22:09:18 +00:00
|
|
|
body = d.Data
|
|
|
|
} else {
|
2021-01-10 14:48:10 +03:00
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
if err := cf.Write(b, &codec.Message{Type: codec.Event}, p.Payload()); err != nil {
|
2019-11-22 22:09:18 +00:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
body = b.Bytes()
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 22:09:18 +00:00
|
|
|
topic := p.Topic()
|
|
|
|
|
|
|
|
// get proxy
|
|
|
|
if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 {
|
|
|
|
options.Exchange = prx
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the exchange
|
|
|
|
if len(options.Exchange) > 0 {
|
|
|
|
topic = options.Exchange
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
return h.opts.Broker.Publish(ctx, topic, &broker.Message{
|
2017-01-01 18:39:05 +00:00
|
|
|
Header: md,
|
2019-11-22 22:09:18 +00:00
|
|
|
Body: body,
|
2021-01-10 14:48:10 +03:00
|
|
|
}, broker.PublishContext(ctx))
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) String() string {
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:11:04 +03:00
|
|
|
func (h *httpClient) Name() string {
|
|
|
|
return h.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func NewClient(opts ...client.Option) client.Client {
|
|
|
|
options := client.NewOptions(opts...)
|
2017-01-01 18:39:05 +00:00
|
|
|
|
|
|
|
if len(options.ContentType) == 0 {
|
2021-02-06 18:54:18 +03:00
|
|
|
options.ContentType = DefaultContentType
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
rc := &httpClient{
|
|
|
|
opts: options,
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
if httpcli, ok := options.Context.Value(httpClientKey{}).(*http.Client); ok {
|
|
|
|
rc.httpcli = httpcli
|
|
|
|
} else {
|
|
|
|
rc.httpcli = http.DefaultClient
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
2021-01-10 14:48:10 +03:00
|
|
|
if dialer, ok := options.Context.Value(httpDialerKey{}).(*net.Dialer); ok {
|
|
|
|
rc.dialer = dialer
|
|
|
|
} else {
|
|
|
|
rc.dialer = &net.Dialer{}
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := client.Client(rc)
|
|
|
|
|
|
|
|
// wrap in reverse
|
|
|
|
for i := len(options.Wrappers); i > 0; i-- {
|
|
|
|
c = options.Wrappers[i-1](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
|
|
|
|
b, err := ioutil.ReadAll(hrsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if hrsp.StatusCode < 400 {
|
|
|
|
// unmarshal
|
|
|
|
if err := cf.Unmarshal(b, 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 {
|
|
|
|
// user not provide map of errors
|
|
|
|
// id: req.Service() ??
|
|
|
|
return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
|
|
|
err, ok = errmap["default"].(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
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|