-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostmanize-eft.jq
More file actions
108 lines (100 loc) · 3.42 KB
/
postmanize-eft.jq
File metadata and controls
108 lines (100 loc) · 3.42 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
include "pretty";
def desc_param:
"(" + .dataType + if .required == true then ", required" else ", optional" end + ") " + .description;
def conv_url:
(.basePath + .path | gsub("\\$\\{"; ":") | gsub("}"; "")) as $path | {
raw: ("https://{{dnac}}:{{port}}" + $path),
protocol: "https",
host: "{{dnac}}",
port: "{{port}}",
path: $path | split("/")[1:],
variable: .pathParams | map({
key: .name,
value: .defaultValue,
description: desc_param
}),
query: .queryParams | map({
key: .name,
value: .defaultValue,
description: desc_param
})
};
def conv_header:
[{
key: "X-Auth-Token",
value: "{{token}}",
description: "(string, required) Authorization token"
}] + if .requestSchema.definitions | length == 0 then [] else [{
key: "Content-Type",
value: "application/json",
description: "(string, required) Data format"
}] end + (.headers | map({
key: .name,
value: .value,
description: desc_param
}));
def conv_attr($ctx; $type):
if $type == "any" then "any"
elif $type == "boolean" then false
elif $type == "integer" then 0
elif $type == "number" then 0.1
elif $type == "string" then "string"
elif $type == "map" then
$ctx[.address] | map({(.name): conv_attr($ctx; .type)}) | add // {}
elif $type == "array" then
[conv_attr($ctx; .arrayType)]
else
error("Unknown attribute type: " + $type)
end;
def conv_body:
. as $ctx | .root | conv_attr($ctx; .type);
def desc_attr($ctx; $prefix):
$prefix + .name + ": (" +
if .enum | length > 0 then
"enum" + (.enum | tostring)
else
.type + if .type == "array" then "[" + .arrayType + "]" else "" end
end +
if .required or prefix == "" then ", required" else ", optional" end +
")" +
if [.type] | inside(["any", "boolean", "integer", "number", "string"]) then
if .displayText | length > 0 then " " + .displayText else "" end +
if .default | length > 0 then ", " + .default else "" end
elif ([.type] | inside(["map", "array"])) and .address then
$ctx[.address] | map(desc_attr($ctx; " " + $prefix)) | join("\n") | if length > 0 then "\n" + . else "" end
else
""
end;
def desc_body:
. as $ctx | .root | desc_attr($ctx; "- ");
{
info: {
name: "DNA-C Platform Intent API v1.2 EFT",
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
item: group_by(.domain) | map({
name: .[0].domain,
item: group_by(.subDomain) | map({
name: .[0].subDomain | (if length == 0 then "General" else . end),
item: map({
name: .name,
request: {
method: .payload.method,
url: .payload | conv_url,
header: .payload | conv_header,
body: .payload.requestSchema.definitions | (if .root == null then null else {
mode: "raw",
raw: conv_body | tojson | pretty
} end),
description: (.description +
(.payload.requestSchema.definitions | if .root == null then "" else "\n\n###### Request Model\n" + desc_body end) +
(.payload.responseSchema.definitions | if .root == null then "" else "\n\n###### Response Model\n" + desc_body end))
},
response: .payload.responseSchema.definitions | (if .root == null then null else [{
name: "Example Response",
body: conv_body | tojson | pretty
}] end)
}) | sort_by(.request.url.raw, .request.method)
})
})
}