-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOVHAPI.go
273 lines (229 loc) · 7.79 KB
/
OVHAPI.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// OVHAPI Struct to get and parse the API definitions from the OVH API.
type OVHAPI struct {
basePath string
}
// OVHAPIRoute based on the OVH API format.
type OVHAPIRoute struct {
Format []string `json:"format"`
Path string `json:"path"`
Schema string `json:"schema"`
Description string `json:"description"`
}
// OVHAPIRoutes based on the OVH API format.
type OVHAPIRoutes struct {
Apis []OVHAPIRoute `json:"apis"`
BasePath string `json:"basePath"`
}
// OVHAPIStatus based on the OVH API format.
type OVHAPIStatus struct {
Value string `json:"value"`
Description string `json:"description"`
DeprecatedDate string `json:"deprecatedDate"`
DeletionDate string `json:"deletionDate"`
Replacement string `json:"replacement"`
}
// OVHAPITypeObjectProperties based on the OVH API format.
type OVHAPITypeObjectProperties struct {
FullType string `json:"fullType"`
Type string `json:"type"`
Description string `json:"description"`
ReadOnly string `json:"readOnly"` // equals to "", "0", "1"
CanBeNull string `json:"canBeNull"` // equals to "", "0", "1"
}
// OVHAPITypeObject based on the OVH API format.
type OVHAPITypeObject struct {
ID string `json:"id"`
Namespace string `json:"namespace"`
Description string `json:"description"`
Properties map[string]OVHAPITypeObjectProperties `json:"properties"`
Generics []interface{} `json:"generics"`
}
// OVHAPITypeEnum based on the OVH API format.
type OVHAPITypeEnum struct {
ID string `json:"id"`
Namespace string `json:"namespace"`
Description string `json:"description"`
EnumType string `json:"enumType"`
Enum []string `json:"enum"`
}
// OVHAPIParameter based on the OVH API format.
type OVHAPIParameter struct {
Required interface{} `json:"required"`
Description string `json:"description"`
DataType string `json:"dataType"`
ParamType string `json:"paramType"`
Name string `json:"name"`
FullType string `json:"fullType"`
Default interface{} `json:"default"`
}
// OVHAPIEndpointOperation based on the OVH API format.
type OVHAPIEndpointOperation struct {
HTTPMethod string `json:"httpMethod"`
APIStatus OVHAPIStatus `json:"apiStatus"`
NoAuthentication interface{} `json:"noAuthentication"`
Description string `json:"description"`
ResponseType string `json:"responseType"`
Parameters []OVHAPIParameter `json:"parameters"`
}
// OVHAPIEndpoint based on the OVH API format.
type OVHAPIEndpoint struct {
Path string `json:"path"`
Description string `json:"description"`
Operations []OVHAPIEndpointOperation `json:"operations"`
}
// OVHAPIDefinition based on the OVH API format.
type OVHAPIDefinition struct {
APIVersion string `json:"apiVersion"`
BasePath string `json:"basePath"`
ResourcePath string `json:"resourcePath"`
Apis []OVHAPIEndpoint `json:"apis"`
RawModels map[string]map[string]interface{} `json:"models"`
Models map[string]interface{}
}
// NewOVHAPI Return a new OVHAPI struct.
func NewOVHAPI(basePath string) *OVHAPI {
api := &OVHAPI{}
api.basePath = basePath
return api
}
// GetRouteList Download, parse, filter and return the routes of the API.
func (api *OVHAPI) GetRouteList(filters []string) ([]string, error) {
var err error
var res *http.Response
if res, err = http.Get(api.basePath); err != nil {
return nil, err
}
defer res.Body.Close()
var body []byte
if body, err = ioutil.ReadAll(res.Body); err != nil {
return nil, err
}
var result OVHAPIRoutes
if err = json.Unmarshal(body, &result); err != nil {
return nil, err
}
routes := []string{}
for _, api := range result.Apis {
var route = api.Path
var fullRoute = fmt.Sprintf("%s.json", route)
if len(filters) == 0 {
routes = append(routes, fullRoute)
} else {
for _, filter := range filters {
if filter == route {
routes = append(routes, fullRoute)
break
}
}
}
}
return routes, err
}
// GetDefinition Download, parse and return a OVH API definition from the API.
func (api *OVHAPI) GetDefinition(route string) (*OVHAPIDefinition, error) {
var err error
var res *http.Response
if res, err = http.Get(api.basePath + route); err != nil {
return nil, err
}
defer res.Body.Close()
var body []byte
if body, err = ioutil.ReadAll(res.Body); err != nil {
return nil, err
}
var definition OVHAPIDefinition
if err = json.Unmarshal(body, &definition); err != nil {
return nil, err
}
definition.Models = map[string]interface{}{}
for modelName, modelProperties := range definition.RawModels {
isEnum := false
for modelPropertyName := range modelProperties {
if modelPropertyName == "enum" {
isEnum = true
break
}
}
if isEnum {
definition.Models[modelName] = newOVHAPITypeEnum(modelProperties)
} else {
definition.Models[modelName] = newOVHAPITypeObject(modelProperties)
}
}
definition.RawModels = nil
return &definition, err
}
// newOVHAPITypeEnum Return a new OVHAPITypeEnum struct.
func newOVHAPITypeEnum(parameter map[string]interface{}) OVHAPITypeEnum {
enum := OVHAPITypeEnum{}
enum.ID = stringFromStringOrNil(parameter["id"])
enum.EnumType = stringFromStringOrNil(parameter["enumType"])
enum.Description = stringFromStringOrNil(parameter["description"])
enum.Namespace = stringFromStringOrNil(parameter["namespace"])
enum.Enum = []string{}
for _, value := range parameter["enum"].([]interface{}) {
enum.Enum = append(enum.Enum, stringFromStringOrNil(value))
}
return enum
}
// newOVHAPITypeObject Return a new OVHAPITypeObject struct.
func newOVHAPITypeObject(parameter map[string]interface{}) OVHAPITypeObject {
object := OVHAPITypeObject{}
object.ID = stringFromStringOrNil(parameter["id"])
object.Description = stringFromStringOrNil(parameter["description"])
object.Namespace = stringFromStringOrNil(parameter["namespace"])
switch t := parameter["generics"].(type) {
case nil:
case []interface{}:
object.Generics = parameter["generics"].([]interface{})
default:
logWarn(fmt.Sprintf("unhandled generics type: %T", t))
}
//object.Generics = parameter["generics"]
object.Properties = map[string]OVHAPITypeObjectProperties{}
for name, property := range parameter["properties"].(map[string]interface{}) {
object.Properties[name] = newOVHAPITypeObjectProperties(property.(map[string]interface{}))
}
return object
}
// newOVHAPITypeObjectProperties Return a new OVHAPITypeObjectProperties struct.
func newOVHAPITypeObjectProperties(parameter map[string]interface{}) OVHAPITypeObjectProperties {
properties := OVHAPITypeObjectProperties{}
properties.FullType = stringFromStringOrNil(parameter["fullType"])
properties.Type = stringFromStringOrNil(parameter["type"])
properties.Description = stringFromStringOrNil(parameter["description"])
properties.ReadOnly = stringFromBoolOrNil(parameter["readOnly"])
properties.CanBeNull = stringFromBoolOrNil(parameter["canBeNull"])
return properties
}
// Util functions.
func stringFromStringOrNil(value interface{}) string {
if value == nil {
return ""
}
return value.(string)
}
func stringFromBoolOrNil(value interface{}) string {
var valfloat float64
if value == nil {
return ""
} else if value == true {
valfloat = 1
} else {
valfloat = 0
}
return fmt.Sprintf("%.0f", valfloat)
}
func stringFromIntOrNil(value interface{}) string {
if value == nil {
return ""
}
return fmt.Sprintf("%.0f", value.(float64))
}