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

Add support for EU data center #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 12 additions & 6 deletions AmplitudeSharp/AmplitudeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public bool OfflineMode
/// </summary>
public Dictionary<string, object> ExtraEventProperties { get; private set; } = new Dictionary<string, object>();

private AmplitudeService(string apiKey)
private AmplitudeService(string apiKey, string apiRegion)
{
lockObject = new object();
api = new AmplitudeApi(apiKey);
api = new AmplitudeApi(apiKey, apiRegion);
eventQueue = new List<IAmplitudeEvent>();
cancellationToken = new CancellationTokenSource();
eventsReady = new SemaphoreSlim(0);
Expand All @@ -70,28 +70,34 @@ private AmplitudeService(string apiKey)
}

public void Dispose()
{
{
Uninitialize();
s_instance = null;
}

/// <summary>
/// Initialize AmplitudeSharp
/// Takes an API key for the project and, optionally,
/// Takes an API key for the project and, optionally,
/// a stream where offline/past events are stored
/// </summary>
/// <param name="apiKey">api key for the project to stream data to</param>
/// <param name="region">Amplitude region to use</param>
/// <param name="persistenceStream">optinal, stream with saved event data <seealso cref="Uninitialize(Stream)"/></param>
/// <param name="logger">Action delegate for logging purposes, if none is specified <see cref="System.Diagnostics.Debug.WriteLine(object)"/> is used</param>
/// <returns></returns>
public static AmplitudeService Initialize(string apiKey, Action<LogLevel, string> logger = null, Stream persistenceStream = null)
public static AmplitudeService Initialize(string apiKey, string apiRegion = "us", Action<LogLevel, string> logger = null, Stream persistenceStream = null)
{
if (apiKey == "<YOUR_API_KEY>")
{
throw new ArgumentOutOfRangeException(nameof(apiKey), "Please specify Amplitude API key");
}

AmplitudeService instance = new AmplitudeService(apiKey);
if (apiRegion == null)
{
throw new ArgumentOutOfRangeException(nameof(apiRegion), "Please specify the Amplitude region to use");
}

AmplitudeService instance = new AmplitudeService(apiKey, apiRegion);
instance.NewSession();

if (Interlocked.CompareExchange(ref s_instance, instance, null) == null)
Expand Down
15 changes: 11 additions & 4 deletions AmplitudeSharp/Api/AmplitudeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public enum SendResult
class AmplitudeApi : IAmplitudeApi
{
private string apiKey;
private string apiRegion;

private HttpClient httpClient;
private HttpClientHandler httpHandler;

public AmplitudeApi(string apiKey)
public AmplitudeApi(string apiKey, string apiRegion)
{
this.apiKey = apiKey;
this.apiRegion = apiRegion;

httpHandler = new HttpClientHandler();
httpHandler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
Expand All @@ -49,9 +52,9 @@ public void ConfigureProxy(string proxyUserName, string proxyPassword)
httpHandler.Proxy = WebRequest.GetSystemWebProxy();
}
else
{
{
httpHandler.Proxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword);
}
}
}

public override Task<SendResult> Identify(AmplitudeIdentify identification)
Expand Down Expand Up @@ -83,9 +86,13 @@ private async Task<SendResult> DoApiCall(string endPoint, string paramName, stri
var data = new StringContent(paramData, UTF8Encoding.UTF8, "application/json");
content.Add(data, paramName);

var apiURL = "https://api.amplitude.com";
if (apiRegion == "eu")
apiURL = "https://api.eu.amplitude.com";

try
{
var postResult = await httpClient.PostAsync($"https://api.amplitude.com/{endPoint}", content);
var postResult = await httpClient.PostAsync($"{apiURL}/{endPoint}", content);

if (postResult.StatusCode >= HttpStatusCode.InternalServerError)
{
Expand Down
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ public partial class App : Application

protected override void OnStartup(StartupEventArgs e)
{
// Only call this once per lifetime of the object
analytics = AmplitudeService.Initialize("<YOUR API KEY>");
// Only call this once per lifetime of the object. The constructor defines
// an optional API region parameter that defines the region (data center)
// to use. Defaults to US
// Set the region to "eu" for the European data center
analytics = AmplitudeService.Initialize("<YOUR API KEY>", "(optional) <AMPLITUDE REGION>");

base.OnStartup(e);
}
Expand Down