def test_get_404(self): with responses.RequestsMock() as rsps: rsps.add(responses.GET, 'https://us1.api.mailchimp.com/3.0/instance/4', json={'type': 'Not found'}, status=404, content_type='application/json') with self.assertRaises(ObjectNotFound): Request.get('https://us1.api.mailchimp.com/3.0/instance/4')
def test_get(self): with responses.RequestsMock() as rsps: rsps.add(responses.GET, 'https://us1.api.mailchimp.com/3.0/instance', json={"Instances": [{ "Id": 1 }, { "Id": 2 }]}, status=200, content_type='application/json') response = Request.get( 'https://us1.api.mailchimp.com/3.0/instance') response_json = response.json() self.assertTrue("Instances" in response_json.keys()) for item in response_json['Instances']: self.assertTrue("Id" in item.keys()) self.assertEqual(1, len(rsps.calls)) self.assertEqual("https://us1.api.mailchimp.com/3.0/instance", rsps.calls[0].request.url) self.assertEqual("application/json", rsps.calls[0].request.headers['Accept']) self.assertEqual("application/json", rsps.calls[0].request.headers['Content-Type']) self.assertEqual( "Basic dXNlcm5hbWU6YTY1YTY1YTY1YTY1YTY1YTU2YTVhNi11czE=", rsps.calls[0].request.headers['Authorization']) rsps.add(responses.GET, 'https://us1.api.mailchimp.com/3.0/instance/1', json={"Instance": { "Id": 1 }}, status=200, content_type='application/json') response = Request.get( 'https://us1.api.mailchimp.com/3.0/instance/1') response_json = response.json() self.assertTrue("Instance" in response_json.keys()) self.assertTrue("Id" in response_json['Instance'].keys()) self.assertEqual(1, response_json['Instance']['Id']) self.assertEqual(2, len(rsps.calls)) self.assertEqual("https://us1.api.mailchimp.com/3.0/instance/1", rsps.calls[1].request.url) self.assertEqual("application/json", rsps.calls[1].request.headers['Accept']) self.assertEqual("application/json", rsps.calls[1].request.headers['Content-Type']) self.assertEqual( "Basic dXNlcm5hbWU6YTY1YTY1YTY1YTY1YTY1YTU2YTVhNi11czE=", rsps.calls[1].request.headers['Authorization'])
def get(cls, list_id): """ Get the list corresponding with the id list_id from the mailchimp API. :param list_id: the id of the list to get :return: the MCList item if successful, raises a ListNotFound exception otherwise """ try: response = Request.get("%s/%s" % (cls.item_url, list_id)) content = response.json() return MCList(content) except ObjectNotFound: raise MCListNotFound(list_id)
def get(cls, list_id, member_id): """ Get the member from the mailchimp API. list_id has to be a valid list and member_id should be the hash of the members email address. :param list_id: the list id to get the member from :param member_id: the hashed email address. :return: a MCMember object containing the member if successful, raises an MemberNotFound exception otherwise """ try: response = Request.get("%s/%s" % (MCMember.get_list_url(list_id), member_id)) return MCMember(response.json()) except ObjectNotFound: raise MCMemberNotFound(list_id, member_id)
def list(cls, list_id, params={}): """ Get the list of members for the list corresponding with the id list_id from the mailchimp API. :param list_id: the id of the list to get members from :param params: parameters for defining limits for the search - can be used to page result or search for a specific status. :return: an array of MCMember objects if successful, raises a ListNotFound exception otherwise """ try: response = Request.get("%s" % MCMember.get_list_url(list_id), params) return [MCMember(member) for member in response.json()['members']] except ObjectNotFound: raise MCListNotFound(list_id)
def get(cls, list_id, category_id): """ Get the category from the mailchimp API. list_id has to be a valid list and category_id should be the id of the category to retrieve. :param list_id: the list id to get the category from :param category_id: the category to get :return: a MCInterestCategory object containing the category if successful, raises an MCInterestCategoryNotFound exception otherwise """ try: response = Request.get( "%s/%s" % (MCInterestCategory.get_list_url(list_id), category_id)) return MCInterestCategory(response.json()) except ObjectNotFound: raise MCInterestCategoryNotFound(list_id, category_id)
def list(cls, params={}): """ Get all list items from the mailchimp API for the current user. :param params: parameters for defining limits for the search - can be used to page result. :return: a list of MCList items. """ search_params = {} lists = [] if params: for key in params.keys(): if key in cls.valid_search_params: search_params[key] = params[key] response = Request.get(cls.item_url, search_params) content = response.json() if 'lists' in content: for list_item in content['lists']: lists.append(MCList(list_item)) return lists