Skip to content

Instantly share code, notes, and snippets.

@jmglov
Created February 25, 2015 16:58
Show Gist options
  • Save jmglov/c8cf2ef268dede96ca69 to your computer and use it in GitHub Desktop.
Save jmglov/c8cf2ef268dede96ca69 to your computer and use it in GitHub Desktop.
Amazon Mobile Analytics client for Unity, WIP
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using UnityEngine;
using Amazon.CognitoIdentity;
using Amazon.Common;
using Amazon.Runtime;
using Amazon.Runtime.Internal;
using Amazon.Runtime.Internal.Auth;
using Amazon.Runtime.Internal.Transform;
using Amazon.Runtime.Internal.Util;
using Amazon.Unity;
using ThirdParty.Json.LitJson;
namespace Amazon.MobileAnalytics {
public class MobileAnalyticsManager {
private AmazonMobileAnalyticsClient client;
private EventClient eventClient;
public MobileAnalyticsManager() {
if (!AmazonInitializer.IsInitialized)
throw new Exception("AmazonInitializer is not added to the scene");
AmazonLogging.EnableSDKLogging = true;
Debug.Log("Using Cognito pool " + AmazonInitializer.IdentityPoolId + " in region " + AmazonInitializer.CognitoRegionEndpoint);
CognitoAWSCredentials credentials = new CachingCognitoAWSCredentials(AmazonInitializer.AmazonAccountId,
AmazonInitializer.IdentityPoolId,
AmazonInitializer.DefaultUnAuthRole,
AmazonInitializer.DefaultAuthRole,
AmazonInitializer.CognitoRegionEndpoint);
client = new AmazonMobileAnalyticsClient(credentials, AmazonInitializer.CognitoRegionEndpoint);
eventClient = new EventClient(client);
}
public EventClient GetEventClient() {
return eventClient;
}
}
public partial class AmazonMobileAnalyticsClient : AmazonWebServiceClient {
AWS4Signer signer = new AWS4Signer();
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
}
public AmazonMobileAnalyticsClient(AWSCredentials credentials, RegionEndpoint region)
: base(credentials, new AmazonMobileAnalyticsConfig(region), AuthenticationTypes.User | AuthenticationTypes.Session) {}
public void PutEventsAsync(PutEventsRequest request, AmazonServiceCallback callback, object state) {
if (!AmazonInitializer.IsInitialized)
throw new Exception("AmazonInitializer is not added to the scene");
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
var marshaller = new PutEventsRequestMarshaller();
var unmarshaller = PutEventsResponseUnmarshaller.GetInstance();
Invoke(request, callback, state, marshaller, unmarshaller, signer);
}));
Debug.Log("Enqueuing Mobile Analytics request");
}
}
public class AmazonMobileAnalyticsConfig : ClientConfig {
public AmazonMobileAnalyticsConfig(RegionEndpoint region) : base() {
RegionEndpoint = region;
}
internal override string RegionEndpointServiceName {
get { return "mobileanalytics"; }
}
public override string ServiceVersion {
get { return "2014-06-05"; }
}
}
public partial class PutEventsRequest : AmazonWebServiceRequest {}
public partial class PutEventsResponse : AmazonWebServiceResponse {}
public class PutEventsRequestMarshaller : IMarshaller<IRequest, PutEventsRequest> {
public IRequest Marshall(PutEventsRequest putEventsRequest) {
IRequest request = new DefaultRequest(putEventsRequest, "Amazon.MobileAnalytics");
request.Headers["Content-Type"] = "application/json";
request.HttpMethod = "POST";
request.ResourcePath = "/2014-06-05/events";
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture)) {
var writer = new JsonWriter(stringWriter);
writer.WriteObjectStart();
writer.WriteObjectEnd();
var content = stringWriter.ToString();
request.Content = System.Text.Encoding.UTF8.GetBytes(content);
Debug.Log("Sending Mobile Analytics request: " + content);
}
return request;
}
}
public class PutEventsResponseUnmarshaller : JsonResponseUnmarshaller {
public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) {
Debug.Log("Got Mobile Analytics response");
var response = new PutEventsResponse();
return response;
}
private static PutEventsResponseUnmarshaller _instance = new PutEventsResponseUnmarshaller();
internal static PutEventsResponseUnmarshaller GetInstance() {
return _instance;
}
public static PutEventsResponseUnmarshaller Instance {
get { return _instance; }
}
}
public class EventClient {
private AmazonMobileAnalyticsClient client;
public EventClient(AmazonMobileAnalyticsClient client) {
this.client = client;
}
public void RecordEvent(AnalyticsEvent analyticsEvent) {
client.PutEventsAsync(new PutEventsRequest(), (AmazonServiceResult result) => {
try {
if (result.Exception != null) {
Debug.Log(result.Exception);
return;
}
var response = result.Response as PutEventsResponse;
Debug.Log("Got a response from PutEvents! " + response);
} catch(Exception e) {
Debug.LogException(e);
}
}, null);
}
}
public class AnalyticsEvent {
public void AddAttribute(string name, string value) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment