Repository: googleapis/google-cloud-python

Issue:
BigQuery client.list_datasets() fails when the project has no datasets
Found while doing clean-up of a project after reproducing #1419

Reproduce:

``` python
In [11]: client.list_datasets()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-fed4650eba20> in <module>()
----> 1 client.list_datasets()

gcloud-python/gcloud/bigquery/client.pyc in list_datasets(self, include_all, max_results, page_token)
     89                                            query_params=params)
     90         datasets = [Dataset.from_api_repr(resource, self)
---> 91                     for resource in resp['datasets']]
     92         return datasets, resp.get('nextPageToken')
     93 

KeyError: 'datasets'``
```



Discussion:
Full payload of response

``` json
{
  "etag": "\"AMpOK...\"", 
  "kind": "bigquery#datasetList"
}
```


Resolving patch:
diff --git a/gcloud/bigquery/client.py b/gcloud/bigquery/client.py
--- a/gcloud/bigquery/client.py
+++ b/gcloud/bigquery/client.py
@@ -88,7 +88,7 @@ def list_datasets(self, include_all=False, max_results=None,
         resp = self.connection.api_request(method='GET', path=path,
                                            query_params=params)
         datasets = [Dataset.from_api_repr(resource, self)
-                    for resource in resp['datasets']]
+                    for resource in resp.get('datasets', ())]
         return datasets, resp.get('nextPageToken')
 
     def dataset(self, dataset_name):


Test patch:
diff --git a/gcloud/bigquery/test_client.py b/gcloud/bigquery/test_client.py
--- a/gcloud/bigquery/test_client.py
+++ b/gcloud/bigquery/test_client.py
@@ -74,27 +74,11 @@ def test_list_datasets_defaults(self):
         self.assertEqual(req['method'], 'GET')
         self.assertEqual(req['path'], '/%s' % PATH)
 
-    def test_list_datasets_explicit(self):
-        from gcloud.bigquery.dataset import Dataset
+    def test_list_datasets_explicit_response_missing_datasets_key(self):
         PROJECT = 'PROJECT'
-        DATASET_1 = 'dataset_one'
-        DATASET_2 = 'dataset_two'
         PATH = 'projects/%s/datasets' % PROJECT
         TOKEN = 'TOKEN'
-        DATA = {
-            'datasets': [
-                {'kind': 'bigquery#dataset',
-                 'id': '%s:%s' % (PROJECT, DATASET_1),
-                 'datasetReference': {'datasetId': DATASET_1,
-                                      'projectId': PROJECT},
-                 'friendlyName': None},
-                {'kind': 'bigquery#dataset',
-                 'id': '%s:%s' % (PROJECT, DATASET_2),
-                 'datasetReference': {'datasetId': DATASET_2,
-                                      'projectId': PROJECT},
-                 'friendlyName': 'Two'},
-            ]
-        }
+        DATA = {}
         creds = _Credentials()
         client = self._makeOne(PROJECT, creds)
         conn = client.connection = _Connection(DATA)
@@ -102,11 +86,7 @@ def test_list_datasets_explicit(self):
         datasets, token = client.list_datasets(
             include_all=True, max_results=3, page_token=TOKEN)
 
-        self.assertEqual(len(datasets), len(DATA['datasets']))
-        for found, expected in zip(datasets, DATA['datasets']):
-            self.assertTrue(isinstance(found, Dataset))
-            self.assertEqual(found.dataset_id, expected['id'])
-            self.assertEqual(found.friendly_name, expected['friendlyName'])
+        self.assertEqual(len(datasets), 0)
         self.assertEqual(token, None)
 
         self.assertEqual(len(conn._requested), 1)