diff --git a/boxsdk/object/base_api_json_object.py b/boxsdk/object/base_api_json_object.py index eecbdd4a2..bc85686be 100644 --- a/boxsdk/object/base_api_json_object.py +++ b/boxsdk/object/base_api_json_object.py @@ -143,3 +143,13 @@ def response_object(self): `dict` """ return copy.deepcopy(self._response_object) + + @property + def object_type(self): + """Return the Box type for the object. + + :rtype: + `unicode` + """ + return self._item_type + diff --git a/boxsdk/object/item.py b/boxsdk/object/item.py index fec3bcc6d..c0f4c83f9 100644 --- a/boxsdk/object/item.py +++ b/boxsdk/object/item.py @@ -358,6 +358,56 @@ def metadata(self, scope='global', template='properties'): """ return Metadata(self._session, self, scope, template) + def create_metadata(self, key_1, scope, template, key_2=None): + """ + Create a metadata instance for a file. + :param key_1: + Key value pair to add to metadata. + :type key_1: + `dict` + :param scope: + The scope of the metadata object. + :type scope: + `unicode` + :param template: + The key of the template + :type template: + `unicode` + """ + url = self.get_url('metadata', scope, template) + body = { + key_1 + } + if key_2 is not None: + body.update(key_2) + headers = {'Content-Type': 'application/json'} + response = self._session.post(url, data=json.dumps(body), headers=headers).json() + return Translator().translate(response['type'])( + self._session, + response['id'], + response, + ) + + def delete_metadata(self, scope, template): + """ + Delete metadata instance on the item. + :param scope: + The scope of the metadata object. + :type scope: + `unicode` + :param template: + The key of the template + :type template: + `unicode` + """ + url = self.get_url('metadata', scope, template) + response = self._session.delete(url, headers=headers).json() + return Translator().translate(response['type'])( + self._session, + response['id'], + response, + ) + def get_watermark(self): """ Return the watermark info for a Box file diff --git a/test/unit/client/test_client.py b/test/unit/client/test_client.py index b8ce61986..2f5344127 100644 --- a/test/unit/client/test_client.py +++ b/test/unit/client/test_client.py @@ -443,6 +443,50 @@ def test_get_groups_return_the_correct_group_objects( mock_box_session.get.assert_called_once_with(expected_url, params={'offset': None}) +def test_create_retention_policy(mock_client, mock_box_session): + policy_name = 'Test Retention Policy' + policy_type = 'indefinite' + disposition_action = 'remove_retention' + expected_url = mock_box_session.get_url('retention_policies') + expected_data = { + 'policy_name': policy_name, + 'policy_type': policy_type, + 'disposition_action': disposition_action, + 'can_owner_extend_retention': False, + 'are_owners_notified': False + } + mock_policy = { + 'type': 'retention_policy', + 'id': '1234', + 'policy_name': policy_name, + } + mock_box_session.post.return_value.json.return_value = mock_policy + policy = mock_client.create_retention_policy(policy_name, policy_type, disposition_action) + mock_box_session.post.assert_called_once_with(expected_url, data=json.dumps(expected_data)) + assert policy.id == mock_policy['id'] + assert policy.type == mock_policy['type'] + + +def test_get_retention_policies(mock_client, mock_box_session): + expected_url = mock_box_session.get_url('retention_policies') + mock_policy = { + 'type': 'retention_policy', + 'id': '12345', + 'name': 'Test Retention Policy' + } + mock_box_session.get.return_value.json.return_value = { + 'limit': 100, + 'entries': [mock_policy], + 'next_marker': 'testMarker' + } + policies = mock_client.retention_policies() + policy = policies.next() + mock_box_session.get.assert_called_once_with(expected_url, params={'policy_name': None, 'policy_type': None, 'created_by_user_id': None}) + assert isinstance(policy, RetentionPolicy) + assert policy.id == mock_policy['id'] + assert policy.name == mock_policy['name'] + + def test_create_group_returns_the_correct_group_object(mock_client, mock_box_session, create_group_response): # pylint:disable=redefined-outer-name expected_url = "{0}/groups".format(API.BASE_API_URL)