REST Email Marketing API Documentation

Example 3 — Manage subscriptions

This example shows a technique for fetching all the topics that the email address is currently either subscribed to or unsubscribed from. This list could be used to generate some HTML for display.

Next is to change the subscriptions, subscribing to a topic and unsubscribing from another.

private class SubscriptionListData
{
	public string topicName;
	public int topicId;
	public bool subscribed;
	public System.DateTime subscribeDate;
	public System.DateTime unsubscribeDate;
}

private static void RestExample3()
{
	try
	{ 
		string url = "http://www.example.com/api/rest/Subscriptions/List?accountName=acme&login=ApiUser&emailAddress=joe@example.com";

		System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
		request.Headers.Add("Password", "xdfgdf7dfa67dfgsdfg");
		string results;
		using (System.Net.WebResponse response = request.GetResponse())
			using (Stream responseStream = response.GetResponseStream())
				using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
					results = reader.ReadToEnd();

		if ( !results.StartsWith("["))
		{
			System.Console.WriteLine("Error: {0}", results);
			return;
		}

		System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
		System.Collections.Generic.List<SubscriptionListData> rd = jss.Deserialize<System.Collections.Generic.List<SubscriptionListData>>(results);

		foreach (SubscriptionListData row in rd)
		{
			//format these in HTML for the user to view and change. We'll just print them.
			System.Console.WriteLine(row.topicName + "  " + row.subscribed);
		}

		// we'll assume now that we have the list of topics to subscribe and unsubscribe for the user.

		string data = "{'accountName':'acme','login':'ApiUser','password':'xdfgdf7dfa67dfgsdfg', 'emailAddress':'joe@example.com',";
		data += "'unsubscribedTopics':['Samsung News'],";
		data += "'subscribedTopics':['Apple News']}";

		url = "http://www.example.com/api/rest/Subscriptions/Change";

		request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
		request.Method = "POST";
		request.ContentType = "application/json";
		request.ContentLength = data.Length;
		using (Stream webStream = request.GetRequestStream())
			using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
				requestWriter.Write(data);

		using (System.Net.WebResponse webResponse = request.GetResponse())
			using (Stream webStream = webResponse.GetResponseStream())
				using (StreamReader responseReader = new StreamReader(webStream))
					results = responseReader.ReadToEnd();

		// The return data indicates the topic and the change, so you can echo this back
		// to the user, if desired.

		System.Console.WriteLine(results);
	}
	catch (System.Exception e)
	{
		System.Diagnostics.Debug.Fail(e.Message);
	}
}						
						
# encoding: utf-8
require 'rest-client'
require 'json'

url = "http://www.example.com/api/rest/Subscriptions/List?accountName=acme&login=ApiUser&emailAddress=joe@example.com"
resp = RestClient.get url, {:Password => "vsdjhsdv7fsahsfd7sgd"}
if resp.code != 200
    puts "Subscription list failed!"
    puts resp.code
    return
elsif resp.body[0] != '['
    puts 'Error: ' + resp.body
    return
end

data = JSON.parse(resp.body)
for row in data
    #format these in HTML for the user to view and change. We'll just puts them.
    puts row["topicName"] + "  " + row["subscribed"].to_s
end

#we'll assume now that we have the list of topics to subscribe and unsubscribe from.

args = {"accountName":"acme","login":"ApiUser","password":"vsdjhsdv7fsahsfd7sgd", "emailAddress":"joe@example.com",
        'unsubscribedTopics':['Samsung News'],
        'subscribedTopics':['Apple News']}
url = "http://www.example.com/api/rest/Subscriptions/Change"
resp = RestClient.post url, args.to_json, {"Content-Type": "application/json"}
if resp.code != 200
    puts "Subscription list failed!"
    puts resp.code
    return
end

puts resp.body

# The return data indicates the topic and the change, so you can echo this back
# to the user, if desired.
import requests
import json


def subscriptions():
    url = "http://www.example.com/api/rest/Subscriptions/List?accountName=acme&login=ApiUser&emailAddress=joe@example.com"
	headers = {'Password':'xfgsd7w47te8dfngad'}
    resp = requests.get(url, headers=headers)
    if resp.status_code != 200:
        print "Subscription list failed!"
        print resp.status_code
        return
    data = json.loads(resp.text)
    for row in data:
        #format these in HTML for the user to view and change. We'll just print them.
        print (row["topicName"] + "  " + str(row["subscribed"]))

    #we'll assume now that we have the list of topics to subscribe and unsubscribe from.

    args = {"accountName":"acme","login":"ApiUser","password":"sdvjusv7svdhsd7", "emailAddress":"joe@example.com",
            'unsubscribedTopics':['Business News'],
            'subscribedTopics':['Apple News']}
    url = "http://www.example.com/api/rest/Subscriptions/Change"
    resp = requests.post(url, json=args, headers=headers)
    if resp.status_code != 200:
        print "Subscription list failed!"
        print resp.status_code
        return

    print resp.text

    # The return data indicates the topic and the change, so you can echo this back
    # to the user, if desired.

subscriptions()

	

Share this: