Skip to content

Commit 2c7d550

Browse files
authored
Merge pull request #12 from gilmae/master
Adds property for the Subject field used in MMS messages.
2 parents e7bf6b2 + 5c43e64 commit 2c7d550

File tree

3 files changed

+103
-67
lines changed

3 files changed

+103
-67
lines changed
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,78 @@
11
using System;
22
using System.Collections.Generic;
33
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Converters;
45

56
namespace MessageMedia.Messages.Models
67
{
78
public struct Message
89
{
9-
[JsonProperty("message_id")] public string MessageId { get; set; }
10+
[JsonProperty("message_id", NullValueHandling = NullValueHandling.Ignore)] public string MessageId { get; set; }
1011

1112
/// <summary>
1213
/// Urls of the media files to send in the Message
1314
///
1415
/// <remarks>Only valid if the Format is MMS</remarks>
1516
/// </summary>
16-
[JsonProperty("media")] public string[] Media { get; set; }
17+
[JsonProperty("media", NullValueHandling = NullValueHandling.Ignore)] public string[] Media { get; set; }
1718

18-
[JsonProperty("status")] public MessageStatus Status { get; set; }
19+
/// <summary>
20+
/// Subject of the Message
21+
///
22+
/// <remarks>Only valid if the Format is MMS</remarks>
23+
/// </summary>
24+
[JsonProperty("subject", NullValueHandling = NullValueHandling.Ignore)] public string Subject { get; set; }
25+
26+
[JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)]
27+
[JsonConverter(typeof(StringEnumConverter))]
28+
public MessageStatus? Status { get; set; }
1929

2030
/// <summary>
2131
/// Replies and delivery reports for this message will be pushed to the URL"
2232
/// </summary>
23-
[JsonProperty("callback_url")] public string CallbackUrl { get; set; }
33+
[JsonProperty("callback_url", NullValueHandling = NullValueHandling.Ignore)] public string CallbackUrl { get; set; }
2434

2535
/// <summary>
2636
/// Content of the message
2737
/// <example>Hello world!</example>
2838
/// </summary>
29-
[JsonProperty("content")] public string Content { get; set; }
39+
[JsonProperty("content", NullValueHandling = NullValueHandling.Ignore)] public string Content { get; set; }
3040

3141
/// <summary>
3242
/// Destination number of the message
3343
/// <example>+61491570156</example>
3444
/// </summary>
35-
[JsonProperty("destination_number")] public string DestinationNumber { get; set; }
45+
[JsonProperty("destination_number", NullValueHandling = NullValueHandling.Ignore)] public string DestinationNumber { get; set; }
3646

3747
/// <summary>
3848
/// Request a delivery report for this message
3949
/// </summary>
40-
[JsonProperty("delivery_report")] public bool DeliveryReport { get; set; }
50+
[JsonProperty("delivery_report", NullValueHandling = NullValueHandling.Ignore)] public bool DeliveryReport { get; set; }
4151

4252
/// <summary>
4353
/// Format of message, SMS or TTS (Text To Speech).
4454
/// </summary>
45-
[JsonProperty("format")] public MessageFormat Format { get; set; }
55+
[JsonProperty("format", NullValueHandling = NullValueHandling.Ignore)]
56+
[JsonConverter(typeof(StringEnumConverter))]
57+
public MessageFormat Format { get; set; }
4658

4759
/// <summary>
4860
/// Date time after which the message expires and will not be sent
4961
/// </summary>
50-
[JsonProperty("message_expiry_timestamp")]
51-
public DateTime MessageExpiryTimestamp { get; set; }
62+
[JsonProperty("message_expiry_timestamp", NullValueHandling = NullValueHandling.Ignore)]
63+
public DateTime? MessageExpiryTimestamp { get; set; }
5264

5365
/// <summary>
5466
/// Metadata for the message specified as a set of key value pairs.
5567
///
5668
/// <remarks>Each key can be up to 100 characters long and each value can be up to 256 characters long.</remarks>
5769
/// </summary>
58-
[JsonProperty("metadata")] public Dictionary<string, string> Metadata { get; set; }
70+
[JsonProperty("metadata", NullValueHandling = NullValueHandling.Ignore)] public Dictionary<string, string> Metadata { get; set; }
5971

6072
/// <summary>
6173
/// Scheduled delivery date time of the message
6274
/// </summary>
63-
[JsonProperty("scheduled")] public DateTime Scheduled { get; set; }
75+
[JsonProperty("scheduled", NullValueHandling = NullValueHandling.Ignore)] public DateTime? Scheduled { get; set; }
6476

6577
/// <summary>
6678
/// Source of the message
@@ -70,11 +82,13 @@ public struct Message
7082
///
7183
/// <remarks>By default this feature is not available and will be ignored in the request. Please contact [email protected] for more information. Specifying a source number is optional and a by default a source number will be assigned to the message.</remarks>
7284
/// </summary>
73-
[JsonProperty("source_number")] public string SourceNumber { get; set; }
85+
[JsonProperty("source_number", NullValueHandling = NullValueHandling.Ignore)] public string SourceNumber { get; set; }
7486

7587
/// <summary>
7688
/// Type of source address specified
7789
/// </summary>
78-
[JsonProperty("source_number_type")] public NumberType SourceNumberType { get; set; }
90+
[JsonProperty("source_number_type", NullValueHandling = NullValueHandling.Ignore)]
91+
[JsonConverter(typeof(StringEnumConverter))]
92+
public NumberType SourceNumberType { get; set; }
7993
}
8094
}

MessageMediaMessages.Tests/MessagesControllerTest.cs

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,38 @@ public static void SetUpClass()
9999
public async Task TestSendMessages1()
100100
{
101101
// Parameters for the API call
102-
SendMessagesRequest body = APIHelper.JsonDeserialize<Models.SendMessagesRequest>("{ \"messages\": [ { \"callback_url\": \"https://my.callback.url.com\", \"content\": \"My first message\", \"destination_number\": \"+61491570156\", \"delivery_report\": true, \"format\": \"SMS\", \"message_expiry_timestamp\": \"2016-11-03T11:49:02.807Z\", \"metadata\": { \"key1\": \"value1\", \"key2\": \"value2\" }, \"scheduled\": \"2016-11-03T11:49:02.807Z\", \"source_number\": \"+61491570157\", \"source_number_type\": \"INTERNATIONAL\" }, { \"callback_url\": \"https://my.callback.url.com\", \"content\": \"My second message\", \"destination_number\": \"+61491570158\", \"delivery_report\": true, \"format\": \"SMS\", \"message_expiry_timestamp\": \"2016-11-03T11:49:02.807Z\", \"metadata\": { \"key1\": \"value1\", \"key2\": \"value2\" }, \"scheduled\": \"2016-11-03T11:49:02.807Z\", \"source_number\": \"+61491570159\", \"source_number_type\": \"INTERNATIONAL\" } ]}");
102+
SendMessagesRequest body = new SendMessagesRequest()
103+
{
104+
Messages = new List<Message>
105+
{
106+
new Message()
107+
{
108+
CallbackUrl = "https://my.callback.url.com",
109+
Content = "My first message",
110+
DestinationNumber = "+61491570156",
111+
DeliveryReport = true,
112+
Format = MessageFormat.SMS,
113+
MessageExpiryTimestamp = new DateTime(2016, 11, 03, 11, 49, 02, DateTimeKind.Utc),
114+
Metadata = new Dictionary<string, string>() {{"key1", "value1"}, {"key2", "value2"}},
115+
Scheduled = new DateTime(2016, 11, 03, 11, 49, 02, DateTimeKind.Utc),
116+
SourceNumber = "+61491570157",
117+
SourceNumberType = NumberType.INTERNATIONAL
118+
},
119+
new Message()
120+
{
121+
CallbackUrl = "https://my.callback.url.com",
122+
Content = "My second message",
123+
DestinationNumber = "+61491570158",
124+
DeliveryReport = true,
125+
Format = MessageFormat.SMS,
126+
MessageExpiryTimestamp = new DateTime(2016, 11, 03, 11, 49, 02, DateTimeKind.Utc),
127+
Metadata = new Dictionary<string, string>() {{"key1", "value1"}, {"key2", "value2"}},
128+
Scheduled = new DateTime(2016, 11, 03, 11, 49, 02, DateTimeKind.Utc),
129+
SourceNumber = "+61491570159",
130+
SourceNumberType = NumberType.INTERNATIONAL
131+
}
132+
}
133+
};
103134

104135
// Perform API call
105136
SendMessagesResponse result = null;
@@ -122,7 +153,7 @@ public async Task TestSendMessages1()
122153
// Test whether the captured response is as we expected
123154
Assert.IsNotNull(result, "Result should exist");
124155

125-
dynamic messages = result.Messages;//JObject.Parse(TestHelper.ConvertStreamToString(httpCallBackHandler.Response.RawBody));
156+
dynamic messages = result.Messages;
126157
int count = (int)messages.Count;
127158

128159
Assert.AreEqual(count, 2);
@@ -134,19 +165,19 @@ public async Task TestSendMessages1()
134165
AssertSendMessageResponseValid(secondMessage, "SMS", "My second message", "https://my.callback.url.com", true, "+61491570158", "+61491570159", "queued");
135166
}
136167

137-
private void AssertSendMessageResponseValid(dynamic message, string expectedFormat, string expectedContent, string expectedCallbackUrl,
168+
private void AssertSendMessageResponseValid(Message message, string expectedFormat, string expectedContent, string expectedCallbackUrl,
138169
bool expectedDeliveryReport, string expectedDestinationNumber, string expectedSourceNumber, string expectedStatus)
139170
{
140-
var format = (string)message.format;
141-
var content = (string)message.content;
142-
var callbackUrl = (string)message.callback_url;
143-
var deliveryReport = (bool)message.delivery_report;
144-
var destinationNumber = (string)message.destination_number;
145-
var sourceNumber = (string)message.source_number;
146-
var status = (string)message.status;
147-
var messageId = (string)message.message_id;
148-
var messageExpiry = (string)message.message_expiry_timestamp;
149-
var scheduled = (string)message.scheduled;
171+
var format = message.Format.ToString();
172+
var content = (string)message.Content;
173+
var callbackUrl = (string)message.CallbackUrl;
174+
var deliveryReport = (bool)message.DeliveryReport;
175+
var destinationNumber = (string)message.DestinationNumber;
176+
var sourceNumber = (string) message.SourceNumber;
177+
var status = (string)message.Status.ToString();
178+
var messageId = (string)message.MessageId;
179+
var messageExpiry = message.MessageExpiryTimestamp;
180+
var scheduled = message.Scheduled;
150181

151182
Assert.AreEqual(format, expectedFormat, "Format should match exactly (string literal match)");
152183
Assert.AreEqual(content, expectedContent, "Content should match exactly (string literal match)");
@@ -158,35 +189,24 @@ private void AssertSendMessageResponseValid(dynamic message, string expectedForm
158189

159190
// note, these are non-deterministic, so we only check for their existence.
160191
Assert.IsNotEmpty(messageId, "Message ID should not be empty.");
161-
Assert.IsNotEmpty(messageExpiry, "Message Expiry should not be empty.");
162-
Assert.IsNotEmpty(scheduled, "Scheduled time should not be empty.");
163-
164-
DateTime date;
165-
bool canParse = DateTime.TryParse(messageExpiry, out date);
166-
167-
Assert.IsTrue(canParse, "Message Expiry must be a valid DateTime");
192+
Assert.IsNotNull(messageExpiry, "Message Expiry should not be empty.");
193+
Assert.IsNotNull(scheduled, "Scheduled time should not be empty.");
168194

169-
canParse = DateTime.TryParse(scheduled, out date);
170-
Assert.IsTrue(canParse, "Scheduled time must be a valid DateTime");
171-
172-
JObject metadata = message.metadata as JObject;
195+
var metadata = message.Metadata;
173196

174197
Assert.IsNotNull(metadata, "Metadata must not be null.");
175198

176199
var metadataCount = metadata.Count;
177200

178201
Assert.AreEqual(metadataCount, 2, "Metadata must have two children.");
179202

180-
var firstKey = ((dynamic)metadata).key1;
181-
var secondKey = ((dynamic)metadata).key2;
182-
183-
Assert.IsNotNull(firstKey, "Metadata must contain key1.");
184-
Assert.IsNotNull(secondKey, "Metadata must contain key2.");
203+
Assert.IsTrue(metadata.ContainsKey("key1"), "Metadata must contain key1.");
204+
Assert.IsTrue(metadata.ContainsKey("key2"), "Metadata must contain key2.");
185205

186-
var firstKeyValue = (string)firstKey;
187-
var secondKeyValue = (string)secondKey;
206+
var firstKeyValue = metadata["key1"];
207+
var secondKeyValue = metadata["key2"];
188208

189-
Assert.AreEqual(firstKeyValue, "value1", "key1 must equal value1.");
209+
Assert.AreEqual(firstKeyValue, "value1", "key1 must equal value1.");
190210
Assert.AreEqual(secondKeyValue, "value2", "key2 must equal value1.");
191211
}
192212
}

README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ It's easy to get started. Simply enter the API Key and secret you obtained from
7575
Destination numbers (`destination_number`) should be in the [E.164](http://en.wikipedia.org/wiki/E.164) format. For example, `+61491570156`.
7676
```csharp
7777
using System;
78+
using System.Linq;
7879
using MessageMedia.Messages;
7980
using MessageMedia.Messages.Controllers;
8081
using MessageMedia.Messages.Models;
8182

83+
8284
namespace TestCSharpSDK
8385
{
8486
class Program
@@ -89,25 +91,24 @@ namespace TestCSharpSDK
8991
String basicAuthUserName = "YOUR_API_KEY";
9092
String basicAuthPassword = "YOUR_API_SECRET";
9193
bool useHmacAuthentication = false; //Change this to true if you are using HMAC keys
92-
94+
9395
// Instantiate the client
9496
MessageMediaMessagesClient client = new MessageMediaMessagesClient(basicAuthUserName, basicAuthPassword, useHmacAuthentication);
9597
IMessagesController messages = client.Messages;
9698

97-
// Perform API call
98-
string bodyValue = @"{
99-
""messages"":[
100-
{
101-
""content"":""Greetings from MessageMedia!"",
102-
""destination_number"":""YOUR_MOBILE_NUMBER""
103-
}
104-
]
105-
}";
106-
107-
var body = Newtonsoft.Json.JsonConvert.DeserializeObject<MessageMedia.Messages.Models.SendMessagesRequest>(bodyValue);
108-
109-
MessageMedia.Messages.Models.SendMessagesResponse result = messages.CreateSendMessages(body);
110-
Console.WriteLine(result.Messages);
99+
var request = new SendMessagesRequest() {
100+
Messages = new []{
101+
new Message() {
102+
Content = "Greetings from MessageMedia!",
103+
DestinationNumber = "YOUR_MOBILE_NUMBER"
104+
}
105+
}
106+
};
107+
108+
SendMessagesResponse result = messages.CreateSendMessages(request);
109+
Message message = result.Messages.First();
110+
111+
Console.WriteLine("Status: {0}, Message Id: {1}", message.Status, message.MessageId);
111112
Console.ReadKey();
112113
}
113114
}
@@ -118,6 +119,7 @@ namespace TestCSharpSDK
118119
Destination numbers (`destination_number`) should be in the [E.164](http://en.wikipedia.org/wiki/E.164) format. For example, `+61491570156`.
119120
```csharp
120121
using System;
122+
using System.Linq;
121123
using MessageMedia.Messages;
122124
using MessageMedia.Messages.Controllers;
123125
using MessageMedia.Messages.Models;
@@ -153,8 +155,8 @@ namespace TestCSharpSDK
153155
}
154156
}
155157
};
156-
157-
Message message = result.Messages.First();
158+
SendMessagesResponse result = messages.CreateSendMessages(request);
159+
Message message = result.Messages.First();
158160

159161
Console.WriteLine("Status: {0}, Message Id: {1}", message.Status, message.MessageId);
160162
Console.ReadKey();
@@ -181,7 +183,7 @@ namespace TestCSharpSDK
181183
String basicAuthUserName = "YOUR_API_KEY";
182184
String basicAuthPassword = "YOUR_API_SECRET";
183185
bool useHmacAuthentication = false; //Change this to true if you are using HMAC keys
184-
186+
185187
// Instantiate the client
186188
MessageMediaMessagesClient client = new MessageMediaMessagesClient(basicAuthUserName, basicAuthPassword, useHmacAuthentication);
187189
IMessagesController messages = client.Messages;
@@ -214,7 +216,7 @@ namespace TestCSharpSDK
214216
String basicAuthUserName = "YOUR_API_KEY";
215217
String basicAuthPassword = "YOUR_API_SECRET";
216218
bool useHmacAuthentication = false; //Change this to true if you are using HMAC keys
217-
219+
218220
// Instantiate the client
219221
MessageMediaMessagesClient client = new MessageMediaMessagesClient(basicAuthUserName, basicAuthPassword, useHmacAuthentication);
220222
IRepliesController replies = client.Replies;
@@ -246,7 +248,7 @@ namespace TestCSharpSDK
246248
String basicAuthUserName = "YOUR_API_KEY";
247249
String basicAuthPassword = "YOUR_API_SECRET";
248250
bool useHmacAuthentication = false; //Change this to true if you are using HMAC keys
249-
251+
250252
// Instantiate the client
251253
MessageMediaMessagesClient client = new MessageMediaMessagesClient(basicAuthUserName, basicAuthPassword, useHmacAuthentication);
252254
IDeliveryReportsController deliveryReports = client.DeliveryReports;
@@ -267,4 +269,4 @@ Check out the [full API documentation](https://developers.messagemedia.com/code/
267269
Please contact developer support at [email protected] or check out the developer portal at [developers.messagemedia.com](https://developers.messagemedia.com/)
268270

269271
## :page_with_curl: License
270-
Apache License. See the [LICENSE](LICENSE) file.
272+
Apache License. See the [LICENSE](LICENSE) file.

0 commit comments

Comments
 (0)