-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapib.go
214 lines (189 loc) · 4.95 KB
/
apib.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
package apib
import (
"bufio"
"fmt"
"log"
"net/url"
"os"
"regexp"
"strings"
"github.com/acsellers/inflections"
"github.com/labstack/echo/v4"
)
var calls = make(map[string]Resources)
var recording bool
var currentGroup string
var currentName string
var currentParams []*Param
type Call struct {
Group string
Name string
Request Request
Response Response
}
type Resources []Resource
type Resource struct {
Name string
URI string
Call Call
Params map[string][]string
ExtraParams []*Param
Attributes map[string]string
}
type Request struct {
URI string
Method string
Headers map[string]string
Params map[string][]string
Body string
PathParams map[string]string
FormParams map[string]string
}
type Response struct {
Headers map[string]string
Body string
StatusCode int
}
type Param struct {
Name string
Example string
Description string
Type string
Required bool
}
func Record() {
recording = true
}
func Store() {
spaces := regexp.MustCompile(" ")
recording = false
for group, rss := range calls {
fileName := inflections.Underscore(group)
fileName = spaces.ReplaceAllString(fileName, "_")
f, err := os.Create(fileName + ".apib")
if err != nil {
log.Fatal("Can't create file.", err)
}
f.Write([]byte(fmt.Sprintf("# Group %s\n\n", group)))
for _, rs := range rss {
c := rs.Call
f.Write([]byte(fmt.Sprintf("## %s [%s %s]\n\n", c.Name, c.Request.Method, c.Request.URI)))
if len(rs.Params) > 0 || len(rs.ExtraParams) > 0 {
f.Write([]byte(fmt.Sprintf("+ Parameters\n\n")))
if len(rs.Params) > 0 {
for k, v := range rs.Params {
for _, p := range v {
f.Write([]byte(fmt.Sprintf(" + %s: `%s` (string)\n", url.QueryEscape(k), p)))
}
}
}
if len(rs.ExtraParams) > 0 {
for _, p := range rs.ExtraParams {
required := "optional"
if p.Required {
required = "required"
}
f.Write([]byte(fmt.Sprintf(" + %s: `%s` (%s, "+required+")\n", url.QueryEscape(p.Name), p.Example, p.Type)))
if p.Description != "" {
desc := stringToLines(p.Description)
for i, s := range desc {
desc[i] = " " + s + " \n"
}
f.Write([]byte(fmt.Sprintf("%s\n", strings.Join(desc, ""))))
}
}
}
}
if len(rs.Attributes) > 0 {
f.Write([]byte(fmt.Sprintf("+ Attributes\n\n")))
for k, v := range rs.Attributes {
f.Write([]byte(fmt.Sprintf(" + %s: `%s` (string)\n", url.QueryEscape(k), v)))
}
}
f.Write([]byte("\n"))
// f.Write([]byte(fmt.Sprintf("### %s [%s]\n\n\n", c.Name, c.Request.Method)))
f.Write([]byte(fmt.Sprintf("+ Request (%s)\n\n", c.Request.Headers["Content-Type"])))
if len(c.Request.Headers) > 0 {
f.Write([]byte(fmt.Sprintf(" + Headers\n\n")))
for k, v := range c.Request.Headers {
if k == "Content-Type" {
continue
}
f.Write([]byte(fmt.Sprintf(" %s: %s\n", k, v)))
}
f.Write([]byte("\n"))
}
if len(c.Request.Body) > 0 {
f.Write([]byte(fmt.Sprintf(" + Body\n\n")))
body := stringToLines(c.Request.Body)
for i, s := range body {
body[i] = " " + s
}
f.Write([]byte(fmt.Sprintf(" \n%s\n \n\n", strings.Join(body, "\n"))))
}
f.Write([]byte("\n"))
f.Write([]byte(fmt.Sprintf("+ Response %d (%s)\n\n", c.Response.StatusCode, c.Response.Headers["Content-Type"])))
if len(c.Response.Headers) > 0 {
f.Write([]byte(fmt.Sprintf(" + Headers\n\n")))
for k, v := range c.Response.Headers {
if k == "Content-Type" {
continue
}
f.Write([]byte(fmt.Sprintf(" %s: %s\n", k, v)))
}
f.Write([]byte("\n"))
}
if len(c.Response.Body) > 0 {
f.Write([]byte(fmt.Sprintf(" + Body\n\n")))
body := stringToLines(c.Response.Body)
for i, s := range body {
body[i] = " " + s
}
f.Write([]byte(fmt.Sprintf(" \n%s\n \n\n", strings.Join(body, "\n"))))
}
f.Write([]byte("\n"))
}
f.Close()
}
}
func stringToLines(s string) []string {
var lines []string
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
return lines
}
func Group(group string) {
currentGroup = group
}
func Name(name string) {
currentName = name
}
func AddParam(name string, typ string, ex string, desc string, req bool) {
if currentParams == nil {
currentParams = []*Param{}
}
currentParams = append(currentParams, &Param{
Name: name,
Type: typ,
Example: ex,
Description: desc,
Required: req,
})
}
func Flush() {
currentGroup = ""
currentName = ""
currentParams = []*Param{}
}
func params(c echo.Context) map[string]string {
params := make(map[string]string)
for _, p := range c.ParamNames() {
params[p] = c.Param(p)
}
return params
}