async Task <HttpResponseMessage> SendMessageAsync(RequestHeaderSdkMessage message, HttpClient httpClient) { using (var httpRequest = CreateHttpRequest(message)) { return(await httpClient.SendAsync(httpRequest)); } }
public void EnqueueMessage(RequestHeaderSdkMessage message) { lock (messages) { messages.Enqueue(message); while (messages.Count > MaximumQueueSize) { messages.Dequeue(); } } }
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(); }
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(); }
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); } }
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); }
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); }
internal UnsuccessfulApiRequestEventArgs(RequestHeaderSdkMessage upload, int statusCode) { HttpStatusCode = statusCode; RequestHeaderSdkMessage = upload; }
internal EventsApiSuccessEventArgs(RequestHeaderSdkMessage upload, string response) { Response = response; RequestHeaderSdkMessage = upload; }
internal EventsApiFailureEventArgs(RequestHeaderSdkMessage upload, Exception error) { Error = error; RequestHeaderSdkMessage = upload; }
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())); }
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)); }
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)); }