Skip to content

Commit

Permalink
Propagated the result key behavior to Resource as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastdriven committed Dec 6, 2013
1 parent ca2f6d0 commit eb1775f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
44 changes: 35 additions & 9 deletions boto3/core/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ def identifiers(self):
"""
return self.resource_data['identifiers']

@requires_loaded
def result_key_for(self, op_name):
"""
Checks for the presence of a ``result_key``, which defines what data
should make up an instance.
Returns ``None`` if there is no ``result_key``.
:param op_name: The operation name to look for the ``result_key`` in.
:type op_name: string
:returns: The expected key to look for data within
:rtype: string or None
"""
ops = self.resource_data.get('operations', {})
op = ops.get(op_name, {})
key = op.get('result_key', None)

if key is None:
return key

# Because botocore.
return to_snake_case(key)


class Resource(object):
"""
Expand Down Expand Up @@ -306,14 +330,6 @@ def update_params(self, conn_method_name, params):
params.update(self.get_identifiers())
return params

def update_params_get(self, params):
params.update(self.get_identifiers())
return params

def update_params_delete(self, params):
params.update(self.get_identifiers())
return params

def full_post_process(self, conn_method_name, result):
"""
When a response from an API method call is received, this goes through
Expand Down Expand Up @@ -383,7 +399,17 @@ def post_process_get(self, result):
:returns: The unmodified response data
"""
for key, value in result.items():
# We need to possibly drill into the response & get out the data here.
# Check for a result key.
result_key = self._details.result_key_for('get')

if not result_key:
# There's no result_key. Just use the top-level data.
data = result
else:
data = result[result_key]

for key, value in data.items():
self._data[to_snake_case(key)] = value

return result
Expand Down
49 changes: 45 additions & 4 deletions tests/unit/core/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def update_params_delete(self, params):

def post_process(self, conn_method_name, result):
result = result.copy()
self.identifier = result.pop('Id')
self.identifier = result.pop('Id', None)
return result

def post_process_delete(self, result):
Expand Down Expand Up @@ -350,16 +350,57 @@ def test_full_post_process(self):
})
self.assertEqual(self.resource.deleted, True)

# ``.get(...)`` has different behavior, in that the data gets
# post-processed & assigned.
self.assertEqual(self.resource._data, {'id': '1872baf45'})
def test_full_post_process_get(self):
# Without a result key.
results = {
'Id': '1872baf45',
'Title': 'A pipe',
}
processed = self.resource.full_post_process('get', results)
self.assertEqual(processed, {
'Title': 'A pipe'
})
# Note that what get's assigned has been snake_cased.
self.assertEqual(self.resource.id, '1872baf45')
self.assertEqual(self.resource.title, 'A pipe')

def test_full_post_process_get_result_key(self):
orig_fake_data = self.fake_details._loaded_data
self.addCleanup(
setattr, self.fake_details, '_loaded_data', orig_fake_data
)

result_key_fake_data = orig_fake_data.copy()
result_key_fake_data['resources']['Pipe']['operations']['get'] = {
'api_name': 'GetPipe',
'result_key': 'Pipe',
}

resource = PipeResource(
connection=self.fake_conn,
id='92aa36e5b'
)

# With a result key.
self.fake_details._loaded_data = result_key_fake_data
results = {
'pipe': {
'Id': '92aa36e5b',
'Title': 'Another pipe',
}
}
self.assertEqual(resource._data, {'id': '92aa36e5b'})
processed = resource.full_post_process('get', results)
self.assertEqual(processed, {
'pipe': {
'Id': '92aa36e5b',
'Title': 'Another pipe'
}
})
# Despite being nested in the response, the right data is assigned.
self.assertEqual(resource.id, '92aa36e5b')
self.assertEqual(resource.title, 'Another pipe')


class ResourceFactoryTestCase(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit eb1775f

Please sign in to comment.