private async Task <Object> SendIdentityRequest(IdentityBaseRequest identityRequest, Uri identityUri)
        {
            using (var httpClient = new HttpClient())
            {
                try
                {
                    var response = await SendIdentityRequestAsync(identityRequest, identityUri, httpClient);

                    if (response.IsSuccessStatusCode)
                    {
                        var stringResult = response.Content.ReadAsStringAsync().Result;
                        this.Logger?.Log(new LogEntry(LoggingEventType.Debug, "Identity Api Request Success:\n" + stringResult));
                        return(JsonConvert.DeserializeObject <IdentityResponse>(stringResult) ?? new IdentityResponse());
                    }
                    else
                    {
                        this.Logger?.Log(new LogEntry(LoggingEventType.Debug, "Identity Api Request failed:\n" + response.ToString()));
                        var stringResult  = response.Content.ReadAsStringAsync().Result;
                        var errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(stringResult);
                        return(errorResponse ?? new ErrorResponse()
                        {
                            StatusCode = (int)response.StatusCode
                        });
                    }
                }
                catch (Exception ex)
                {
                    this.Logger?.Log(new LogEntry(LoggingEventType.Debug, "Identity Api Request failed:\n" + ex.Message));
                    return(new ErrorResponse()
                    {
                        StatusCode = -1
                    });
                }
            }
        }
 private async Task <HttpResponseMessage> SendIdentityRequestAsync(IdentityBaseRequest identityRequest, Uri identityUri, HttpClient httpClient)
 {
     using (var httpRequest = CreateHttpRequest(identityRequest, identityUri))
     {
         return(await httpClient.SendAsync(httpRequest));
     }
 }
 private void PopulateBaseIdentityRequest(IdentityBaseRequest identityRequest)
 {
     identityRequest.ClientSdk = new ClientSdk()
     {
         Platform   = Platform.Xbox,
         SdkVendor  = "mParticle",
         SdkVersion = MParticle.SdkVersion
     };
     identityRequest.SourceRequestId    = Guid.NewGuid().ToString();
     identityRequest.RequestTimestampMs = DateTimeOffset.Now.ToUnixTimeMilliseconds();
     identityRequest.Environment        =
         MParticle.Instance.Options.DevelopmentMode ? Core.Dto.Identity.Environment.Development : Core.Dto.Identity.Environment.Production;
 }
        private HttpRequestMessage CreateHttpRequest(IdentityBaseRequest identityRequest, Uri identityUri)
        {
            String identityJson = JsonConvert.SerializeObject(identityRequest);

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

            if (!string.IsNullOrEmpty(UserAgent))
            {
                request.Headers.UserAgent.ParseAdd(UserAgent);
            }
            request.Headers.Add("x-mp-key", apiKey);

            EventsApiClient.AddSignature(request, identityJson, identityUri.PathAndQuery, apiSecret);

            return(request);
        }