This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
353 lines (318 loc) · 9.71 KB
/
api.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package model
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/kr/text"
"github.com/taskcluster/taskcluster-client-java/codegenerator/utils"
)
//////////////////////////////////////////////////////////////////
//
// From: http://schemas.taskcluster.net/base/v1/api-reference.json
//
//////////////////////////////////////////////////////////////////
// API represents the HTTP interface of a Taskcluster service
type API struct {
*APIReferenceFile
apiDef *APIDefinition
}
func (api *API) String() string {
var result string = fmt.Sprintf(
"Version = '%v'\n"+
"Schema = '%v'\n"+
"Title = '%v'\n"+
"Description = '%v'\n"+
"Base URL = '%v'\n",
api.Version, api.Schema, api.Title, api.Description, api.BaseURL,
)
for i, entry := range api.Entries {
result += fmt.Sprintf("Entry %-6v=\n%v", i, entry.String())
}
return result
}
func (api *API) postPopulate(apiDef *APIDefinition) {
// make sure each entry defined for this API has a unique generated method name
methods := make(map[string]bool)
for i := range api.Entries {
api.Entries[i].Parent = api
api.Entries[i].MethodName = utils.NormaliseLower(api.Entries[i].Name, methods)
api.Entries[i].postPopulate(apiDef)
}
}
func (api *API) generateAPICode(apiName string) string {
className := strings.Title(apiName)
packageName := strings.ToLower(apiName)
content := `package org.mozilla.taskcluster.client.` + packageName + `;
import org.mozilla.taskcluster.client.APICallFailure;
import org.mozilla.taskcluster.client.CallSummary;
import org.mozilla.taskcluster.client.Credentials;
import org.mozilla.taskcluster.client.EmptyPayload;
import org.mozilla.taskcluster.client.TaskclusterRequestHandler;
`
comment := "/**\n"
if api.Description != "" {
comment += utils.Indent(api.Description, " * ", true)
}
if len(comment) >= 1 && comment[len(comment)-1:] != "\n" {
comment += "\n"
}
comment += " *\n"
comment += fmt.Sprintf(" * @see \"[%v API Documentation](%v)\"\n", className, api.apiDef.DocRoot)
comment += " */\n"
content += comment
content += "public class " + className + ` extends TaskclusterRequestHandler {
protected static final String defaultBaseURL = "` + api.BaseURL + `";
public ` + className + `(Credentials credentials) {
super(credentials, defaultBaseURL);
}
public ` + className + `(Credentials credentials, String baseURL) {
super(credentials, baseURL);
}
public ` + className + `(String clientId, String accessToken) {
super(new Credentials(clientId, accessToken), defaultBaseURL);
}
public ` + className + `(String clientId, String accessToken, String certificate) {
super(new Credentials(clientId, accessToken, certificate), defaultBaseURL);
}
public ` + className + `(String baseURL) {
super(baseURL);
}
public ` + className + `() {
super(defaultBaseURL);
}
`
for _, entry := range api.Entries {
content += entry.generateAPICode(apiName)
}
content += "}"
return content
}
func (api *API) setAPIDefinition(apiDef *APIDefinition) {
api.apiDef = apiDef
}
type APIEntry struct {
*Entry
MethodName string
Parent *API
}
// Add entry.Input and entry.Output to schemaURLs, if they are set
func (entry *APIEntry) postPopulate(apiDef *APIDefinition) {
for _, v := range []string{
entry.Input,
entry.Output,
} {
if x := &entry.Parent.apiDef.schemaURLs; v != "" {
*x = append(*x, v)
}
}
}
func (entry *APIEntry) String() string {
return fmt.Sprintf(
" Entry Type = '%v'\n"+
" Entry Method = '%v'\n"+
" Entry Route = '%v'\n"+
" Entry Args = '%v'\n"+
" Entry Query = '%v'\n"+
" Entry Name = '%v'\n"+
" Entry Stability = '%v'\n"+
" Entry Scopes = '%v'\n"+
" Entry Input = '%v'\n"+
" Entry Output = '%v'\n"+
" Entry Title = '%v'\n"+
" Entry Description = '%v'\n",
entry.Type, entry.Method, entry.Route, entry.Args,
entry.Query, entry.Name, entry.Stability, &entry.Scopes,
entry.Input, entry.Output, entry.Title, entry.Description,
)
}
func (entry *APIEntry) generateAPICode(apiName string) string {
comment := "\n /**\n"
if entry.Description != "" {
comment += utils.Indent(entry.Description, " * ", true)
}
if len(comment) >= 1 && comment[len(comment)-1:] != "\n" {
comment += "\n"
}
comment += requiredScopesComment(&entry.Scopes)
comment += " *\n"
comment += fmt.Sprintf(" * @see \"[%v API Documentation](%v#%v)\"\n", entry.Title, entry.Parent.apiDef.DocRoot, entry.Name)
comment += " */\n"
inputParams := ""
if len(entry.Args) > 0 {
inputParams += "String " + strings.Join(entry.Args, ", String ")
}
apiArgsPayload := "null"
if entry.Input != "" {
apiArgsPayload = "payload"
p := entry.Parent.apiDef.schemas.SubSchema(entry.Input).TypeName + " payload"
if inputParams == "" {
inputParams = p
} else {
inputParams += ", " + p
}
}
requestType := "EmptyPayload"
if entry.Input != "" {
requestType = entry.Parent.apiDef.schemas.SubSchema(entry.Input).TypeName
}
responseType := "EmptyPayload"
if entry.Output != "" {
responseType = entry.Parent.apiDef.schemas.SubSchema(entry.Output).TypeName
}
returnType := "CallSummary<" + requestType + ", " + responseType + ">"
content := comment
content += " public " + returnType + " " + entry.MethodName + "(" + inputParams + ") throws APICallFailure {\n"
content += " return apiCall(" + apiArgsPayload + ", \"" + strings.ToUpper(entry.Method) + "\", \"" + strings.Replace(strings.Replace(entry.Route, "<", "\" + uriEncode(", -1), ">", ") + \"", -1) + "\", " + responseType + ".class);\n"
content += " }\n"
// can remove any code that added an empty string to another string
return strings.Replace(content, ` + ""`, "", -1)
}
func requiredScopesComment(scopes *ScopeExpressionTemplate) string {
if scopes.Type == "" {
return ""
}
comment := "\n"
comment += "Required scopes:\n"
comment += text.Indent(scopes.String()+"\n", " ")
return text.Indent(comment, " * ")
}
func (scopes *ScopeExpressionTemplate) String() string {
switch scopes.Type {
case "":
return ""
case "AllOf":
return scopes.AllOf.String()
case "AnyOf":
return scopes.AnyOf.String()
case "ForEachIn":
return scopes.ForEachIn.String()
case "IfThen":
return scopes.IfThen.String()
case "RequiredScope":
return scopes.RequiredScope.String()
default:
panic(fmt.Sprintf("Internal error - did not recognise scope form '%v'", scopes.Type))
}
}
func (allOf *AllOf) String() string {
if allOf == nil {
return "WARNING: NIL AllOf"
}
switch len(allOf.AllOf) {
case 0:
return ""
case 1:
return allOf.AllOf[0].String()
}
var desc string
for _, exp := range allOf.AllOf {
x := text.Indent(exp.String(), " ")
if len(x) >= 2 {
desc += "\n" + "* " + x[2:]
}
}
return "All of:" + desc
}
func (anyOf *AnyOf) String() string {
if len(anyOf.AnyOf) == 0 {
return "AnyOf empty set - INVALID"
}
if len(anyOf.AnyOf) == 1 {
return anyOf.AnyOf[0].String()
}
var desc string
for _, exp := range anyOf.AnyOf {
x := text.Indent(exp.String(), " ")
if len(x) >= 2 {
desc += "\n" + "- " + x[2:]
}
}
return "Any of:" + desc
}
func (forEachIn *ForEachIn) String() string {
return "For " + forEachIn.For + " in " + forEachIn.In + " each " + forEachIn.Each
}
func (ifThen *IfThen) String() string {
return "If " + ifThen.If + ":\n" + text.Indent(ifThen.Then.String(), " ")
}
func (rs *RequiredScope) String() string {
return string(*rs)
}
// MarshalJSON calls json.RawMessage method of the same name. Required since
// ScopeExpressionTemplate is of type json.RawMessage...
func (this *ScopeExpressionTemplate) MarshalJSON() ([]byte, error) {
return (this.RawMessage).MarshalJSON()
}
// UnmarshalJSON identifies the data structure at runtime, and unmarshals in
// the appropriate type
func (this *ScopeExpressionTemplate) UnmarshalJSON(data []byte) error {
if this == nil {
return errors.New("ScopeExpressionTemplate: UnmarshalJSON on nil pointer")
}
this.RawMessage = append((this.RawMessage)[0:0], data...)
var tempObj interface{}
err := json.Unmarshal(this.RawMessage, &tempObj)
if err != nil {
panic("Internal error: " + err.Error())
}
switch t := tempObj.(type) {
case string:
this.Type = "RequiredScope"
this.RequiredScope = new(RequiredScope)
*(this.RequiredScope) = RequiredScope(t)
case map[string]interface{}:
j, err := json.Marshal(t)
if err != nil {
panic("Internal error: " + err.Error())
}
if _, exists := t["AnyOf"]; exists {
this.Type = "AnyOf"
this.AnyOf = new(AnyOf)
err = json.Unmarshal(j, this.AnyOf)
}
if _, exists := t["AllOf"]; exists {
this.Type = "AllOf"
this.AllOf = new(AllOf)
err = json.Unmarshal(j, this.AllOf)
}
if _, exists := t["if"]; exists {
this.Type = "IfThen"
this.IfThen = new(IfThen)
err = json.Unmarshal(j, this.IfThen)
}
if _, exists := t["for"]; exists {
this.Type = "ForEachIn"
this.ForEachIn = new(ForEachIn)
err = json.Unmarshal(j, this.ForEachIn)
}
if err != nil {
panic("Internal error: " + err.Error())
}
// for old style scopesets [][]string (normal disjunctive form)
case []interface{}:
this.Type = "AnyOf"
this.AnyOf = &AnyOf{
AnyOf: make([]ScopeExpressionTemplate, len(t), len(t)),
}
for i, j := range t {
allOf := j.([]interface{})
this.AnyOf.AnyOf[i] = ScopeExpressionTemplate{
Type: "AllOf",
AllOf: &AllOf{
AllOf: make([]ScopeExpressionTemplate, len(allOf), len(allOf)),
},
}
for k, l := range allOf {
rs := RequiredScope(l.(string))
this.AnyOf.AnyOf[i].AllOf.AllOf[k] = ScopeExpressionTemplate{
Type: "RequiredScope",
RequiredScope: &rs,
}
}
}
default:
panic(fmt.Sprintf("Internal error: unrecognised type %T", t))
}
return nil
}