コード例 #1
0
 async Task <HttpResponseMessage> SendMessageAsync(RequestHeaderSdkMessage message, HttpClient httpClient)
 {
     using (var httpRequest = CreateHttpRequest(message))
     {
         return(await httpClient.SendAsync(httpRequest));
     }
 }
コード例 #2
0
 public void EnqueueMessage(RequestHeaderSdkMessage message)
 {
     lock (messages)
     {
         messages.Enqueue(message);
         while (messages.Count > MaximumQueueSize)
         {
             messages.Dequeue();
         }
     }
 }
コード例 #3
0
        public void TestUploadSuccess()
        {
            EventsApiClient         apiManager = new EventsApiClient(ApiKey, ApiSecret);
            RequestHeaderSdkMessage message    = new RequestHeaderSdkMessage();

            message.DeviceInfo = new DeviceInfo();
            message.ClientMpId = 123;
            message.AppInfo    = new AppInfo();
            apiManager.EventsApiUploadSuccess += (request, response) => { Assert.IsFalse(response == null); };
            apiManager.EventsApiUploadFailure += (request, exception) => { Assert.Fail("Failed:" + exception.Error); };
            apiManager.EnqueueMessage(message);
            apiManager.DispatchAsync().Wait();
        }
コード例 #4
0
        public void TestBadApiKey()
        {
            EventsApiClient         apiManager = new EventsApiClient("foo bad key", "foo bad secret");
            RequestHeaderSdkMessage message    = new RequestHeaderSdkMessage();

            message.DeviceInfo = new DeviceInfo();
            message.ClientMpId = 123;
            message.AppInfo    = new AppInfo();
            apiManager.EventsApiUploadSuccess      += (request, successargs) => { Assert.Fail(); };
            apiManager.EventsApiUploadFailure      += (request, failureargs) => { Assert.Fail(); };
            apiManager.EventsApiUploadUnsuccessful += (request, badrequestargs) => {
                Assert.IsNotNull(request);
                Assert.AreEqual(400, badrequestargs.HttpStatusCode);
            };
            apiManager.EnqueueMessage(message);
            apiManager.DispatchAsync().Wait();
        }
コード例 #5
0
 async Task Upload(RequestHeaderSdkMessage upload, HttpClient httpClient)
 {
     try
     {
         using (var response = await SendMessageAsync(upload, httpClient))
         {
             if (response.IsSuccessStatusCode)
             {
                 await OnMessageSentAsync(upload, response);
             }
             else
             {
                 OnUnsuccessfulApiRequest(upload, response);
             }
         }
     }
     catch (Exception ex)
     {
         OnApiFailure(upload, ex);
     }
 }
コード例 #6
0
        private RequestHeaderSdkMessage CreateBatch(SdkMessage[] messages)
        {
            var batch = new RequestHeaderSdkMessage()
            {
                Messages               = messages,
                Timestamp              = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
                AppInfo                = appInfo,
                DeviceInfo             = deviceInfo,
                ClientMpId             = MParticle.Instance.Identity.CurrentUser?.Mpid,
                DeviceApplicationStamp = Guid.Parse(persistenceManager.DeviceApplicationStamp),
                Debug      = MParticle.Instance.Options.DevelopmentMode,
                SdkVersion = MParticle.SdkVersion
            };
            var user = MParticle.Instance.Identity.CurrentUser;

            if (user != null)
            {
                batch.UserIdentities = persistenceManager.UserIdentities(user.Mpid).ToArray();
                batch.UserAttributes = persistenceManager.UserAttributes(user.Mpid);
            }
            return(batch);
        }
コード例 #7
0
        private HttpRequestMessage CreateHttpRequest(RequestHeaderSdkMessage batchRequest)
        {
            String message = JsonConvert.SerializeObject(batchRequest, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            this.Logger?.Log(new LogEntry(LoggingEventType.Debug, "Performing upload:\n" + message));
            var request = new HttpRequestMessage()
            {
                RequestUri = eventsEndpointUri,
                Method     = HttpMethod.Post,
                Content    = new StringContent(message, Encoding.UTF8, "application/json")
            };

            if (!string.IsNullOrEmpty(UserAgent))
            {
                request.Headers.UserAgent.ParseAdd(UserAgent);
            }

            AddSignature(request, message, eventsEndpointUri.PathAndQuery, apiSecret);

            return(request);
        }
コード例 #8
0
 internal UnsuccessfulApiRequestEventArgs(RequestHeaderSdkMessage upload, int statusCode)
 {
     HttpStatusCode          = statusCode;
     RequestHeaderSdkMessage = upload;
 }
コード例 #9
0
 internal EventsApiSuccessEventArgs(RequestHeaderSdkMessage upload, string response)
 {
     Response = response;
     RequestHeaderSdkMessage = upload;
 }
コード例 #10
0
 internal EventsApiFailureEventArgs(RequestHeaderSdkMessage upload, Exception error)
 {
     Error = error;
     RequestHeaderSdkMessage = upload;
 }
コード例 #11
0
 async Task OnMessageSentAsync(RequestHeaderSdkMessage upload, HttpResponseMessage response)
 {
     this.Logger?.Log(new LogEntry(LoggingEventType.Debug, "Upload succeeded: " + response.StatusCode));
     EventsApiUploadSuccess?.Invoke(this, new EventsApiSuccessEventArgs(upload, await response.Content.ReadAsStringAsync()));
 }
コード例 #12
0
 void OnApiFailure(RequestHeaderSdkMessage upload, Exception exception)
 {
     this.Logger?.Log(new LogEntry(LoggingEventType.Debug, "Failed Api Request:\n" + exception.Message));
     EventsApiUploadFailure?.Invoke(this, new EventsApiFailureEventArgs(upload, exception));
 }
コード例 #13
0
 void OnUnsuccessfulApiRequest(RequestHeaderSdkMessage upload, HttpResponseMessage response)
 {
     this.Logger?.Log(new LogEntry(LoggingEventType.Debug, "Bad Api Request:\n" + response.StatusCode));
     EventsApiUploadUnsuccessful?.Invoke(this, new UnsuccessfulApiRequestEventArgs(upload, (int)response.StatusCode));
 }