Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
youngj committed Nov 11, 2014
0 parents commit aa8d7dd
Show file tree
Hide file tree
Showing 23 changed files with 3,570 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2014 Telerivet, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
93 changes: 93 additions & 0 deletions README.md
@@ -0,0 +1,93 @@
.NET client library for Telerivet REST API

http://telerivet.com/api

https://www.nuget.org/packages/Telerivet.NET/

Overview
--------
This library makes it easy to integrate your .NET application with Telerivet.
You can use it to:

- send SMS messages via an Android phone or SMS gateway service
- update contact information in Telerivet (e.g. from a signup form on your own website)
- add or remove contacts from groups
- export your message/contact data from Telerivet into your own systems
- schedule messages to be sent at a later time
- control automated services
- much more

All API methods are fully documented at https://telerivet.com/api/rest/dotnet ,
as well as in the comments of the C# source files.

System Requirements
-------------------
.NET framework 4.5 or higher

Installation
------------
To install the library, run the following command in the Package Manager Console in Visual Studio:

PM> Install-Package Telerivet.NET -Version 1.1.1


Example Usage
-------------

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerivet.Client;
using Newtonsoft.Json.Linq;
namespace ExampleNamespace
{
public class ExampleClass
{
public async Task foo()
{
TelerivetAPI tr = new TelerivetAPI("YOUR_API_KEY");
Project project = tr.InitProjectById("PROJECT_ID");
// send message
Message sent_msg = await project.SendMessageAsync(Util.Options(
"content", "hello world",
"to_number", "+16505550123"
));
// import contact and add to group
Contact contact = await project.GetOrCreateContactAsync(Util.Options(
"name", "John Smith",
"phone_number", "5550123",
"vars", Util.Options("email", "jsmith@example.com", "birthdate", "1953-01-04")
));
Group group = await project.GetOrCreateGroupAsync("Subscribers");
await contact.AddToGroupAsync(group);
// query contact information
String namePrefix = "John";
APICursor<Contact> query = project.QueryContacts(Util.Options(
"name", Util.Options("prefix", namePrefix),
"sort", "name"
));
int count = await query.CountAsync();
List<Contact> contacts = await query.Limit(50).AllAsync();
Console.WriteLine(count + " contacts matching " + namePrefix + ":");
foreach (Contact c in contacts)
{
Console.WriteLine(c.Name + " " + c.PhoneNumber + " " + c.Vars.Get("birthdate"));
}
}
}
}
```
16 changes: 16 additions & 0 deletions Telerivet.nuspec
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Telerivet.NET</id>
<version>1.1.1</version>
<authors>youngj</authors>
<projectUrl>https://telerivet.com/api</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>.NET library for Telerivet REST API.</description>
<tags>SMS</tags>
<dependencies>
<dependency id="Newtonsoft.Json" version="6.0.6" />
</dependencies>
</metadata>
<files />
</package>
22 changes: 22 additions & 0 deletions TelerivetAPIClient.sln
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2013 for Web
VisualStudioVersion = 12.0.30723.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelerivetAPIClient", "TelerivetAPIClient\TelerivetAPIClient.csproj", "{BF4A8E93-F744-4EFA-8398-94D5A6726F0F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BF4A8E93-F744-4EFA-8398-94D5A6726F0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF4A8E93-F744-4EFA-8398-94D5A6726F0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF4A8E93-F744-4EFA-8398-94D5A6726F0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF4A8E93-F744-4EFA-8398-94D5A6726F0F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
137 changes: 137 additions & 0 deletions TelerivetAPIClient/APICursor.cs
@@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Reflection;

namespace Telerivet.Client
{
public class APICursor<T> where T : Entity
{
private int? limit = null;
private String nextMarker;
private JArray data;
private bool truncated;
int pos = -1;
int offset = 0;

private int? count = null;

private TelerivetAPI api;
private string path;
private JObject parameters;
private ConstructorInfo ctor;

public APICursor(TelerivetAPI api, string path, JObject parameters)
{
if (parameters == null)
{
parameters = new JObject();
}

if (parameters["count"] != null)
{
throw new ArgumentException("Cannot construct APICursor with 'count' parameter. Call the count() method instead.");
}

this.api = api;
this.path = path;
this.parameters = parameters;

this.ctor = typeof(T).GetConstructor(new Type[] {
typeof(TelerivetAPI),
typeof(JObject),
typeof(bool)
});
}

public APICursor<T> Limit(int limit)
{
this.limit = limit;
return this;
}

public async Task<int> CountAsync()
{
if (count == null)
{
JObject requestParams = new JObject(parameters);
requestParams["count"] = 1;
JObject res = (JObject) await api.DoRequestAsync("GET", path, requestParams);
count = (int) res["count"];
}
return count.Value;
}

public async Task<List<T>> AllAsync()
{
List<T> res = new List<T>();

while (true)
{
T item = await NextAsync();
if (item == null)
{
break;
}
res.Add(item);
}

return res;
}

public async Task<T> NextAsync()
{
if (limit != null && offset >= limit)
{
return null;
}

if (data == null || (pos >= data.Count && truncated))
{
await loadNextPage();
}

if (pos < data.Count)
{
JObject itemData = (JObject) data[pos];
pos += 1;
offset += 1;

return (T)ctor.Invoke(new object[] { api, itemData, true });
}
else
{
return null;
}
}

private async Task loadNextPage()
{
JObject requestParams = new JObject(parameters);

if (nextMarker != null)
{
requestParams["marker"] = nextMarker;
}


if (limit != null && requestParams["page_size"] == null)
{
requestParams["page_size"] = Math.Min(limit.Value, 200);
}

JObject response = (JObject) await api.DoRequestAsync("GET", path, requestParams);

data = (JArray) response["data"];
truncated = (bool) response["truncated"];
nextMarker = (String) response["next_marker"];

pos = 0;
}

}
}

0 comments on commit aa8d7dd

Please sign in to comment.