This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
apis.go
121 lines (117 loc) · 3.55 KB
/
apis.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
package main
import (
"bytes"
"strings"
"strconv"
)
func BuildAPIs(apiBase string, s Swagger) {
for _, api := range s.APIs {
curPath := api.Path
for _, operation := range api.Operations {
BuildClientFunc(apiBase, curPath, operation)
}
}
}
func BuildClientFunc(apiBase string, curPath string, o Operation) {
var returnString string
p := make(chan string)
replacedURL := curPath
var hasOptions bool = false
var hasErrorResponses bool = false
optionBlock := bytes.NewBufferString("")
pathArgs := bytes.NewBufferString("")
errorBlock := bytes.NewBufferString("")
if o.ResponseClass != "void" {
returnString = join("(*", convertType(o.ResponseClass), ", error)")
} else {
returnString = "error"
}
go func(p chan string) {
for s := range p {
clientAPIBuf.WriteString(s)
}
}(p)
for _, param := range o.Parameters {
if !(param.Required || param.ParamType == "path") {
hasOptions = true
}
}
funcName := join(Canonicalize(apiBase), Canonicalize(o.Nickname))
funcArgs := bytes.NewBufferString("")
setArgs := bytes.NewBufferString("")
if hasOptions {
optionBlock.WriteString("for index, value := range options {\n")
optionBlock.WriteString("switch index {\n")
}
var argCount int = 0
for _, param := range o.Parameters {
if param.Required || param.ParamType == "path" {
pName := Canonicalize(param.Name)
if pName == "Variable" {
pName = "Var"
}
funcArgs.WriteString(join(pName, " ", convertType(param.DataType), ","))
if param.ParamType == "query" || param.ParamType == "body" {
setArgs.WriteString(join("paramMap[\"", param.Name, "\"] = ", pName, "\n"))
} else if param.ParamType == "path" {
replacedURL = strings.Replace(replacedURL, join("{", param.Name, "}"), "%s", 1)
pathArgs.WriteString(join(", ", pName))
}
} else {
optionBlock.WriteString(join("case ", strconv.Itoa(argCount),":\n"))
optionBlock.WriteString("if len(value) > 0 {\n")
optionBlock.WriteString(join("paramMap[\"", param.Name, "\"] = value\n"))
optionBlock.WriteString("}\n")
argCount++
}
}
pathArgs.WriteString(")")
if hasOptions {
optionBlock.WriteString("}\n}\n")
funcArgs.WriteString(" options ...string")
} else {
a := funcArgs.String()
funcArgs.Reset()
funcArgs.WriteString(strings.TrimRight(a, ","))
}
if o.ErrorResponses != nil {
hasErrorResponses = true
errorBlock.WriteString("switch result.StatusCode {\n")
}
for _, errorResponse := range o.ErrorResponses {
errorBlock.WriteString(join ("case ", strconv.Itoa(errorResponse.Code), ":\n"))
errorBlock.WriteString(join("err = errors.New(\"", errorResponse.Reason, "\")\n"))
}
if hasErrorResponses {
errorBlock.WriteString("default:\n")
errorBlock.WriteString("err = nil\n")
errorBlock.WriteString("}\n")
}
setURL := join("url := fmt.Sprintf(\"", replacedURL, "\"", pathArgs.String(), "\n")
p <- join("func ", "(a *AppInstance) ", funcName, "(", funcArgs.String(),") ", returnString,"{\n")
p <- "var err error\n"
p <- join("paramMap := make(map[string]string)\n")
p <- setArgs.String()
p <- setURL
p <- optionBlock.String()
p <- "body := buildJSON(paramMap)\n"
p <- join("result := a.processCommand(url, body, \"", o.HTTPMethod, "\")\n")
p <- join("")
if hasErrorResponses {
p <- errorBlock.String()
} else {
p <- "err = nil\n"
}
if o.ResponseClass != "void" {
p <- join("var r *", convertType(o.ResponseClass), "\n")
p <- "json.Unmarshal([]byte(result.ResponseBody), r)\n"
p <- "return r, err\n"
} else {
p <- "return err\n"
}
p <- "}\n\n\n"
p <- "\n"
}
func join(joinstrings ...string) string {
return strings.Join(joinstrings, "")
}