/
rest_invoker.go
executable file
·57 lines (46 loc) · 1.39 KB
/
rest_invoker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package core
import (
"context"
"fmt"
"github.com/go-chassis/go-chassis/client/rest"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/invocation"
)
// RestInvoker is rest invoker
// one invoker for one microservice
// thread safe
type RestInvoker struct {
*abstractInvoker
}
// NewRestInvoker is gives the object of rest invoker
func NewRestInvoker(opt ...Option) *RestInvoker {
opts := newOptions(opt...)
ri := &RestInvoker{
abstractInvoker: &abstractInvoker{
opts: opts,
},
}
return ri
}
// ContextDo is for requesting the API
// by default if http status is 5XX, then it will return error
func (ri *RestInvoker) ContextDo(ctx context.Context, req *rest.Request, options ...InvocationOption) (*rest.Response, error) {
if string(req.GetRequest().URL.Scheme) != "cse" {
return nil, fmt.Errorf("scheme invalid: %s, only support cse://", req.GetRequest().URL.Scheme)
}
opts := getOpts(req.GetRequest().Host, options...)
opts.Protocol = common.ProtocolRest
resp := rest.NewResponse()
inv := invocation.New(ctx)
wrapInvocationWithOpts(inv, opts)
inv.MicroServiceName = req.GetRequest().Host
// TODO load from openAPI schema
// inv.SchemaID = schemaID
// inv.OperationID = operationID
inv.Args = req
inv.Reply = resp
inv.URLPathFormat = req.Req.URL.Path
inv.SetMetadata(common.RestMethod, req.GetMethod())
err := ri.invoke(inv)
return resp, err
}