2020-09-05 02:11:29 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-10-09 13:43:04 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
2020-09-05 02:11:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2020-10-09 13:43:04 +03:00
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2020-09-05 02:11:29 +03:00
|
|
|
)
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
var (
|
2020-12-08 00:38:37 +03:00
|
|
|
// DefaultCodecs will be used to encode/decode data
|
2020-11-23 16:18:47 +03:00
|
|
|
DefaultCodecs = map[string]codec.Codec{
|
|
|
|
//"application/json": cjson.NewCodec,
|
|
|
|
//"application/json-rpc": cjsonrpc.NewCodec,
|
|
|
|
//"application/protobuf": cproto.NewCodec,
|
|
|
|
//"application/proto-rpc": cprotorpc.NewCodec,
|
|
|
|
"application/octet-stream": codec.NewCodec(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
type noopClient struct {
|
2020-09-05 02:11:29 +03:00
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopMessage struct {
|
2020-10-09 13:43:04 +03:00
|
|
|
topic string
|
|
|
|
payload interface{}
|
|
|
|
opts MessageOptions
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type noopRequest struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
body interface{}
|
|
|
|
codec codec.Codec
|
2020-09-05 02:11:29 +03:00
|
|
|
service string
|
|
|
|
method string
|
|
|
|
endpoint string
|
|
|
|
contentType string
|
|
|
|
stream bool
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewClient returns new noop client
|
|
|
|
func NewClient(opts ...Option) Client {
|
2021-02-12 16:33:16 +03:00
|
|
|
nc := &noopClient{opts: NewOptions(opts...)}
|
|
|
|
// wrap in reverse
|
|
|
|
|
|
|
|
c := Client(nc)
|
|
|
|
|
|
|
|
for i := len(nc.opts.Wrappers); i > 0; i-- {
|
|
|
|
c = nc.opts.Wrappers[i-1](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
2020-11-03 01:08:23 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (n *noopClient) Name() string {
|
|
|
|
return n.opts.Name
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
func (n *noopRequest) Service() string {
|
|
|
|
return n.service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Method() string {
|
|
|
|
return n.method
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Endpoint() string {
|
|
|
|
return n.endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) ContentType() string {
|
|
|
|
return n.contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Body() interface{} {
|
|
|
|
return n.body
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
func (n *noopRequest) Codec() codec.Codec {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Stream() bool {
|
|
|
|
return n.stream
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopResponse struct {
|
2020-11-23 16:18:47 +03:00
|
|
|
codec codec.Codec
|
2020-11-18 16:50:41 +03:00
|
|
|
header metadata.Metadata
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
func (n *noopResponse) Codec() codec.Codec {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.codec
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:50:41 +03:00
|
|
|
func (n *noopResponse) Header() metadata.Metadata {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopResponse) Read() ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopStream struct{}
|
|
|
|
|
|
|
|
func (n *noopStream) Context() context.Context {
|
|
|
|
return context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Request() Request {
|
|
|
|
return &noopRequest{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Response() Response {
|
|
|
|
return &noopResponse{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Send(interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Recv(interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Error() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopMessage) Topic() string {
|
|
|
|
return n.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopMessage) Payload() interface{} {
|
|
|
|
return n.payload
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopMessage) ContentType() string {
|
2020-10-09 13:43:04 +03:00
|
|
|
return n.opts.ContentType
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
func (n *noopClient) newCodec(contentType string) (codec.Codec, error) {
|
|
|
|
if cf, ok := n.opts.Codecs[contentType]; ok {
|
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
if cf, ok := DefaultCodecs[contentType]; ok {
|
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
return nil, codec.ErrUnknownContentType
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Init(opts ...Option) error {
|
2020-09-05 02:11:29 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&n.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Options() Options {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.opts
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) String() string {
|
2020-09-05 02:11:29 +03:00
|
|
|
return "noop"
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error {
|
2020-09-05 02:11:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request {
|
2021-02-18 15:57:42 +03:00
|
|
|
return &noopRequest{service: service, endpoint: endpoint}
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) NewMessage(topic string, msg interface{}, opts ...MessageOption) Message {
|
|
|
|
options := NewMessageOptions(opts...)
|
2020-10-09 13:43:04 +03:00
|
|
|
return &noopMessage{topic: topic, payload: msg, opts: options}
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) {
|
2020-09-05 02:11:29 +03:00
|
|
|
return &noopStream{}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Publish(ctx context.Context, p Message, opts ...PublishOption) error {
|
2020-10-09 13:43:04 +03:00
|
|
|
var body []byte
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
options := NewPublishOptions(opts...)
|
2020-10-09 13:43:04 +03:00
|
|
|
|
2021-02-09 01:08:45 +03:00
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
2020-10-09 13:43:04 +03:00
|
|
|
if !ok {
|
2020-11-04 00:38:12 +03:00
|
|
|
md = metadata.New(0)
|
2020-10-09 13:43:04 +03:00
|
|
|
}
|
|
|
|
md["Content-Type"] = p.ContentType()
|
|
|
|
md["Micro-Topic"] = p.Topic()
|
|
|
|
|
|
|
|
// passed in raw data
|
2020-11-23 16:18:47 +03:00
|
|
|
if d, ok := p.Payload().(*codec.Frame); ok {
|
2020-10-09 13:43:04 +03:00
|
|
|
body = d.Data
|
|
|
|
} else {
|
2020-11-23 16:18:47 +03:00
|
|
|
// use codec for payload
|
|
|
|
cf, err := n.newCodec(p.ContentType())
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
2020-10-09 13:43:04 +03:00
|
|
|
// set the body
|
2020-10-16 09:38:57 +03:00
|
|
|
b, err := cf.Marshal(p.Payload())
|
2020-10-09 13:43:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
body = b
|
|
|
|
}
|
|
|
|
|
|
|
|
topic := p.Topic()
|
|
|
|
|
|
|
|
// get the exchange
|
|
|
|
if len(options.Exchange) > 0 {
|
|
|
|
topic = options.Exchange
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
return n.opts.Broker.Publish(ctx, topic, &broker.Message{
|
2020-10-09 13:43:04 +03:00
|
|
|
Header: md,
|
|
|
|
Body: body,
|
|
|
|
}, broker.PublishContext(options.Context))
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|