Skip to content

Commit

Permalink
Add header support
Browse files Browse the repository at this point in the history
Add support for adding extra headers with '-H' or '--header'.
The flag can be specified multiple times to add multiple headers
  • Loading branch information
toimela committed Dec 19, 2019
1 parent 1ca501f commit e550de3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Options:
-o, --output PATH Directory to store schema files
-p, --prefix TEXT Prefix for JSON references (only for OpenAPI versions
before 3.0)
-H, --header TEXT Extra header to use when getting a schema. May be
specified multiple times.
--stand-alone Whether or not to de-reference JSON schemas
--kubernetes Enable Kubernetes specific processors
--strict Prohibits properties not in the schema
Expand Down
15 changes: 13 additions & 2 deletions openapi2jsonschema/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
allow_null_optional_fields,
change_dict_values,
append_no_duplicates,
parse_headers,
)
from openapi2jsonschema.errors import UnsupportedError

Expand All @@ -34,6 +35,12 @@
default="_definitions.json",
help="Prefix for JSON references (only for OpenAPI versions before 3.0)",
)
@click.option(
"-H",
"--header",
multiple=True,
help="Extra header to use when getting a schema. May be specified multiple times.",
)
@click.option(
"--stand-alone", is_flag=True, help="Whether or not to de-reference JSON schemas"
)
Expand All @@ -49,7 +56,7 @@
help="Prohibits properties not in the schema (additionalProperties: false)",
)
@click.argument("schema", metavar="SCHEMA_URL")
def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
def default(output, schema, prefix, header, stand_alone, expanded, kubernetes, strict):
"""
Converts a valid OpenAPI specification into a set of JSON Schema files
"""
Expand All @@ -59,7 +66,11 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict):
else:
if os.path.isfile(schema):
schema = "file://" + os.path.realpath(schema)
req = urllib.request.Request(schema)
if header:
headers = parse_headers(header)
req = urllib.request.Request(schema, headers=headers)
else:
req = urllib.request.Request(schema)
response = urllib.request.urlopen(req)

info("Parsing schema")
Expand Down
15 changes: 15 additions & 0 deletions openapi2jsonschema/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,18 @@ def append_no_duplicates(obj, key, value):
obj[key] = []
if value not in obj[key]:
obj[key].append(value)


def parse_headers(header):
"""
Argument is a tuple of header strings.
e.g. ('Content-Type: application/json', 'Accept: application/text')
Invalid headers are ignored.
The function returns a dictionary of the headers.
"""
res = {}
for h in header:
h = h.split(":", 1)
if len(h) == 2:
res[h[0].strip()] = h[1].strip()
return res

0 comments on commit e550de3

Please sign in to comment.