-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_cache.go
147 lines (130 loc) · 4.08 KB
/
api_cache.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Cloud API
*
* The public facing API through which connectors are exposed as a single abstract API
*
* API version: v1.5
* Contact: support@trexis.net
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package finite
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// A CacheApiController binds http requests to an api service and writes the service results to the http response
type CacheApiController struct {
service CacheApiServicer
}
// NewCacheApiController creates a default api controller
func NewCacheApiController(s CacheApiServicer) Router {
return &CacheApiController{service: s}
}
// Routes returns all of the api route for the CacheApiController
func (c *CacheApiController) Routes() Routes {
return Routes{
{
"DeleteAllCache",
strings.ToUpper("Delete"),
"/cloud/v1/admin/cache",
c.DeleteAllCache,
},
{
"DeleteCache",
strings.ToUpper("Delete"),
"/cloud/v1/admin/cache/{finiteType}",
c.DeleteCache,
},
{
"GetCache",
strings.ToUpper("Get"),
"/cloud/v1/admin/cache/{finiteType}",
c.GetCache,
},
{
"ListAllCache",
strings.ToUpper("Get"),
"/cloud/v1/admin/cache",
c.ListAllCache,
},
{
"RenewCache",
strings.ToUpper("Post"),
"/cloud/v1/admin/cache/{finiteType}",
c.RenewCache,
},
}
}
// DeleteAllCache - Clear the cache for all Finite types
func (c *CacheApiController) DeleteAllCache(w http.ResponseWriter, r *http.Request) {
xTRACEID := r.Header.Get("X-TRACE-ID")
result, err := c.service.DeleteAllCache(r.Context(), xTRACEID)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// DeleteCache - Clear the cache for a given type
func (c *CacheApiController) DeleteCache(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
finiteType := FiniteType(params["finiteType"])
xTRACEID := r.Header.Get("X-TRACE-ID")
result, err := c.service.DeleteCache(r.Context(), finiteType, xTRACEID)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetCache - Get the cache statistics for a given type
func (c *CacheApiController) GetCache(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
finiteType := FiniteType(params["finiteType"])
xTRACEID := r.Header.Get("X-TRACE-ID")
result, err := c.service.GetCache(r.Context(), finiteType, xTRACEID)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// ListAllCache - List of all cache statistics
func (c *CacheApiController) ListAllCache(w http.ResponseWriter, r *http.Request) {
xTRACEID := r.Header.Get("X-TRACE-ID")
result, err := c.service.ListAllCache(r.Context(), xTRACEID)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// RenewCache - Clear and renew the cache
func (c *CacheApiController) RenewCache(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
finiteType := FiniteType(params["finiteType"])
xTRACEID := r.Header.Get("X-TRACE-ID")
cacheReference := &CacheReference{}
if err := json.NewDecoder(r.Body).Decode(&cacheReference); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.RenewCache(r.Context(), finiteType, xTRACEID, *cacheReference)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}