-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d91fd89
commit 3625bff
Showing
8 changed files
with
194 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,28 +9,28 @@ public class IdentityTests : AuthorizationClass | |
[Fact] | ||
public async Task IdentifyAsyncFailsIfNotGivenIdentity() | ||
{ | ||
var customerIo = new CustomerIo(SiteId, ApiKey); | ||
var customerIo = new TrackApi(SiteId, ApiKey); | ||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await customerIo.IdentifyAsync()); | ||
} | ||
|
||
[Fact] | ||
public async Task IdentifyAsyncSucceedsWithStaticIdentity() | ||
{ | ||
var customerIo = new CustomerIo(SiteId, ApiKey); | ||
var customerIo = new TrackApi(SiteId, ApiKey); | ||
await customerIo.IdentifyAsync(new CustomerDetails("from_static_identity", "[email protected]")); | ||
} | ||
|
||
[Fact] | ||
public async Task IdentifyAsyncSucceedsWithIdentityFactory() | ||
{ | ||
var customerIo = new CustomerIo(SiteId, ApiKey, new IdentityFactory()); | ||
var customerIo = new TrackApi(SiteId, ApiKey, new IdentityFactory()); | ||
await customerIo.IdentifyAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task IdentifyAsyncSucceedsWithIdentityFactoryAndCustomCustomerDetails() | ||
{ | ||
var customerIo = new CustomerIo(SiteId, ApiKey, new IdentityFactoryWithExtraCustomerDetails()); | ||
var customerIo = new TrackApi(SiteId, ApiKey, new IdentityFactoryWithExtraCustomerDetails()); | ||
await customerIo.IdentifyAsync(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace CustomerIOSharp | ||
{ | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
public class AppApi | ||
{ | ||
private const string ApiEndpoint = "https://api.customer.io/v1/api"; | ||
|
||
private readonly HttpClient _httpClient; | ||
|
||
public AppApi(string appApiKey) | ||
{ | ||
_httpClient = new HttpClient(); | ||
|
||
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {appApiKey}"); | ||
} | ||
|
||
/// <summary> | ||
/// Track a custom event for a non-customer. | ||
/// </summary> | ||
/// <see cref="https://learn.customer.io/api/#apibroadcast_trigger" /> | ||
/// <param name="campaignId">The Campaign Id you wish to trigger</param> | ||
/// <param name="data">Any related information you’d like to attach to this broadcast. These attributes can be used in the email/ action body of the triggered email. You can set any number of data key and values.</param> | ||
/// <param name="recipientFilter">Allows you to pass in filters that will override any preset segment or recipient criteria. </param> | ||
/// <see cref="https://learn.customer.io/documentation/api-triggered-broadcast-setup.html#step-1-define-recipients" /> | ||
/// <returns>Nothing if successful, throws if failed</returns> | ||
/// <exception cref="CustomerIoApiException">If any code besides 200 OK is returned from the server.</exception> | ||
public async Task TriggerBroadcastAsync(int campaignId, object data = null, object recipientFilter = null) | ||
{ | ||
var wrappedData = new TriggerBroadcast | ||
{ | ||
Data = data, | ||
Recipients = recipientFilter | ||
}; | ||
|
||
var resource = $"{ApiEndpoint}/campaigns/{campaignId.ToString()}/triggers"; | ||
|
||
await Utilities.CallMethodAsync( | ||
_httpClient, | ||
resource, | ||
HttpMethod.Post, | ||
wrappedData).ConfigureAwait(false); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace CustomerIOSharp | ||
{ | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
internal static class Utilities | ||
{ | ||
static Utilities() | ||
{ | ||
var settings = new JsonSerializerSettings() | ||
{ | ||
MissingMemberHandling = MissingMemberHandling.Ignore, | ||
NullValueHandling = NullValueHandling.Ignore, | ||
DefaultValueHandling = DefaultValueHandling.Include, | ||
ContractResolver = new CamelCasePropertyNamesContractResolver() | ||
}; | ||
|
||
foreach (var converter in settings.Converters.OfType<DateTimeConverterBase>().ToList()) | ||
{ | ||
settings.Converters.Remove(converter); | ||
} | ||
|
||
settings.Converters.Add(new UnixTimestampConverter()); | ||
|
||
JsonSerializerSettings = settings; | ||
} | ||
|
||
internal static JsonSerializerSettings JsonSerializerSettings { get; } | ||
|
||
internal static async Task CallMethodAsync(HttpClient client, string resource, HttpMethod httpMethod, object data) | ||
{ | ||
|
||
var requestMessage = new HttpRequestMessage(httpMethod, resource) | ||
{ | ||
Content = new StringContent( | ||
JsonConvert.SerializeObject(data, Utilities.JsonSerializerSettings), | ||
Encoding.UTF8, | ||
"application/json") | ||
}; | ||
var result = await client.SendAsync(requestMessage).ConfigureAwait(false); | ||
if (result.StatusCode != HttpStatusCode.OK) | ||
{ | ||
throw new CustomerIoApiException(result.StatusCode, result.ReasonPhrase); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.