-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
69 lines (57 loc) · 1.81 KB
/
request.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
package roc
import (
proto "github.com/treethought/roc/proto/v1"
"google.golang.org/protobuf/types/known/anypb"
)
// Request represents request for a resouce
type Request struct {
m *proto.Request
}
func NewRequest(i Identifier, verb proto.Verb, class RepresentationClass) *Request {
classStr := ""
if class != "" {
classStr = class.String()
}
return &Request{
m: &proto.Request{
Identifier: i.String(),
Verb: verb,
RepresentationClass: classStr,
Arguments: make(map[string]*proto.StringSlice),
ArgumentValues: make(map[string]*anypb.Any),
},
}
}
func (r *Request) Identifier() Identifier {
return NewIdentifier(r.m.Identifier)
}
func (r *Request) Verb() proto.Verb {
return r.m.Verb
}
func (r *Request) SetVerb(verb proto.Verb) {
r.m.Verb = verb
}
// SetRepresentationClass sets the desired format of the representation response
func (r *Request) SetRepresentationClass(class string) {
r.m.RepresentationClass = class
}
// SetArgument sets the value of an argument to an identifier
// The argument's representation can then be sources during evalutation
// This replaces any existing values, to append an identifier, use AddArgument
func (r *Request) SetArgument(name string, i Identifier) {
r.m.Arguments[name] = &proto.StringSlice{
Values: []string{i.String()},
}
}
// AddArgument appends an identifier to any existing ones for the named arguement
func (r *Request) AddArgument(name string, i Identifier) {
_, exists := r.m.Arguments[name]
if !exists {
r.m.Arguments[name] = &proto.StringSlice{}
}
r.m.Arguments[name].Values = append(r.m.Arguments[name].Values, i.String())
}
func (r *Request) SetArgumentByValue(name string, val Representation) {
log.Info("setting argument value", "arg", name, "type", val.Type())
r.m.ArgumentValues[name] = val.any()
}