Skip to content

Commit

Permalink
Merge e11bac9 into 966659e
Browse files Browse the repository at this point in the history
  • Loading branch information
Ira Miller committed Oct 19, 2015
2 parents 966659e + e11bac9 commit 6f205d1
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions bravado/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ class SwaggerClient(object):
"""A client for accessing a Swagger-documented RESTful service.
"""

def __init__(self, swagger_spec):
def __init__(self, swagger_spec, resource_decorator=None):
"""
:param swagger_spec: :class:`bravado_core.spec.Spec`
:param resource_decorator: The ResourceDecorator class to use
:type resource_decorator: ResourceDecorator
"""
self.swagger_spec = swagger_spec
self.resource_decorator = resource_decorator or ResourceDecorator

@classmethod
def from_url(cls, spec_url, http_client=None, request_headers=None,
config=None):
config=None, resource_decorator=None):
"""
Build a :class:`SwaggerClient` from a url to the Swagger
specification for a RESTful API.
Expand All @@ -91,30 +94,35 @@ def from_url(cls, spec_url, http_client=None, request_headers=None,
:type request_headers: dict
:param config: bravado_core config dict. See
bravado_core.spec.CONFIG_DEFAULTS
:param resource_decorator: The ResourceDecorator class to use
:type resource_decorator: ResourceDecorator
"""
# TODO: better way to customize the request for api calls, so we don't
# have to add new kwargs for everything
log.debug(u"Loading from %s" % spec_url)
http_client = http_client or RequestsClient()
loader = Loader(http_client, request_headers=request_headers)
spec_dict = loader.load_spec(spec_url)
return cls.from_spec(spec_dict, spec_url, http_client, config)
return cls.from_spec(spec_dict, spec_url, http_client, config,
resource_decorator)

@classmethod
def from_spec(cls, spec_dict, origin_url=None, http_client=None,
config=None):
config=None, resource_decorator=None):
"""
Build a :class:`SwaggerClient` from swagger api docs
:param spec_dict: a dict with a Swagger spec in json-like form
:param origin_url: the url used to retrieve the spec_dict
:type origin_url: str
:param config: Configuration dict - see spec.CONFIG_DEFAULTS
:param resource_decorator: The ResourceDecorator class to use
:type resource_decorator: ResourceDecorator
"""
http_client = http_client or RequestsClient()
swagger_spec = Spec.from_dict(
spec_dict, origin_url, http_client, config)
return cls(swagger_spec)
return cls(swagger_spec, resource_decorator)

def get_model(self, model_name):
return self.swagger_spec.definitions[model_name]
Expand All @@ -135,7 +143,7 @@ def __getattr__(self, item):

# Wrap bravado-core's Resource and Operation objects in order to
# execute a service call via the http_client.
return ResourceDecorator(resource)
return self.resource_decorator(resource)

def __dir__(self):
return self.swagger_spec.resources.keys()
Expand Down

0 comments on commit 6f205d1

Please sign in to comment.