/
caller.go
140 lines (115 loc) · 3.55 KB
/
caller.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package http
import (
"strings"
"github.com/jexia/semaphore/v2/pkg/broker"
"github.com/jexia/semaphore/v2/pkg/broker/logger"
"github.com/jexia/semaphore/v2/pkg/discovery"
"github.com/jexia/semaphore/v2/pkg/functions"
"github.com/jexia/semaphore/v2/pkg/references"
"github.com/jexia/semaphore/v2/pkg/specs"
"github.com/jexia/semaphore/v2/pkg/specs/labels"
"github.com/jexia/semaphore/v2/pkg/specs/types"
"github.com/jexia/semaphore/v2/pkg/transport"
"go.uber.org/zap"
)
// NewCaller constructs a new HTTP caller
func NewCaller() transport.NewCaller {
return func(ctx *broker.Context) transport.Caller {
return &Caller{
ctx: ctx,
}
}
}
// Caller represents the caller constructor
type Caller struct {
ctx *broker.Context
}
// Name returns the name of the given caller
func (caller *Caller) Name() string {
return "http"
}
// Dial constructs a new caller for the given host
func (caller *Caller) Dial(service *specs.Service, functions functions.Custom, opts specs.Options, resolver discovery.Resolver) (transport.Call, error) {
module := broker.WithModule(caller.ctx, "caller", "http")
ctx := logger.WithFields(logger.WithLogger(module), zap.String("service", service.Name))
logger.Info(ctx, "constructing new HTTP caller", zap.String("host", service.Host))
options, err := ParseCallerOptions(opts)
if err != nil {
return nil, err
}
methods := make(map[string]*Method, len(service.Methods))
for _, method := range service.Methods {
request, endpoint, err := GetMethodEndpoint(method)
if err != nil {
return nil, err
}
references, err := TemplateReferences(endpoint, functions)
if err != nil {
return nil, err
}
methods[method.Name] = &Method{
name: method.Name,
request: request,
endpoint: endpoint,
references: references,
}
}
result := &Call{
ctx: caller.ctx,
service: service.Name,
host: service.Host,
proxy: NewProxy(options),
methods: methods,
resolver: resolver,
}
return result, nil
}
// LookupEndpointReferences looks up the references within the given endpoint and returns the newly constructed endpoint
func LookupEndpointReferences(method *Method, store references.Store) string {
result := method.endpoint
for _, prop := range method.references {
ref := store.Load(prop.Reference.String())
if ref == nil || prop.Scalar.Type != types.String {
result = strings.Replace(result, prop.Path, "", 1)
continue
}
str, is := ref.Value.(string)
if !is {
result = strings.Replace(result, prop.Path, "", 1)
continue
}
result = strings.Replace(result, prop.Path, str, 1)
}
return result
}
// TemplateReferences returns the property references within the given value
func TemplateReferences(value string, functions functions.Custom) ([]*specs.Property, error) {
references := RawNamedParameters(value)
result := make([]*specs.Property, 0, len(references))
for _, key := range references {
path := key[1:]
property := &specs.Property{
Path: key,
Label: labels.Optional,
Template: specs.Template{
Reference: &specs.PropertyReference{
Resource: ".params",
Path: path,
},
Scalar: &specs.Scalar{
Type: types.String,
},
},
}
result = append(result, property)
}
return result, nil
}
// GetMethodEndpoint attempts to find the endpoint for the given method.
// Empty values are returned when a empty method name is given.
func GetMethodEndpoint(method *specs.Method) (string, string, error) {
options := method.Options
request := options[MethodOption]
endpoint := options[EndpointOption]
return request, endpoint, nil
}