Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Groups functionality #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 83 additions & 7 deletions samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Net;
using com.esendex.sdk.contacts;
using com.esendex.sdk.groups;
using com.esendex.sdk.inbox;
using com.esendex.sdk.messaging;
using com.esendex.sdk.sent;
Expand Down Expand Up @@ -99,23 +101,28 @@ static void Main(string[] args)
Console.WriteLine("Contacts Example\r\n");
GetContactsExample(credentials);

Console.WriteLine();
Console.WriteLine("Groups Example\r\n");
GetGroupsExample(credentials);

Console.WriteLine();
Console.WriteLine("Contacts in Group Example\r\n");
GetContactsByGroupExample(credentials);

AddContactToGroup(credentials);

Console.WriteLine();
Console.WriteLine("Press enter to continue ... ");
Console.ReadLine();
}

private static void ShowUsage(OptionSet optionSet)
{
Console.WriteLine(
@"
Esendex .Net SDK Samples
");
Console.WriteLine(@"Esendex .Net SDK Samples");

optionSet.WriteOptionDescriptions(Console.Out);

Console.WriteLine(@"
Enjoy...
");
Console.WriteLine(@"Enjoy...");
}

private static void SendMessageExample(EsendexCredentials credentials)
Expand Down Expand Up @@ -223,5 +230,74 @@ private static void GetContactsExample(EsendexCredentials credentials)
Console.Write(ex.Message);
}
}

private static void GetGroupsExample(EsendexCredentials credentials)
{
var groupService = new GroupService(credentials);

try
{
var collection = groupService.GetGroups(_accountReference, PageIndex, PageSize);

foreach (var item in collection.Groups)
{
Console.WriteLine("\tGroup Id:{0}\tName:{1}", item.Id, item.Name);
}
}
catch (WebException ex)
{
Console.Write(ex.Message);
}
}

private static void GetContactsByGroupExample(EsendexCredentials credentials)
{
var groupService = new GroupService(credentials);

try
{
var collection = groupService.GetGroups(_accountReference, PageIndex, PageSize);
var contacts = new PagedContactCollection();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be inlined with the contact creation.


var groupId = "";

foreach (var item in collection.Groups.Where(item => item.Name == "Test group"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the group name is unique you can use FirstOrDefault with null check to verify if a group has been found.

var contactGroup = collection.Groups.FirstOrDefault(x => x.Name == "Test group");

if (contactGroup == null)
   return;

{
groupId = item.Id.ToString();
break;
}

if (groupId == "") return;

contacts = groupService.GetContactsFromGroup(_accountReference, groupId, 1, 15);

foreach (var item in contacts.Contacts)
{
Console.WriteLine("\tContact Id:{0}\tNumber:{1}", item.Id, item.PhoneNumber);
}
}
catch (WebException ex)
{
Console.Write(ex.Message);
}
}

private static void AddContactToGroup(EsendexCredentials credentials)
{
var groupService = new GroupService(credentials);
var contactService = new ContactService(credentials);

try
{
var guid = new Guid("{YOUR Contact GUID}");
var contact = contactService.GetContact(guid);
groupService.AddContactToGroup(_accountReference, "{YOUR Group GUID}", contact);

}
catch (WebException ex)
{
Console.Write(ex.Message);
}
}
}
}
2 changes: 0 additions & 2 deletions source/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ internal static Uri API_URI
{
get { return api_uri ?? (api_uri = new UriBuilder("https", "api.esendex.com").Uri); }
}

internal static string JSON_MEDIA_TYPE = "application/json; charset=utf-8";
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs re-instating along with the file includes

6 changes: 0 additions & 6 deletions source/EsendexCredentials.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Net;
using System.Text;

namespace com.esendex.sdk
{
Expand Down Expand Up @@ -105,10 +104,5 @@ private void SetProxy(IWebProxy proxy)

WebProxy = proxy;
}

internal string EncodedValue()
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(UseSessionAuthentication ? SessionId.Value.ToString() : Username + ":" + Password));
}
}
}
4 changes: 2 additions & 2 deletions source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("2.2.0")]
[assembly: AssemblyFileVersion("2.2.0")]
[assembly: AssemblyVersion("2.0.1")]
[assembly: AssemblyFileVersion("2.0.1")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We follow SEMVER so this will be 2.3.0 as it's a non-breaking feature enhancement

[assembly: InternalsVisibleTo("com.esendex.sdk.test")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
28 changes: 7 additions & 21 deletions source/com.esendex.sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -48,9 +46,6 @@
<DocumentationFile>bin\Release\com.esendex.sdk.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand All @@ -67,8 +62,12 @@
<Compile Include="core\CharacterSet.cs" />
<Compile Include="core\MessageCollection.cs" />
<Compile Include="core\PagedCollection.cs" />
<Compile Include="exceptions\BadRequestException.cs" />
<Compile Include="extensions\HttpWebResponseExtensions.cs" />
<Compile Include="groups\Group.cs" />
<Compile Include="groups\GroupCollection.cs" />
<Compile Include="groups\GroupResponse.cs" />
<Compile Include="groups\GroupService.cs" />
<Compile Include="groups\IGroupService.cs" />
<Compile Include="groups\PagedGroupCollection.cs" />
<Compile Include="http\HttpClient.cs" />
<Compile Include="http\HttpRequest.cs" />
<Compile Include="http\HttpRequestHelper.cs" />
Expand Down Expand Up @@ -97,10 +96,8 @@
<Compile Include="inbox\InboxService.cs" />
<Compile Include="messaging\MessageBodyService.cs" />
<Compile Include="messaging\VoiceMessageLanguage.cs" />
<Compile Include="models\requests\SurveysAddRecipientRequest.cs" />
<Compile Include="models\requests\SurveysAddRecipientsRequest.cs" />
<Compile Include="Request.cs" />
<Compile Include="rest\resources\ContactsResource.cs" />
<Compile Include="rest\resources\GroupsResource.cs" />
<Compile Include="rest\resources\InboxMessagesResource.cs" />
<Compile Include="rest\resources\MessageHeadersResource.cs" />
<Compile Include="core\IPagedCollection.cs" />
Expand All @@ -109,9 +106,6 @@
<Compile Include="messaging\MessageCollection.cs" />
<Compile Include="rest\resources\ResourceLinkResource.cs" />
<Compile Include="rest\resources\SessionResource.cs" />
<Compile Include="sent\FailureReason.cs" />
<Compile Include="results\Error.cs" />
<Compile Include="results\SurveyResult.cs" />
<Compile Include="sent\SentMessage.cs" />
<Compile Include="messaging\SmsMessage.cs" />
<Compile Include="messaging\SmsMessageCollection.cs" />
Expand All @@ -125,7 +119,6 @@
<Compile Include="session\ISessionService.cs" />
<Compile Include="session\EsendexSession.cs" />
<Compile Include="session\SessionService.cs" />
<Compile Include="surveys\SurveysService.cs" />
<Compile Include="utilities\ISerialiser.cs" />
<Compile Include="rest\resources\MessageDispatcherResource.cs" />
<Compile Include="rest\RestClient.cs" />
Expand All @@ -136,12 +129,6 @@
<Compile Include="messaging\MessagingService.cs" />
<Compile Include="ServiceBase.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="builders\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand All @@ -154,5 +141,4 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
</Project>
17 changes: 17 additions & 0 deletions source/contacts/ContactCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public ContactCollection(Contact contact)
Items.Add(contact);
}

/// <summary>
/// Initialises a new instance of the com.esendex.sdk.contacts.ContactCollection
/// </summary>
/// <param name="id">A com.esendex.sdk.contacts.Contact.Id instance that contains a contacts guid.</param>
public ContactCollection(string id)
{
if (id == null) throw new ArgumentNullException("contact");

ItemsId.Add(id);
}

/// <summary>
/// Initialises a new instance of the com.esendex.sdk.contacts.ContactCollection
/// </summary>
Expand All @@ -46,6 +57,12 @@ public ContactCollection(IEnumerable<Contact> contacts)
/// </summary>
[XmlElement("contact")] public List<Contact> Items = new List<Contact>();

/// <summary>
/// <![CDATA[A System.Collections.Generic.List<com.esendex.sdk.contacts.Contact.Id> instance that contains the contacts guid.]]>
/// </summary>
[XmlElement("contactid")]
public List<string> ItemsId = new List<string>();

/// <summary>
/// Determines whether the specified System.Object are considered equal.
/// </summary>
Expand Down
83 changes: 83 additions & 0 deletions source/groups/Group.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Xml.Serialization;

namespace com.esendex.sdk.groups
{
/// <summary>
/// Represents a contact.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be amended to groups.

/// </summary>
[Serializable]
[XmlRoot("contactgroup", Namespace = Constants.API_NAMESPACE)]
public class Group
{
/// <summary>
/// Initialises a new instance of the com.esendex.sdk.groups.group
/// </summary>
/// <param name="name">A System.String instance that contains the quick name.</param>
/// <exception cref="System.ArgumentNullException"></exception>
public Group(string accountReference, string name)
: this()
{
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

AccountReference = accountReference;
Name = name;
}

/// <summary>
/// Initialises a new instance of the com.esendex.sdk.groups.group
/// </summary>
public Group()
{
}

/// <summary>
/// Gets or sets a the Id.
/// </summary>
[XmlAttribute("id")]
public Guid Id { get; set; }

public bool ShouldSerializeId()
{
return Id != Guid.Empty;
}

/// <summary>
/// Gets or sets the name.
/// </summary>
[XmlElement("name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the account reference.
/// </summary>
[XmlElement("accountreference")]
public string AccountReference { get; set; }

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Group) obj);
}

protected bool Equals(Group other)
{
return Id.Equals(other.Id)
&& string.Equals(Name, other.Name)
&& string.Equals(AccountReference, other.AccountReference);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = Id.GetHashCode();
hashCode = (hashCode*397) ^ (Name != null ? Name.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (AccountReference != null ? AccountReference.GetHashCode() : 0);
return hashCode;
}
}
}
}
Loading