Skip to content

Commit

Permalink
Merge pull request #72 from bpicolo/71_request_options_for_api_doc_re…
Browse files Browse the repository at this point in the history
…quest

Allow headers to be sent with api doc request
  • Loading branch information
Ben Picolo committed Dec 10, 2014
2 parents d4f4402 + 4abd9c4 commit 62f52cb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="swaggerpy",
version="0.7.1",
version="0.7.2",
license="BSD 3-Clause License",
description="Library for accessing Swagger-enabled API's",
long_description=open(os.path.join(os.path.dirname(__file__),
Expand Down
7 changes: 5 additions & 2 deletions swaggerpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ class SwaggerClient(object):
"""

def __init__(self, url_or_resource, http_client=None,
api_base_path=None, raise_with=None):
api_base_path=None, raise_with=None,
api_doc_request_headers=None):
if not http_client:
http_client = SynchronousHttpClient()
# Wrap http client's errors with raise_with
Expand All @@ -301,7 +302,9 @@ def __init__(self, url_or_resource, http_client=None,
# Load Swagger APIs always synchronously
loader = Loader(
SynchronousHttpClient(),
[ClientProcessor()])
[ClientProcessor()],
api_doc_request_headers=api_doc_request_headers,
)

forced_api_base_path = api_base_path is not None
# url_or_resource can be url of type str,
Expand Down
19 changes: 14 additions & 5 deletions swaggerpy/swagger_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def process_property(self, resources, resource, model, prop,
allowed_refs, None)


def json_load_url(http_client, url):
def json_load_url(http_client, url, headers):
"""Download and parse JSON from a URL.
:param http_client: HTTP client interface.
Expand All @@ -123,7 +123,7 @@ def json_load_url(http_client, url):
finally:
fp.close()
else:
resp = http_client.request(u'GET', url)
resp = http_client.request(u'GET', url, headers=headers)
resp.raise_for_status()
return resp.json()

Expand All @@ -137,8 +137,10 @@ class Loader(object):
:type processors: list of SwaggerProcessor
"""

def __init__(self, http_client, processors=None):
def __init__(self, http_client, processors=None,
api_doc_request_headers=None):
self.http_client = http_client
self.api_doc_request_headers = api_doc_request_headers
if processors is None:
processors = []
# always go through the validation processor first
Expand All @@ -162,7 +164,11 @@ def load_resource_listing(self, resources_url, base_url=None):
"""

# Load the resource listing
resource_listing = json_load_url(self.http_client, resources_url)
resource_listing = json_load_url(
self.http_client,
resources_url,
self.api_doc_request_headers,
)
self.pre_process_resource_listing(resource_listing)

# Some extra data only known about at load time
Expand Down Expand Up @@ -190,7 +196,10 @@ def load_api_declaration(self, base_url, api_dict):
path = api_dict.get(u'path').replace(u'{format}', u'json')
api_dict[u'url'] = urlparse.urljoin(base_url + u'/', path.strip(u'/'))
api_dict[u'api_declaration'] = json_load_url(
self.http_client, api_dict[u'url'])
self.http_client,
api_dict[u'url'],
self.api_doc_request_headers,
)

def pre_process_resource_listing(self, resources):
"""Apply pre-processors before loading resource listing.
Expand Down
13 changes: 13 additions & 0 deletions tests/resource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def test_setattrs_on_client_and_resource(self):
self.assertTrue(isinstance(client.api_test, Resource))
self.assertTrue(isinstance(client.api_test.testHTTP, Operation))

@httpretty.activate
def test_headers_sendable_with_api_doc_request(self):
self.register_urls()
SwaggerClient(
u'http://localhost/api-docs',
api_doc_request_headers={'foot': 'bart'},
)

self.assertEqual(
'bart',
httpretty.last_request().headers.get('foot'),
)

@httpretty.activate
def test_api_base_path_if_passed_is_always_used_as_base_path(self):
httpretty.register_uri(
Expand Down

0 comments on commit 62f52cb

Please sign in to comment.