Mailchimp is a free email marketing service provider. It helps you to design email newsletters, share them on social networks, integrate with services you already use and track your results.
Haskell is an advanced purely-functional programming language, which has improved performs than other imperative languages.
I have developed a library in Haskell interacting with the JSON API in Mailchimp, which enables one to do the following tasks with respect to a mailing list in Mailchimp;
- Add subscribers
- List subscribers in a particular mailing list
- List mailing lists for a particular user account
- Past activities performed on a given mailing list
Before starting off with the implementation, you should understand the functionalities we can perform on a mailing list in Mailchimp. For that, you should create a mailing list in Mailchimp. So, let's first go through the steps of creating a mailing list in Mailchimp (Here I'm referring to version 2.0 API)
Create a mailing list in Mailchimp
- After creating an account in Mailchimp, first thing you have to do is the creation of an 'apikey', which provides the authentication purpose in the JSON API in Haskell
- Navigate to your 'Account'
- Then go to the 'API keys' from the drop down menu in 'Extras'
- Then create an API key from the menu
Implementation of library
After that I recommend you to read the Mailchimp API documentation. This one is the Mailchimp version 2.0. Pay a closer attention towards the sample JSON requests and responses.
Then select the functionality you want in your library, and start coding. In my library I have selected the above mentioned functionalities.
The first step of the implementation is the construction of the EndPoint URL, since all of the method descriptions in the API documentation are relative to it. The format of the end-point URL is as follows; `https://<dc>.api.mailchimp.com/2.0/SECTION/SOME_METHOD.OUTPUT_FORMAT`, where `<dc>` refers to the data-center prefix.
The data-center prefix is the last part of your apikey separated by the "-" character. In order to retrieve the end-point URL you need to extract it. Here is a snapshot of my implementation of it;
One of the main sections, that you must pay attention is the exception handling in HTTP requests and responses.
In my implementation I have used the `catch` method imported from `Control.Exception` package.
As explained in the Hackage documentation the statement which throws the exception is encapsulated within the catch statement, and then in the handler function the thrown `StatusCodeException` is handled by calling the `getResponse` function.
Here the `StatusCodeException` has 3 parameters, they are; Status, ResponseHeaders, and CookieJar as explained in the Hackage documentation.
These are then passed into `getResponse` function. Using those 3 components it builds a Response of the same format as `withManager $ httpLbs req`.
Here is the link to the GitHub repository of my code, which is a work in progress. I hope to further develop it to support Mailchimp version 3.0 in the future.