public override void SendAsync(NSUrl url, NSString method, NSDictionary <NSString, NSString> headers, NSData data, MSHttpRequestCompletionHandler completionHandler)
        {
            _httpClientDelegate?.WillSendHTTPRequestToURL(url, headers);
            var managedHeaders = new Dictionary <string, string>();

            foreach (KeyValuePair <NSObject, NSObject> header in headers)
            {
                managedHeaders[header.Key.ToString()] = header.Value.ToString();
            }
            _httpNetworkAdapter.SendAsync(url.ToString(), method, managedHeaders, data.ToString(), CancellationToken.None).ContinueWith(t =>
            {
                var innerException = t.Exception?.InnerException;
                if (innerException is HttpException)
                {
                    var response = (innerException as HttpException).HttpResponse;
                    completionHandler(NSData.FromString(response.Content), new NSHttpUrlResponse(url, response.StatusCode, "1.1", new NSDictionary()), null);
                }
                else if (innerException != null)
                {
                    var userInfo = NSDictionary.FromObjectAndKey(new NSString("stackTrace"), new NSString(innerException.ToString()));
                    completionHandler(null, null, new NSError(new NSString(".NET SDK"), 1, userInfo));
                }
                else
                {
                    var response = t.Result;
                    completionHandler(NSData.FromString(response.Content), new NSHttpUrlResponse(url, response.StatusCode, "1.1", new NSDictionary()), null);
                }
            });
        }
        /// <exception cref="IngestionException"/>
        public async Task ExecuteCallAsync(IServiceCall call)
        {
            if (call.CancellationToken.IsCancellationRequested)
            {
                return;
            }
            var baseUrl = string.IsNullOrEmpty(_baseLogUrl) ? DefaultBaseUrl : _baseLogUrl;

            MobileCenterLog.Verbose(MobileCenterLog.LogTag, $"Calling {baseUrl + ApiVersion}...");

            // Create request headers.
            var requestHeaders = CreateHeaders(call.AppSecret, call.InstallId);

            MobileCenterLog.Verbose(MobileCenterLog.LogTag, "Headers: " +
                                    $"{AppSecret}={GetRedactedAppSecret(call.AppSecret)}, " +
                                    $"{InstallId}={call.InstallId}");

            // Create request content.
            var requestContent = CreateLogsContent(call.Logs);

            MobileCenterLog.Verbose(MobileCenterLog.LogTag, requestContent);

            // Send request.
            await _httpNetwork.SendAsync(baseUrl + ApiVersion, HttpMethod.Post, requestHeaders, requestContent, call.CancellationToken).ConfigureAwait(false);
        }
Пример #3
0
        public IServiceCall CallAsync(string uri, string method, IDictionary <string, string> headers, IAndroidHttpClientCallTemplate callTemplate, IServiceCallback serviceCallback)
        {
            callTemplate?.OnBeforeCalling(new Java.Net.URL(uri), headers);
            var jsonContent             = callTemplate?.BuildRequestBody();
            var cancellationTokenSource = new CancellationTokenSource();

            _httpNetworkAdapter.SendAsync(uri, method, headers, jsonContent, cancellationTokenSource.Token).ContinueWith(t =>
            {
                var innerException = t.Exception?.InnerException;
                if (innerException is HttpException)
                {
                    var response = (innerException as HttpException).HttpResponse;
                    serviceCallback.OnCallFailed(new AndroidHttpException(new AndroidHttpResponse(response.StatusCode, response.Content)));
                }
                else if (innerException != null)
                {
                    serviceCallback.OnCallFailed(new Java.Lang.Exception(innerException.Message));
                }
                else
                {
                    var response = t.Result;
                    serviceCallback.OnCallSucceeded(new AndroidHttpResponse(response.StatusCode, response.Content));
                }
            });
            return(new ServiceCall(cancellationTokenSource));
        }
        /// <exception cref="IngestionException"/>
        public async Task ExecuteCallAsync(IServiceCall call)
        {
            if (call.CancellationToken.IsCancellationRequested)
            {
                return;
            }
            var requestContent = CreateLogsContent(call.Logs);

            using (var request = CreateRequest(call.AppSecret, call.InstallId, requestContent))
                using (var response = await _httpNetwork.SendAsync(request, call.CancellationToken).ConfigureAwait(false))
                {
                    if (response == null)
                    {
                        throw new IngestionException("Null response received");
                    }
                    var responseContent = "(null)";
                    if (response.Content != null)
                    {
                        responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    }
                    MobileCenterLog.Verbose(MobileCenterLog.LogTag, $"HTTP response status={(int)response.StatusCode} ({response.StatusCode}) payload={responseContent}");
                    if (call.CancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        var ex = new HttpIngestionException($"Operation returned an invalid status code '{response.StatusCode}'")
                        {
                            Method          = request.Method,
                            RequestUri      = request.RequestUri,
                            StatusCode      = response.StatusCode,
                            RequestContent  = requestContent,
                            ResponseContent = responseContent
                        };
                        throw ex;
                    }
                }
        }
        /// <exception cref="IngestionException"/>
        private async Task <string> CallAsync(string appSecret, Guid installId, IList <Log> logs, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            var baseUrl = string.IsNullOrEmpty(_baseLogUrl) ? DefaultBaseUrl : _baseLogUrl;

            AppCenterLog.Verbose(AppCenterLog.LogTag, $"Calling {baseUrl + ApiVersion}...");

            // Create request headers.
            var requestHeaders = CreateHeaders(appSecret, installId);

            AppCenterLog.Verbose(AppCenterLog.LogTag, "Headers: " +
                                 $"{AppSecret}={GetRedactedAppSecret(appSecret)}, " +
                                 $"{InstallId}={installId}");

            // Create request content.
            var requestContent = CreateLogsContent(logs);

            AppCenterLog.Verbose(AppCenterLog.LogTag, requestContent);

            // Send request.
            return(await _httpNetwork.SendAsync(baseUrl + ApiVersion, "POST", requestHeaders, requestContent, token).ConfigureAwait(false));
        }