-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostman.go
195 lines (168 loc) · 4.66 KB
/
postman.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
package main
import (
"encoding/json"
"os"
"path"
"sort"
"strconv"
"strings"
"github.com/astaxie/beego/swagger"
"github.com/rbretecher/go-postman-collection"
)
var (
description = `# DORAEMON POSTMAN COLLECTION\n## Usage\nPut ` + "`{{DOR_BASE_URL}}`" + `as environment. For more context, refer to: https://learning.postman.com/docs/sending-requests/variables/.`
baseHeaders = []*postman.Header{
{
Key: "Accept",
Value: "application/json",
},
{
Key: "Content-Language",
Value: "{{DOR_CONTENT_LANGUAGE}}",
},
{
Key: "User-Agent",
Value: "{{DOR_USER_AGENT}}",
},
}
)
func generatePostman(curpath string) error {
fd, err := os.ReadFile(path.Join(curpath, "swagger", "swagger.json"))
if err != nil {
return err
}
var sAPIs swagger.Swagger
err = json.Unmarshal(fd, &sAPIs)
if err != nil {
return err
}
p := postman.CreateCollection(sAPIs.Infos.Title, description)
collection := make(map[string]*postman.Items)
for sURL, sItem := range sAPIs.Paths {
sURL = sAPIs.BasePath + sURL
// Postman uses `:pathVariable` instead of swagger's `{pathVariable}`
// format.
sURL = strings.ReplaceAll(sURL, "{", ":")
sURL = strings.ReplaceAll(sURL, "}", "")
if get := sItem.Get; get != nil {
c := upsertNewCollection(p, collection, get.Tags[0])
addItemToCollection(sURL, c, get, postman.Get)
}
if put := sItem.Put; put != nil {
c := upsertNewCollection(p, collection, put.Tags[0])
addItemToCollection(sURL, c, put, postman.Put)
}
if post := sItem.Post; post != nil {
c := upsertNewCollection(p, collection, post.Tags[0])
addItemToCollection(sURL, c, post, postman.Post)
}
if del := sItem.Delete; del != nil {
c := upsertNewCollection(p, collection, del.Tags[0])
addItemToCollection(sURL, c, del, postman.Delete)
}
if options := sItem.Options; options != nil {
c := upsertNewCollection(p, collection, options.Tags[0])
addItemToCollection(sURL, c, options, postman.Options)
}
if head := sItem.Head; head != nil {
c := upsertNewCollection(p, collection, head.Tags[0])
addItemToCollection(sURL, c, head, postman.Head)
}
if patch := sItem.Patch; patch != nil {
c := upsertNewCollection(p, collection, patch.Tags[0])
addItemToCollection(sURL, c, patch, postman.Patch)
}
}
sort.Slice(p.Items, func(i, j int) bool {
if strings.EqualFold(p.Items[i].Name, "customers") {
return true
}
return p.Items[i].Name < p.Items[j].Name
})
if _, err = json.Marshal(p); err != nil {
return err
}
pd, err := os.Create(path.Join(curpath, "swagger", "postman-collection.json"))
if err != nil {
return err
}
defer pd.Close()
err = p.Write(pd, postman.V210)
if err != nil {
return err
}
return nil
}
func upsertNewCollection(p *postman.Collection, collection map[string]*postman.Items, s string) *postman.Items {
if c, ok := collection[s]; ok {
return c
}
collection[s] = p.AddItemGroup(s)
return collection[s]
}
func addItemToCollection(url string, collection *postman.Items, op *swagger.Operation, method postman.Method) {
var headers []*postman.Header
var variables []*postman.Variable
var queryParams []*postman.QueryParam
var body *postman.Body
var formData []*postman.Variable
for _, param := range op.Parameters {
switch param.In {
case "path":
variables = append(variables, &postman.Variable{
ID: param.Name,
Type: param.Type,
Name: param.Name,
Description: param.Description,
})
case "formData":
formData = append(formData, &postman.Variable{
Key: param.Name,
Type: param.Type,
Description: param.Description,
})
case "query":
description := param.Description
queryParams = append(queryParams, &postman.QueryParam{
Key: param.Name,
Description: &description,
})
}
}
if len(formData) > 0 {
body = &postman.Body{
Mode: "formdata",
}
body.FormData = formData
headers = append(headers, &postman.Header{
Key: "Content-Type",
Value: "multipart/form-data",
})
}
var responses []*postman.Response
for status, response := range op.Responses {
s, _ := strconv.Atoi(status)
responses = append(responses, &postman.Response{
Status: status,
Code: s,
Name: response.Description,
})
}
collection.AddItem(postman.CreateItem(postman.Item{
Name: string(method) + " " + url,
Description: op.Description,
ID: op.OperationID,
Request: &postman.Request{
URL: &postman.URL{
Host: []string{"{{DOR_BASE_URL}}"},
Path: strings.Split(url, "/"),
Query: queryParams,
Variables: variables,
},
Method: method,
Header: append(baseHeaders, headers...),
Body: body,
},
Responses: responses,
}))
}