forked from saifiqbal/AmazonSNS
/
AwsSnshelper.cs
167 lines (156 loc) · 6.14 KB
/
AwsSnshelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
namespace AmazonSNS
{
/*
* Created By Saif Iqbal
* Dated : Aug 24 2015
* Purpose: To raise any notification on amazon sns whenever any changes made to patient intake form.
*/
public class AWSSNS
{
#region Private Methods
private void PublishSNSEvent(string Message, string AmazonSNSKey, string AmazonSNSSecretKey, string TopicName, string EventType)
{
string topicname = ProcessSNSTopicName(TopicName);
try
{
var client = new AmazonSimpleNotificationServiceClient(AmazonSNSKey, AmazonSNSSecretKey);
string topicarn = string.Empty;
PublishResult publishResult = new PublishResult();
if (IsTopicExists(client, topicname, out topicarn))
{
PublishRequest publishRequest = new PublishRequest();
publishRequest.Message = Message;
publishRequest.TopicArn = topicarn;
PublishResponse publishResponse = client.Publish(publishRequest);
publishResult = publishResponse.PublishResult;
}
else
{
CreateTopicRequest topicRequest = new CreateTopicRequest();
topicRequest.Name = topicname;
CreateTopicResponse topicResponse = client.CreateTopic(topicRequest);
CreateTopicResult result = topicResponse.CreateTopicResult;
PublishRequest publishRequest = new PublishRequest();
publishRequest.Message = Message;
publishRequest.TopicArn = result.TopicArn;
PublishResponse publishResponse = client.Publish(publishRequest);
publishResult = publishResponse.PublishResult;
}
if (publishResult.IsSetMessageId())
{
CreateLogs(Message, EventType, topicname, true);
}
}
catch (AmazonSimpleNotificationServiceException ex)
{
/*Log details to logentries server.*/
CreateLogs(Message, EventType, topicname, false);
throw ex;
}
}
private string ProcessSNSTopicName(string TopicName)
{
string topicname = string.Empty;
try
{
Regex r = new Regex("(?:[^a-zA-Z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
topicname = r.Replace(TopicName, String.Empty);
topicname = topicname.Replace(" ", string.Empty);
}
catch (Exception ex)
{
throw ex;
}
return topicname;
}
private string CreateMessageBody(string IntakeID, string UserID, string Eventtype)
{
string Message = "";
try
{
Message = String.Format(Constants.SNSMessageBody, Eventtype, IntakeID, UserID);
}
catch (Exception ex)
{
throw ex;
}
return Message;
}
private void CreateLogs(string message, string EventName, string TopicName, bool IsSucess)
{
try
{
DbWrapper.ExecuteNonQuery("CreateSNSLogs",
message, TopicName, EventName,
IsSucess);
}
catch (Exception ex)
{
throw ex;
}
}
private void ProcessSNSEvent(string IntakeID, string UserID, string EventType, string AmazonSNSKey, string AmazonSNSSecretKey, string TopicName)
{
try
{
string message = CreateMessageBody(IntakeID.ToString(), UserID, EventType);
PublishSNSEvent(message, AmazonSNSKey, AmazonSNSSecretKey, TopicName, EventType);
}
catch (Exception ex)
{
throw ex;
}
}
private bool IsTopicExists(AmazonSimpleNotificationServiceClient client, string Topic, out string TopicArn)
{
ListTopicsRequest lstTopicRequest = new ListTopicsRequest();
ListTopicsResponse lstTopicResponse = client.ListTopics(lstTopicRequest);
ListTopicsResult lstTopicResult = lstTopicResponse.ListTopicsResult;
List<Topic> TopicsList = lstTopicResult.Topics;
TopicArn = string.Empty;
bool IsTopicExists = TopicsList.Exists(x => x.TopicArn.Split(':')[x.TopicArn.Split(':').Count() - 1] == Topic);
if (IsTopicExists)
{
string topicarn = TopicsList.Where(x => x.IsSetTopicArn() & x.TopicArn.Split(':')[x.TopicArn.Split(':').Count() - 1] == Topic).Select(s => s.TopicArn).First();
TopicArn = topicarn;
}
return IsTopicExists;
}
#endregion
#region Public Methods
public void CreateSNSEvent(int FormID, string IntakeID, string UserID, string AmazonSNSKey, string AmazonSNSSecretKey, string TopicName, string eventType = "")
{
try
{
string EventType = string.Empty;
switch (FormID)
{
case 1002: /**/
EventType="Updated";
default:
break;
}
if (!string.IsNullOrEmpty(eventType))
{
EventType = eventType;
}
ProcessSNSEvent(IntakeID, UserID, EventType, AmazonSNSKey, AmazonSNSSecretKey, TopicName);
}
catch (Exception ex)
{
throw ex;
}
}
public void SubscribeEvents(string emailaddress)
{
}
#endregion
}
}