Allow deserialized discovery docs to be passed to build_from_document().
Reviewed in https://codereview.appspot.com/6906052/.
diff --git a/apiclient/discovery.py b/apiclient/discovery.py
index eb179ff..1fc33ca 100644
--- a/apiclient/discovery.py
+++ b/apiclient/discovery.py
@@ -225,7 +225,9 @@
document that is it given, as opposed to retrieving one over HTTP.
Args:
- service: string, discovery document.
+ service: string or object, the JSON discovery document describing the API.
+ The value passed in may either be the JSON string or the deserialized
+ JSON.
base: string, base URI for all HTTP requests, usually the discovery URI.
This parameter is no longer used as rootUrl and servicePath are included
within the discovery document. (deprecated)
@@ -245,7 +247,8 @@
# future is no longer used.
future = {}
- service = simplejson.loads(service)
+ if isinstance(service, basestring):
+ service = simplejson.loads(service)
base = urlparse.urljoin(service['rootUrl'], service['servicePath'])
schema = Schemas(service)
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index b2ee56c..2c28cd2 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -125,6 +125,14 @@
discovery = file(datafile('plus.json')).read()
plus = build_from_document(discovery, base="https://www.googleapis.com/")
self.assertTrue(plus is not None)
+ self.assertTrue(hasattr(plus, 'activities'))
+
+ def test_can_build_from_local_deserialized_document(self):
+ discovery = file(datafile('plus.json')).read()
+ discovery = simplejson.loads(discovery)
+ plus = build_from_document(discovery, base="https://www.googleapis.com/")
+ self.assertTrue(plus is not None)
+ self.assertTrue(hasattr(plus, 'activities'))
def test_building_with_base_remembers_base(self):
discovery = file(datafile('plus.json')).read()