-
Notifications
You must be signed in to change notification settings - Fork 5
/
Client.go
executable file
·103 lines (90 loc) · 3.5 KB
/
Client.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
package dashboard
import(
"errors"
"fmt"
"encoding/json"
"github.com/cohesity/management-sdk-go/models"
"github.com/cohesity/management-sdk-go/unirest-go"
"github.com/cohesity/management-sdk-go/apihelper"
"github.com/cohesity/management-sdk-go/configuration"
)
/*
* Client structure as interface implementation
*/
type DASHBOARD_IMPL struct {
config configuration.CONFIGURATION
}
/**
* If no parameters are specified, dashboard for the local cluster is returned.
* @param *int64 clusterId parameter: Optional
* @param *bool allClusters parameter: Optional
* @param *bool refresh parameter: Optional
* @param []models.TileTypesEnum tileTypes parameter: Optional
* @return Returns the *models.DashboardResponse response from the API call
*/
func (me *DASHBOARD_IMPL) GetDashboard (
clusterId *int64,
allClusters *bool,
refresh *bool,
tileTypes []models.TileTypesEnum) (*models.DashboardResponse, error) {
//the endpoint path uri
_pathUrl := "/public/dashboard"
//variable to hold errors
var err error = nil
//the base uri for api requests
_queryBuilder := configuration.GetBaseURI(configuration.DEFAULT_HOST,me.config);
//prepare query string for API call
_queryBuilder = _queryBuilder + _pathUrl
//process optional query parameters
_queryBuilder, err = apihelper.AppendUrlWithQueryParameters(_queryBuilder, map[string]interface{} {
"clusterId" : clusterId,
"allClusters" : allClusters,
"refresh" : refresh,
"tileTypes" : models.TileTypesEnumArrayToValue(tileTypes),
})
if err != nil {
//error in query param handling
return nil, err
}
//validate and preprocess url
_queryBuilder, err = apihelper.CleanUrl(_queryBuilder)
if err != nil {
//error in url validation or cleaning
return nil, err
}
if me.config.AccessToken() == nil {
return nil, errors.New("Access Token not set. Please authorize the client using client.Authorize()");
}
//prepare headers for the outgoing request
headers := map[string]interface{} {
"user-agent" : "cohesity-Go-sdk-6.2.0",
"accept" : "application/json",
"Authorization" : fmt.Sprintf("%s %s",*me.config.AccessToken().TokenType, *me.config.AccessToken().AccessToken),
}
//prepare API request
_request := unirest.Get(_queryBuilder, headers)
//and invoke the API call request to fetch the response
_response, err := unirest.AsString(_request,me.config.SkipSSL());
if err != nil {
//error in API invocation
return nil, err
}
//error handling using HTTP status codes
if (_response.Code == 0) {
err = apihelper.NewAPIError("Error", _response.Code, _response.RawBody)
} else if (_response.Code < 200) || (_response.Code > 206) { //[200,206] = HTTP OK
err = apihelper.NewAPIError("HTTP Response Not OK", _response.Code, _response.RawBody)
}
if(err != nil) {
//error detected in status code validation
return nil, err
}
//returning the response
var retVal *models.DashboardResponse = &models.DashboardResponse{}
err = json.Unmarshal(_response.RawBody, &retVal)
if err != nil {
//error in parsing
return nil, err
}
return retVal, nil
}