MSP-1021 refactor invoker
This commit is contained in:
1 parent
40c8fb88cc
commit
76b9b7774e
2 files changed
+38
-28
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/integration-system/isp-kit/grpc/client"
|
||||
"github.com/pkg/errors"
|
||||
md "google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
cli *client.Client
|
||||
}
|
||||
|
||||
func NewRouter(cli *client.Client) Router {
|
||||
return Router{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (i Router) Invoke(methodPath string, request interface{}, metadata map[string]interface{}) (interface{}, error) {
|
||||
ctx := context.Background()
|
||||
newMd := md.New(nil)
|
||||
for key, val := range metadata {
|
||||
newMd.Set(key, fmt.Sprint(val))
|
||||
}
|
||||
ctx = md.NewOutgoingContext(ctx, newMd)
|
||||
|
||||
var response interface{}
|
||||
|
||||
err := i.cli.Invoke(methodPath).JsonRequestBody(request).ReadJsonResponse(&response).Do(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "invoke")
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/backend"
|
||||
"google.golang.org/grpc"
|
||||
md "google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
var Client = backend.NewRxGrpcClient(
|
||||
backend.WithDialOptions(grpc.WithInsecure()),
|
||||
)
|
||||
|
||||
func Invoke(methodPath string, request interface{}, metadata map[string]interface{}) (interface{}, error) {
|
||||
newMd := md.New(nil)
|
||||
for key, val := range metadata {
|
||||
newMd.Set(key, fmt.Sprint(val))
|
||||
}
|
||||
|
||||
var response interface{}
|
||||
err := Client.Invoke(methodPath, -3, request, &response, backend.WithMetadata(newMd))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
Reference in new issue
Block a user