forked from CheckPointSW/cp-mgmt-api-go-sdk
/
APIResponse.go
69 lines (57 loc) · 1.59 KB
/
APIResponse.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 api_go_sdk
import (
"encoding/json"
"net/http"
)
const OkResponseCode string = "200 OK"
// API Response struct represent http response (Created from httpResponse struct)
type APIResponse struct {
StatusCode string
data map[string]interface{}
Success bool
ErrorMsg string
resObj map[string]interface{}
}
/*
Generate APIResponse from httpResponse object
httpResponse: input HTTP response object
errMsg: if there is an error message included, we include it in the APIResponse
return: The APIResponse object we generated
*/
func fromHTTPResponse(httpResponse *http.Response, errMsg string) (APIResponse, error) {
var data map[string]interface{}
errResp := json.NewDecoder(httpResponse.Body).Decode(&data)
defer httpResponse.Body.Close()
if errResp != nil {
return APIResponse{}, errResp
}
return APIResponse{httpResponse.Status, data, httpResponse.Status == OkResponseCode, errMsg, map[string]interface{}{}}, nil
}
// Get response data (payload)
func (r *APIResponse) GetData() map[string]interface{} {
return r.data
}
// Get response data (payload)
func (r *APIResponse) GetResTmp() map[string]interface{} {
return r.resObj
}
// Convert API Response to a map
func (r *APIResponse) asGoMap() map[string]interface{} {
dict := map[string]interface{}{
"res_obj": r.resObj,
"success": r.Success,
"status_code": r.StatusCode,
"data": r.data,
}
if r.ErrorMsg != "" {
dict["error_message"] = r.ErrorMsg
}
return dict
}
/*
Set the response success status
status: input status
*/
func (r *APIResponse) setSuccessStatus(status bool) {
r.Success = status
}