-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
84 lines (71 loc) · 1.79 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package gossamer
import (
"log"
"net/url"
"strings"
)
func CreateIncomingRequest(url *url.URL, t ProtocolType) (Request, error) {
rp := &SensorThingsResourcePath{
currIndex: -1,
items: []ResourcePathItem{},
}
path := url.Path
pathSplit := strings.Split(path, "/")[2:]
items := []ResourcePathItem{}
pathSplitItems := len(pathSplit)
for idx, val := range pathSplit {
if IsEntity(val) {
navItem := &SensorThingsResourcePathItem{}
entityType := DiscoverEntityType(val)
navItem.entityType = entityType
br1Index := strings.Index(val, "(")
br2Index := strings.Index(val, ")")
if br1Index != -1 && br2Index != -1 {
parenthesisValue := val[br1Index+1 : br2Index]
// Query Option
if strings.HasPrefix(parenthesisValue, "$") {
navItem.queryOptions, _ = CreateQueryOptions(parenthesisValue)
} else {
navItem.entityId = parenthesisValue
}
}
items = append(items, navItem)
} else {
if strings.HasPrefix(val, "$") && idx == pathSplitItems-1 {
rp.property = val
} else {
if idx == pathSplitItems-1 || idx == pathSplitItems-2 {
rp.propertyValue = val
} else {
return nil, ERR_INVALID_ENTITY
}
}
}
}
rp.items = items
queryOpts, err := CreateQueryOptions(url.RawQuery)
if err != nil {
log.Println("Error creating option: ", err)
return nil, err
}
req := &GossamerRequest{
protocol: t,
resourcePath: rp,
queryOptions: queryOpts,
}
return req, nil
}
type GossamerRequest struct {
protocol ProtocolType
resourcePath ResourcePath
queryOptions QueryOptions
}
func (r GossamerRequest) GetProtocol() ProtocolType {
return r.protocol
}
func (r GossamerRequest) GetQueryOptions() QueryOptions {
return r.queryOptions
}
func (r GossamerRequest) GetResourcePath() ResourcePath {
return r.resourcePath
}