Repository: boto/botocore
Pull request #554 (2015-06-09 17:59:23)

Add support for api_version in create_client
Fixes https://github.com/boto/botocore/issues/553

cc @kyleknap @mtdowling


Patch:
diff --git a/botocore/client.py b/botocore/client.py
index eac1e54cd..c76b2898f 100644
--- a/botocore/client.py
+++ b/botocore/client.py
@@ -46,16 +46,17 @@ class ClientCreator(object):
     def create_client(self, service_name, region_name, is_secure=True,
                       endpoint_url=None, verify=None,
                       credentials=None, scoped_config=None,
+                      api_version=None,
                       client_config=None):
-        service_model = self._load_service_model(service_name)
+        service_model = self._load_service_model(service_name, api_version)
         cls = self._create_client_class(service_name, service_model)
         client_args = self._get_client_args(
             service_model, region_name, is_secure, endpoint_url,
             verify, credentials, scoped_config, client_config)
         return cls(**client_args)
 
-    def create_client_class(self, service_name):
-        service_model = self._load_service_model(service_name)
+    def create_client_class(self, service_name, api_version=None):
+        service_model = self._load_service_model(service_name, api_version)
         return self._create_client_class(service_name, service_model)
 
     def _create_client_class(self, service_name, service_model):
@@ -69,8 +70,9 @@ class ClientCreator(object):
         cls = type(str(service_name), tuple(bases), class_attributes)
         return cls
 
-    def _load_service_model(self, service_name):
-        json_model = self._loader.load_service_model(service_name, 'service-2')
+    def _load_service_model(self, service_name, api_version=None):
+        json_model = self._loader.load_service_model(service_name, 'service-2',
+                                                     api_version=api_version)
         service_model = ServiceModel(json_model, service_name=service_name)
         self._register_retries(service_model)
         return service_model
diff --git a/botocore/session.py b/botocore/session.py
index 4f6a29c80..a34b0a353 100644
--- a/botocore/session.py
+++ b/botocore/session.py
@@ -837,7 +837,7 @@ class Session(object):
         client = client_creator.create_client(
             service_name, region_name, use_ssl, endpoint_url, verify,
             credentials, scoped_config=self.get_scoped_config(),
-            client_config=config)
+            client_config=config, api_version=api_version)
         return client
 
 


Test patch:
diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py
index 4a3123803..d1f7eac5e 100644
--- a/tests/unit/test_client.py
+++ b/tests/unit/test_client.py
@@ -139,6 +139,29 @@ class TestAutoGeneratedClient(unittest.TestCase):
         self.assertEqual(service_client.meta.endpoint_url,
                          'https://foo.bar')
 
+    def test_api_version_is_passed_to_loader_if_provided(self):
+        creator = self.create_client_creator()
+        self.endpoint.host = 'https://foo.bar'
+        specific_api_version = '2014-03-01'
+        creator.create_client(
+            'myservice', 'us-west-2', credentials=self.credentials,
+            api_version=specific_api_version)
+        self.loader.load_service_model.assert_called_with(
+            'myservice', 'service-2', api_version=specific_api_version)
+
+    def test_create_client_class_creates_class(self):
+        creator = self.create_client_creator()
+        client_class = creator.create_client_class('myservice')
+        self.assertTrue(hasattr(client_class, 'test_operation'))
+
+    def test_create_client_class_forwards_api_version(self):
+        creator = self.create_client_creator()
+        specific_api_version = '2014-03-01'
+        creator.create_client_class('myservice',
+                                    api_version=specific_api_version)
+        self.loader.load_service_model.assert_called_with(
+            'myservice', 'service-2', api_version=specific_api_version)
+
     def test_client_uses_region_from_client_config(self):
         client_config = client.Config()
         client_config.region_name = 'us-west-1'
diff --git a/tests/unit/test_session.py b/tests/unit/test_session.py
index d9a3dbc2d..287706025 100644
--- a/tests/unit/test_session.py
+++ b/tests/unit/test_session.py
@@ -412,7 +412,8 @@ class TestCreateClient(BaseSessionTest):
 
         client_creator.return_value.create_client.assert_called_with(
             mock.ANY, mock.ANY, mock.ANY, mock.ANY, mock.ANY, mock.ANY,
-            scoped_config=mock.ANY, client_config=config)
+            scoped_config=mock.ANY, client_config=config,
+            api_version=mock.ANY)
 
     def test_create_client_with_region(self):
         ec2_client = self.session.create_client(