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.
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
private static async Task RestExample3(HttpClient client)
{
string url = "https://example.com/API/Rest/Subscriptions/List/Email?accountName=acme&login=ApiUser&emailAddress=joe@example.com";
string result;
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
request.Headers.Add("Password", "YOUR_PASSWORD");
using (HttpResponseMessage response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
result = await response.Content.ReadAsStringAsync();
}
}
if (!result.TrimStart().StartsWith("["))
{
Console.WriteLine("Subscription list failed: " + result);
return;
}
using (JsonDocument subscriptions = JsonDocument.Parse(result))
{
foreach (JsonElement row in subscriptions.RootElement.EnumerateArray())
Console.WriteLine(row.GetProperty("topicName") + " " + row.GetProperty("subscribed"));
}
var body = new
{
accountName = "acme",
login = "ApiUser",
password = "YOUR_PASSWORD",
emailAddress = "joe@example.com",
unsubscribedTopics = new[] { "Samsung News" },
subscribedTopics = new[] { "Apple News" }
};
using (HttpResponseMessage response =
await client.PostAsJsonAsync("https://example.com/API/Rest/Subscriptions/Change", body))
{
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
# encoding: utf-8
require 'rest-client'
require 'json'
url = "https://example.com/api/rest/Subscriptions/List/Email?accountName=acme&login=ApiUser&emailAddress=joe@example.com"
resp = RestClient.get url, {:Password => "YOUR_PASSWORD"}
if resp.code != 200
puts "Subscription list failed!"
puts resp.code
exit
elsif resp.body[0] != '['
puts 'Error: ' + resp.body
exit
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":"YOUR_PASSWORD", "emailAddress":"joe@example.com",
'unsubscribedTopics':['Samsung News'],
'subscribedTopics':['Apple News']}
url = "https://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
exit
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 = "https://example.com/api/rest/Subscriptions/List/Email?accountName=acme&login=ApiUser&emailAddress=joe@example.com"
headers = {'Password':'YOUR_PASSWORD'}
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":"YOUR_PASSWORD", "emailAddress":"joe@example.com",
'unsubscribedTopics':['Business News'],
'subscribedTopics':['Apple News']}
url = "https://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()