-
Notifications
You must be signed in to change notification settings - Fork 3
/
agent-logic.go
97 lines (75 loc) · 2.31 KB
/
agent-logic.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
/*
Copyright © 2022 Bronco Oostermeyer <dev@bfv.io>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package logic
import (
"encoding/json"
"fmt"
"net/http"
"github.com/bfv/pasoe-cli/model"
)
func printError(err error) {
fmt.Printf("error: %v\n", err)
}
func readJson(res *http.Response) (model.Response, error) {
defer res.Body.Close()
resp := model.Response{}
err := json.NewDecoder(res.Body).Decode(&resp)
return resp, err
}
func extractApplications(res model.Response) ([]model.Application, error) {
var appResponse model.ApplicationResponse
var apps []model.Application
err := json.Unmarshal(res.Result, &appResponse)
if err != nil {
printError(err)
} else {
apps = append(apps, appResponse.Applications...)
}
return apps, err
}
func extractApplicationNames(res model.Response) ([]string, error) {
var appResponse model.ApplicationResponse
var apps []string
err := json.Unmarshal(res.Result, &appResponse)
if err != nil {
printError(err)
} else {
for _, app := range appResponse.Applications {
apps = append(apps, app.Name)
}
}
return apps, err
}
func extractAgents(res model.Response) ([]model.Agent, error) {
var agentResponse model.AgentsResponse
var agents []model.Agent
err := json.Unmarshal(res.Result, &agentResponse)
if err != nil {
printError(err)
} else {
agents = append(agents, agentResponse.Agents...)
}
return agents, err
}
func extractAgentSessions(res model.Response) ([]model.AgentSession, error) {
var agentSessionResponse model.AgentSessionsReponse
var agentSessions []model.AgentSession
err := json.Unmarshal(res.Result, &agentSessionResponse)
if err != nil {
printError(err)
} else {
agentSessions = append(agentSessions, agentSessionResponse.AgentsSessions...)
}
return agentSessions, err
}