-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostmanize-ga.jq
More file actions
135 lines (127 loc) · 4 KB
/
postmanize-ga.jq
File metadata and controls
135 lines (127 loc) · 4 KB
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
include "pretty";
def desc_param:
"(" +
.type +
if .type == "array" then "[\(.items.type)]" else "" end +
if .required == true then ", required" else ", optional" end +
") " + .description;
def conv_url:
(.path | gsub("\\{"; ":") | gsub("}"; "")) as $path | {
raw: ("https://{{dnac}}:{{port}}" + $path),
protocol: "https",
host: "{{dnac}}",
port: "{{port}}",
path: $path | split("/")[1:],
variable: [.parameters[] | select(.in == "path") | {
key: .name,
value: .default,
description: desc_param
}],
query: [.parameters[] | select(.in == "query") | {
key: .name,
value: .default,
description: desc_param
}]
};
def conv_header:
if .parameters | any(.in == "header" and .name == "Authorization") then [] else [{
key: "X-Auth-Token",
value: "{{token}}",
description: "(string, required) Authorization token"
}] end +
[.parameters[] | select(.in == "header") | {
key: .name,
value: .default,
description: desc_param
}];
def conv_attr:
if .type == "boolean" then false
elif .type == "integer" then 0
elif .type == "number" then 0.1
elif .type == "string" then if .enum then .enum | join(" | ") else "string" end
elif .type == "object" then (.properties // {}) | map_values(conv_attr)
elif .type == "array" then [.items | conv_attr]
else
error("Unknown attribute type: " + .type)
end;
def desc_attr($name; $prefix):
if .type == "object" then
$prefix + $name + "\n" + (.properties // {} | to_entries | map(.key as $key | .value | desc_attr($key; " " + $prefix)) | add)
elif .type == "array" then
if .items.type == "object" or .items.type == "array" then
.items | desc_attr($name + ": array"; $prefix)
else
$prefix + $name + ": array[" + .items.type + "]\n"
end
else
$prefix + $name + ": " + .type + "\n"
end;
def find_req:
if .parameters | any(.in == "body") then
.parameters[] | select(.in == "body").schema."$ref" | ltrimstr("#/definitions/")
else
null
end;
def find_resp:
.responses."200".schema."$ref" | ltrimstr("#/definitions/");
.definitions as $defs | {
info: {
name: "DNA-C Platform Intent API v1.2.6 GA",
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
item: .paths | to_entries | map(.key as $path | .value | to_entries | map(.key as $method | .value | .path = $path | .method = $method)) | flatten | group_by(.tags) | map({
name: .[0].tags[0],
item: map(find_req as $req | find_resp as $resp | {
name: .summary,
request: {
method: .method | ascii_upcase,
url: conv_url,
header: conv_header,
body: (if $req then {
mode: "raw",
raw: $defs[$req] | conv_attr | tostring | pretty
} else null end),
description: (.description +
(if $req then "\n\n###### Request Model\n" + ($defs[$req] | desc_attr($req; "- ")) else "" end) +
(if $resp then "\n\n###### Response Model\n" + ($defs[$resp] | desc_attr($resp; "- ")) else "" end))
},
response: (if $resp then [{
name: "Example Response",
body: $defs[$resp] | conv_attr | tojson | pretty
}] else null end)
}) | sort_by(.request.url.raw, .request.method) | map(if .name != "Authentication API" then . else .event = [{
listen: "test",
script: {
type: "text/javascript",
exec: [
"var data = JSON.parse(responseBody);",
"postman.setEnvironmentVariable(\"token\", data.Token);"
]
}
}] | .request.auth = {
type: "basic",
basic: [
{
key: "username",
value: "{{username}}",
type: "string"
},
{
key: "password",
value: "{{password}}",
type: "string"
},
{
key: "saveHelperData",
value: true,
type: "boolean"
},
{
key: "showPassword",
value: false,
type: "boolean"
}
]
} end)
})
}