-
Notifications
You must be signed in to change notification settings - Fork 10
/
postman.go
153 lines (133 loc) · 3.84 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
package postman
import (
"crypto/sha256"
"encoding/json"
"fmt"
"net/http"
"github.com/luraproject/lura/v2/config"
)
const (
namespace = "documentation/postman"
defaultDescription = "Collection parsed from KrakenD config"
postmanJsonSchema = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
)
// HandleCollection returns a simple http.HandleFunc exposing the POSTMAN collection description
func HandleCollection(c Collection) func(http.ResponseWriter, *http.Request) {
b, err := json.Marshal(c)
if err != nil {
return func(rw http.ResponseWriter, _ *http.Request) {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
return func(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Add("Content-Type", "application/json")
rw.Write(b)
}
}
// Parse converts the received service config into a simple POSTMAN collection description
// @see https://schema.postman.com/collection/json/v2.1.0/draft-07/docs/index.html
func Parse(cfg config.ServiceConfig) (Collection, error) {
serviceOpts, err := parseServiceOptions(&cfg)
if err != nil {
return Collection{}, err
}
c := Collection{
Info: Info{
Name: serviceOpts.Name,
PostmanID: hash(serviceOpts.Name),
Description: serviceOpts.Description,
Schema: postmanJsonSchema,
},
Item: ItemList{},
Variables: parseVariables(&cfg),
}
v, err := parseVersion(serviceOpts)
if err == errInvalidSemver {
return Collection{}, err
}
c.Info.Version = v
for _, e := range cfg.Endpoints {
entry := newItem(e.Endpoint)
entry.Request = &Request{
URL: URL{
Raw: "{{SCHEMA}}://{{HOST}}" + e.Endpoint,
Protocol: "{{SCHEMA}}",
Host: []string{"{{HOST}}"},
Path: []string{e.Endpoint[1:]},
},
Method: e.Method,
}
// The endpoints that do not have options are added to the root of the collection
// This simple check handles the backwards compatibility of the generator
opts, err := parseEndpointOptions(e)
if err == errMissingEndpointConfig {
c.Item = append(c.Item, entry)
continue
} else if err != nil {
return Collection{}, err
}
if opts.Name != "" {
entry.Name = opts.Name
}
if opts.Description != "" {
entry.Request.Description = opts.Description
}
if opts.Folder != "" && opts.Folder != separator {
folder := createFolder(&c.Item, opts.Folder, findFolderOptions(serviceOpts, opts.Folder))
if folder != nil {
folder.Item = append(folder.Item, entry)
continue
}
}
c.Item = append(c.Item, entry)
}
return c, nil
}
func hash(input string) string {
return fmt.Sprintf("%x", sha256.Sum224([]byte(input)))
}
type Collection struct {
Variables []Variable `json:"variables"`
Info Info `json:"info"`
Item ItemList `json:"item"`
}
type Info struct {
Name string `json:"name"`
PostmanID string `json:"_postman_id"`
Description string `json:"description,omitempty"`
Schema string `json:"schema"`
Version *Version `json:"version,omitempty"`
}
type Request struct {
URL URL `json:"url"`
Method string `json:"method"`
Header []Header `json:"header,omitempty"`
Body *Body `json:"body,omitempty"`
Description string `json:"description,omitempty"`
}
type URL struct {
Raw string `json:"raw"`
Protocol string `json:"protocol"`
Host []string `json:"host"`
Path []string `json:"path"`
}
type Body struct {
Mode string `json:"mode,omitempty"`
Raw string `json:"raw,omitempty"`
}
type Header []struct {
Key string `json:"key"`
Value string `json:"value"`
Description string `json:"description,omitempty"`
}
type Version struct {
Major uint64 `json:"major"`
Minor uint64 `json:"minor"`
Patch uint64 `json:"patch"`
}
type Variable struct {
ID string `json:"id"`
Key string `json:"key"`
Value string `json:"value"`
Type string `json:"type"`
}