def save(self, list_id=None): """ Saves the current member to the list :param list_id: the id of the list to save to - if not specified, the members list_id will be used :return: True if successful """ hash_value = self.id if not list_id: list_id = self.list_id if not self.id: response = Request.post("%s" % MCMember.get_list_url(list_id), self.to_dict()) self._update(response.json()) return True try: response = Request.put( "%s/%s" % (MCMember.get_list_url(list_id), hash_value), self.to_dict()) self._update(response.json()) return True except Exception as e: logger.error("Unable to save member") raise e
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 test_post_with_relative_url(self): with responses.RequestsMock() as rsps: rsps.add(responses.POST, 'https://us1.api.mailchimp.com/3.0/instance', json={"Instance": { "Id": 1 }}, status=200, content_type='application/json') response = Request.post('/instance', {"Id": 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(1, len(rsps.calls)) self.assertEqual("https://us1.api.mailchimp.com/3.0/instance", rsps.calls[0].request.url) self.assertEqual(json.dumps({"Id": 1}), rsps.calls[0].request.body.decode("utf-8")) 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'])
def delete(self): """ Deletes the current member from the list :return: True if sucessful """ if not self.id: return False try: Request.delete("%s/%s" % (MCMember.get_list_url(self.list_id), self.id)) return True except Exception as e: logger.error("Unable to delete member from list") raise e
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 test_delete_with_relative_url(self): with responses.RequestsMock() as rsps: rsps.add(responses.DELETE, 'https://us1.api.mailchimp.com/3.0/instance/1', status=204, content_type='application/json') response = Request.delete('/instance/1') self.assertEqual(response.status_code, 204) self.assertEqual(b'', response.content) self.assertEqual(1, len(rsps.calls)) self.assertEqual("https://us1.api.mailchimp.com/3.0/instance/1", 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'])
def save(self): """ Saves the current category to the list :return: True if successful """ hash_value = self.id if not self.id: md = hashlib.md5() md.update(self.email_address.lower().encode("utf-8")) hash_value = md.hexdigest() try: response = Request.put( "%s/%s" % (MCMember.get_list_url(self.list_id), hash_value), self.to_dict()) self._update(response.json()) return True except Exception as e: logger.error("Unable to save member") raise e
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
def test_get_headers(self): self.assertEqual(Request.get_headers(), { 'Content-Type': 'application/json', 'Accept': 'application/json' })