private BacktraceResult Send(Guid requestId, string json, List <string> attachments, BacktraceReport report, int deduplication = 0) { var requestUrl = _serverurl.ToString(); if (deduplication > 0) { requestUrl += $"&_mod_duplicate={deduplication}"; } var formData = FormDataHelper.GetFormData(json, attachments, requestId); string contentType = FormDataHelper.GetContentTypeWithBoundary(requestId); var request = WebRequest.Create(requestUrl) as HttpWebRequest; //Set up the request properties. request.Method = "POST"; request.ContentType = contentType; request.ContentLength = formData.Length; try { using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(formData, 0, formData.Length); requestStream.Close(); } return(ReadServerResponse(request, report)); } catch (Exception exception) { OnServerError?.Invoke(exception); return(BacktraceResult.OnError(report, exception)); } #endif }
private IEnumerator Send(string json, List <string> attachments, BacktraceReport report, Action <BacktraceResult> callback) { using (var request = new UnityWebRequest(_serverurl, "POST")) { AppendBodyToFile(json); byte[] bodyRaw = Encoding.UTF8.GetBytes(json); request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); BacktraceResult result; if (request.responseCode == 200) { result = new BacktraceResult(); OnServerResponse?.Invoke(result); var response = BacktraceResult.FromJson(request.downloadHandler.text); if (attachments != null && attachments.Count > 0) { var stack = new Stack <string>(attachments); yield return(SendAttachment(response.Object, stack)); } } else { PrintLog(request); var exception = new Exception(request.error); result = BacktraceResult.OnError(report, exception); OnServerError?.Invoke(exception); } callback?.Invoke(result); yield return(result); } }
public void Setup() { //mock file context var mockFileContext = new Mock <IBacktraceDatabaseFileContext>(); mockFileContext.Setup(n => n.GetRecords()) .Returns(new List <FileInfo>()); mockFileContext.Setup(n => n.Clear()); ////mock api var mockApi = new Mock <IBacktraceApi>(); mockApi.Setup(n => n.Send(It.IsAny <BacktraceData>())) .Returns(BacktraceResult.OnError(It.IsAny <BacktraceReport>(), new Exception("error"))); var backtraceDatabaseSettings = new BacktraceDatabaseSettings(_projectDirectory) { RetryLimit = _batchLimit, RetryBehavior = Types.RetryBehavior.NoRetry, AutoSendMode = false, DeduplicationStrategy = DeduplicationStrategy.LibraryName | DeduplicationStrategy.Classifier | DeduplicationStrategy.Message }; _backtraceDatabase = new BacktraceDatabase(backtraceDatabaseSettings) { BacktraceApi = mockApi.Object, BacktraceDatabaseFileContext = mockFileContext.Object }; _backtraceDatabase.Start(); }
private IEnumerator Send(string json, List <string> attachments, BacktraceReport report, int deduplication, Action <BacktraceResult> callback) { var requestUrl = _serverurl.ToString(); if (deduplication > 0) { var startingChar = string.IsNullOrEmpty(_serverurl.Query) ? "?" : "&"; requestUrl += string.Format("{0}_mod_duplicate={1}", startingChar, deduplication); } using (var request = new UnityWebRequest(requestUrl, "POST")) { #if UNITY_2018_4_OR_NEWER if (_ignoreSslValidation) { request.certificateHandler = new BacktraceSelfSSLCertificateHandler(); } #endif byte[] bodyRaw = Encoding.UTF8.GetBytes(json); request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); BacktraceResult result; if (request.responseCode == 200) { result = new BacktraceResult(); if (OnServerResponse != null) { OnServerResponse.Invoke(result); } var response = BacktraceResult.FromJson(request.downloadHandler.text); if (attachments != null && attachments.Count > 0) { var stack = new Stack <string>(attachments); yield return(SendAttachment(response.RxId, stack)); } } else { PrintLog(request); var exception = new Exception(request.error); result = BacktraceResult.OnError(report, exception); if (OnServerError != null) { OnServerError.Invoke(exception); } } if (callback != null) { callback.Invoke(result); } yield return(result); } }
internal async Task <BacktraceResult> SendAsync(Guid requestId, string json, List <string> attachments, BacktraceReport report, int deduplication = 0) { string contentType = FormDataHelper.GetContentTypeWithBoundary(requestId); string boundary = FormDataHelper.GetBoundary(requestId); using (var content = new MultipartFormDataContent(boundary)) { var requestUrl = _serverurl.ToString(); if (deduplication > 0) { requestUrl += $"&_mod_duplicate={deduplication}"; } var request = new HttpRequestMessage(HttpMethod.Post, requestUrl); content.AddJson("upload_file.json", json); content.AddFiles(attachments); //// clear and add content type with boundary tag content.Headers.Remove("Content-Type"); content.Headers.TryAddWithoutValidation("Content-Type", contentType); request.Content = content; try { using (var response = await HttpClient.SendAsync(request)) { var fullResponse = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { var err = new WebException(response.ReasonPhrase); System.Diagnostics.Trace.WriteLine(fullResponse); OnServerError?.Invoke(err); return(BacktraceResult.OnError(report, err)); } var result = JsonConvert.DeserializeObject <BacktraceResult>(fullResponse); result.BacktraceReport = report; OnServerResponse?.Invoke(result); return(result); } } catch (Exception exception) { System.Diagnostics.Trace.WriteLine($"Backtrace - Server error: {exception.ToString()}"); OnServerError?.Invoke(exception); return(BacktraceResult.OnError(report, exception)); } } }
/// <summary> /// Sending diagnostic report to Backtrace /// </summary> /// <param name="json">diagnostic data JSON</param> /// <param name="attachments">List of report attachments</param> /// <param name="queryAttributes">Query string attributes</param> /// <param name="callback">coroutine callback</param> /// <returns>Server response</returns> public IEnumerator Send(string json, List <string> attachments, Dictionary <string, string> queryAttributes, Action <BacktraceResult> callback) { var stopWatch = EnablePerformanceStatistics ? System.Diagnostics.Stopwatch.StartNew() : new System.Diagnostics.Stopwatch(); var requestUrl = queryAttributes != null ? GetParametrizedQuery(_serverUrl.ToString(), queryAttributes) : ServerUrl; using (var request = new UnityWebRequest(requestUrl, "POST")) { #if UNITY_2018_4_OR_NEWER if (_ignoreSslValidation) { request.certificateHandler = new BacktraceSelfSSLCertificateHandler(); } #endif request.timeout = 15000; byte[] bodyRaw = Encoding.UTF8.GetBytes(json); request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); BacktraceResult result; if (request.responseCode == 200) { result = BacktraceResult.FromJson(request.downloadHandler.text); if (OnServerResponse != null) { OnServerResponse.Invoke(result); } if (attachments != null && attachments.Count > 0) { var stack = new Stack <string>(attachments); yield return(SendAttachment(result.RxId, stack)); } } else { PrintLog(request); var exception = new Exception(request.error); result = BacktraceResult.OnError(exception); if (OnServerError != null) { OnServerError.Invoke(exception); } } if (callback != null) { callback.Invoke(result); } if (EnablePerformanceStatistics) { stopWatch.Stop(); Debug.Log(string.Format("Backtrace - JSON send time: {0}μs", stopWatch.GetMicroseconds())); } yield return(result); } }