MailChimp documentation:

* This library can be used in any .NET language (c#, vb). Just add reference to "FSharp.Core.dll".

Example code:

 1) Ping MailChimp server:

open MailChimp
open MailChimp.Types

let key = "***-us1"
let m = MCApi(key, true)
printfn "Ping: %A" (m.Ping()) // "Ping: Everything's Chimpy!"

2) CampaignSegmentTest:

let key = "***-us1"
let m = MCApi(key, true)

let sopt = Campaign.SegmentOptions.Parse(Campaign.Match.AND,  
                    [ "static_segment => eq => 86";    
                      "date => gt => 2011-01-01";    
                      "social_gender => eq => male" ])

let result = mc.CampaignSegmentTest(lid, sopt)

Note, that you can supply both single and multiple values for SegmentCondition.Value parameter. In general, there is no need to escape commas - the wrapper will handle it for you. However this behavior can be overridden by postpending commas with another backslash - ",\"!

CampaignSegmentTest: C#:

Assume that we want to get the number of subscribers in the next interest groups: "\X1\", "X2,X2", "X3\,X3", "X4\\,X4" (groups names contain commas and backslashes).
Note that there are several ways to create segment_opts parameter:

Single condition:

var cnd = new Campaign.SegmentCondition("interests-1", "one", @"\X1\ ,\ X2,X2,\ X3\,X3,\ X4\\,X4");

var cnd = new Campaign.SegmentCondition("interests-1", "one", new string [] { @"\X1\", @"X2,X2", @"X3\,X3", @"X4\\,X4" });

Segment Options parameter:

var sopt = Campaign.SegmentOptions.Parse(Campaign.Match.AND, @"interests-1 => one => \X1\ , X2\,X2, X3\\,X3, X4\\\,X4");

var sopt = Campaign.SegmentOptions.Parse(Campaign.Match.AND, new string[] { @"interests-1 => one => \X1\ , X2\,X2, X3\\,X3, X4\\\,X4" });

3) Custom types (like opt, mcnull, mcseq) implement implicit/explicit conversion operators that simplifies usage of these types for C#/VB users:

Campaigns; C#:

var mc = new MCApi(key, true);

var filter = new Campaign.Filter();
filter.FolderID = 5; // single value
filter.FolderID = new int[] {1, 2, 3, 4, 5}; // array of values
filter.Exact = false;

var result = mc.Campaigns(filter);

MailChimp.Types.Opt<'T>; C#:

// Implicit conversion:
Opt<int> x = 5;  is equivalent to Opt<int> x = new Opt<int>(5);

// Explicit conversion:
int z = (int)x; is equivalent to int z = x.Value;

// null ~ None:
x = null; is equivalent to x = Opt<int>.None;

4) Mandrill (Transactional Email Service):

This class works the same way as other wrappers do. Just initialize MandrillApi type with appropriate key and search for relevant methods.

let key = "***"
let m = MandrillApi(key)
printfn "Ping: %A" (m.Ping()) // "Ping: PONG!"

5) Webhooks Listener:

Look at the attached WebhooksTester project (downloads page). As starting point look at the HttpListener class documentation (MSDN) to see how to define prefixes and SSL support.

Last edited May 13, 2013 at 12:22 PM by denisk1981, version 35


No comments yet.