/
selectables.go
106 lines (99 loc) · 3.33 KB
/
selectables.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
/*
* Copyright (c) 2022 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package selectables
import (
"bytes"
"encoding/json"
"fmt"
"github.com/SENERGY-Platform/smart-service-repository/pkg/auth"
"github.com/SENERGY-Platform/smart-service-repository/pkg/configuration"
"github.com/SENERGY-Platform/smart-service-repository/pkg/model"
"log"
"net/http"
"net/url"
"runtime/debug"
)
type Selectables struct {
config configuration.Config
}
func New(config configuration.Config) *Selectables {
return &Selectables{config: config}
}
func (this *Selectables) Get(token auth.Token, searchedEntities []string, criteria []model.Criteria) (result []model.Selectable, err error, code int) {
requestBody := new(bytes.Buffer)
err = json.NewEncoder(requestBody).Encode(criteria)
if err != nil {
return result, err, http.StatusInternalServerError
}
var query url.Values = map[string][]string{}
if len(searchedEntities) == 0 {
query.Set("include_devices", "true")
query.Set("include_groups", "true")
query.Set("include_imports", "true")
query.Set("import_path_trim_first_element", "true")
} else {
for _, searched := range searchedEntities {
if searched == model.DeviceFilter {
query.Set("include_devices", "true")
}
if searched == model.GroupFilter {
query.Set("include_groups", "true")
}
if searched == model.ImportFilter {
query.Set("include_imports", "true")
query.Set("import_path_trim_first_element", "true")
}
if searched == model.DeviceServiceGroupFilter {
query.Set("include_devices", "true")
query.Set("include_id_modified", "true")
}
}
}
endpoint := this.config.DeviceSelectionApi + "/v2/query/selectables?" + query.Encode()
req, err := http.NewRequest("POST", endpoint, requestBody)
if err != nil {
log.Println("ERROR: ", err)
temp, _ := json.Marshal(criteria)
log.Printf("used request=\n%v\n", string(temp))
debug.PrintStack()
return result, err, http.StatusInternalServerError
}
req.Header.Set("Authorization", token.Jwt())
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("ERROR: ", err)
temp, _ := json.Marshal(criteria)
log.Printf("used request=\n%v\n", string(temp))
debug.PrintStack()
return result, err, http.StatusInternalServerError
}
if resp.StatusCode >= 300 {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
err = fmt.Errorf("unable to load selectables: %v, %v", resp.StatusCode, buf.String())
criteriaJson, _ := json.Marshal(criteria)
log.Println("ERROR:", err, searchedEntities, string(criteriaJson))
return result, err, http.StatusInternalServerError
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
log.Println("ERROR: ", err)
debug.PrintStack()
return result, err, http.StatusInternalServerError
}
return result, nil, http.StatusOK
}