Skip to content

Commit 0532799

Browse files
committed
Add header support
Add support for adding extra headers with '-H' or '--header'. The flag can be specified multiple times to add multiple headers
1 parent 1ca501f commit 0532799

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Options:
4949
-o, --output PATH Directory to store schema files
5050
-p, --prefix TEXT Prefix for JSON references (only for OpenAPI versions
5151
before 3.0)
52+
-H, --header TEXT Extra header to use when getting a schema. May be
53+
specified multiple times.
5254
--stand-alone Whether or not to de-reference JSON schemas
5355
--kubernetes Enable Kubernetes specific processors
5456
--strict Prohibits properties not in the schema
@@ -101,6 +103,7 @@ do
101103
openapi2jsonschema -o "${version}-standalone" --stand-alone "${schema}"
102104
openapi2jsonschema -o "${version}-local" "${schema}"
103105
openapi2jsonschema -o "${version}"" --prefix "${prefix}" "${schema}"
106+
openapi2jsonschema -o "${version}" -H "Authorization: Bearer ${token}" "${schema}"
104107
done
105108
```
106109

openapi2jsonschema/command.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
allow_null_optional_fields,
1717
change_dict_values,
1818
append_no_duplicates,
19+
parse_headers
1920
)
2021
from openapi2jsonschema.errors import UnsupportedError
2122

@@ -34,6 +35,12 @@
3435
default="_definitions.json",
3536
help="Prefix for JSON references (only for OpenAPI versions before 3.0)",
3637
)
38+
@click.option(
39+
"-H",
40+
"--header",
41+
multiple=True,
42+
help="Extra header to use when getting a schema. May be specified multiple times.",
43+
)
3744
@click.option(
3845
"--stand-alone", is_flag=True, help="Whether or not to de-reference JSON schemas"
3946
)
@@ -49,7 +56,7 @@
4956
help="Prohibits properties not in the schema (additionalProperties: false)",
5057
)
5158
@click.argument("schema", metavar="SCHEMA_URL")
52-
def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
59+
def default(output, schema, prefix, header, stand_alone, expanded, kubernetes, strict):
5360
"""
5461
Converts a valid OpenAPI specification into a set of JSON Schema files
5562
"""
@@ -59,7 +66,11 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
5966
else:
6067
if os.path.isfile(schema):
6168
schema = "file://" + os.path.realpath(schema)
62-
req = urllib.request.Request(schema)
69+
if header:
70+
headers = parse_headers(header)
71+
req = urllib.request.Request(schema, headers=headers)
72+
else:
73+
req = urllib.request.Request(schema)
6374
response = urllib.request.urlopen(req)
6475

6576
info("Parsing schema")

openapi2jsonschema/util.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,18 @@ def append_no_duplicates(obj, key, value):
108108
obj[key] = []
109109
if value not in obj[key]:
110110
obj[key].append(value)
111+
112+
113+
def parse_headers(header):
114+
"""
115+
Argument is a tuple of header strings.
116+
e.g. ('Content-Type: application/json', 'Accept: application/text')
117+
Invalid headers are ignored.
118+
The function returns a dictionary of the headers.
119+
"""
120+
res = {}
121+
for h in header:
122+
h = h.split(":", 1)
123+
if len(h) == 2:
124+
res[h[0].strip()] = h[1].strip()
125+
return res

0 commit comments

Comments
 (0)