mailchimp.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from wagtail.models import Site
  2. from coderedcms.models.wagtailsettings_models import LayoutSettings
  3. import requests
  4. class MailchimpApi:
  5. user_string = "Website"
  6. proto_base_url = "https://{0}.api.mailchimp.com/3.0/"
  7. def __init__(self, site=None):
  8. self.set_access_token(site=None)
  9. def set_access_token(self, site=None):
  10. site = site or Site.objects.get(is_default_site=True)
  11. self.access_token = LayoutSettings.for_site(site).mailchimp_api_key
  12. if self.access_token:
  13. self.set_base_url()
  14. self.is_active = True
  15. else:
  16. self.is_active = False
  17. def set_base_url(self):
  18. """
  19. The base url for the mailchimip api is dependent on the api key.
  20. """
  21. key, datacenter = self.access_token.split("-")
  22. self.base_url = self.proto_base_url.format(datacenter)
  23. def default_headers(self):
  24. return {
  25. "Content-Type": "application/json",
  26. }
  27. def default_auth(self):
  28. return (self.user_string, self.access_token)
  29. def get_lists(self):
  30. endpoint = "lists?fields=lists.name,lists.id"
  31. json_response = self._get(endpoint)
  32. return json_response
  33. def get_merge_fields_for_list(self, list_id):
  34. endpoint = (
  35. f"lists/{list_id}/merge-fields"
  36. "?fields=merge_fields.tag,merge_fields.merge_id,merge_fields.name"
  37. )
  38. json_response = self._get(endpoint)
  39. return json_response
  40. def get_interest_categories_for_list(self, list_id):
  41. endpoint = (
  42. f"lists/{list_id}/interest-categories"
  43. "?fields=categories.id,categories.title"
  44. )
  45. json_response = self._get(endpoint)
  46. return json_response
  47. def get_interests_for_interest_category(
  48. self, list_id, interest_category_id
  49. ):
  50. endpoint = (
  51. f"lists/{list_id}/interest-categories/{interest_category_id}/interests"
  52. "?fields=interests.id,interests.name"
  53. )
  54. json_response = self._get(endpoint)
  55. return json_response
  56. def add_user_to_list(self, list_id, data):
  57. endpoint = "lists/{0}".format(list_id)
  58. json_response = self._post(endpoint, data=data)
  59. return json_response
  60. def _get(self, endpoint, data={}, auth=None, headers=None, **kwargs):
  61. auth = auth or self.default_auth()
  62. headers = headers or self.default_headers()
  63. full_url = "{0}{1}".format(self.base_url, endpoint)
  64. r = requests.get(
  65. full_url, auth=auth, headers=headers, data=data, **kwargs
  66. )
  67. return r.json()
  68. def _post(self, endpoint, data={}, auth=None, headers=None, **kwargs):
  69. auth = auth or self.default_auth()
  70. headers = headers or self.default_headers()
  71. full_url = "{0}{1}".format(self.base_url, endpoint)
  72. r = requests.post(
  73. full_url, auth=auth, headers=headers, data=data, **kwargs
  74. )
  75. return r.json()