private async void BVisibility_Click(object sender, EventArgs e) { using (frmSINnerVisibility visfrm = new frmSINnerVisibility()) { using (new CursorWait(this, true)) { if (myUC.MyCE.MySINnerFile.SiNnerMetaData.Visibility.UserRights.Count == 0) { var client = StaticUtils.GetClient(); if (myUC.MyCE.MySINnerFile.Id != null) { HttpOperationResponse <ResultSinnerGetSINnerVisibilityById> res = await client.GetSINnerVisibilityByIdWithHttpMessagesAsync( myUC.MyCE.MySINnerFile.Id.Value).ConfigureAwait(true); await Backend.Utils.HandleError(res, res.Body).ConfigureAwait(true); if (res.Body?.CallSuccess == true) { myUC.MyCE.MySINnerFile.SiNnerMetaData.Visibility.UserRights = res.Body.UserRights; } res.Dispose(); } } } visfrm.MyVisibility = myUC.MyCE.MySINnerFile.SiNnerMetaData.Visibility; var result = visfrm.ShowDialog(this); if (result == DialogResult.OK) { myUC.MyCE.MySINnerFile.SiNnerMetaData.Visibility = visfrm.MyVisibility; } } }
public static T GetBodyAndDispose <T>(this HttpOperationResponse <T> response) { T body = response.Body; response.Dispose(); return(body); }
protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { _inner.Dispose(); _response.Dispose(); } }
public async Task AnalyzesSentimentScore(string message, double?score) { var config = Options.Create(new WebConfiguration { TextAnalyticsSubscriptionKey = _validSubscriptionKey }); // SentimentBatchAsync extension method uses SentimentWithHttpMessagesAsync internally and mocking // extension methods with Moq isn't currently possible. Extension methods are "hard dependencies" that // one could also break by introducing a wrapper class and an interface. // Let's mock SentimentWithHttpMessagesAsync method here instead. var httpOperationResponse = new HttpOperationResponse <SentimentBatchResult> { Body = new SentimentBatchResult(new SentimentBatchResultItem[] { new SentimentBatchResultItem(score: score) }) }; var textAnalyticsClientMock = new Mock <ITextAnalyticsClient>(); textAnalyticsClientMock.Setup(x => x.SentimentWithHttpMessagesAsync( It.IsAny <bool?>(), It.IsAny <MultiLanguageBatchInput>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .ReturnsAsync(httpOperationResponse); var textAnalyticsClientFactoryMock = new Mock <ITextAnalyticsClientFactory>(); textAnalyticsClientFactoryMock.Setup(x => x.CreateClient(It.IsAny <string>(), It.IsAny <string>())) .Returns(textAnalyticsClientMock.Object); ITextAnalyzer textAnalyzer = new TextAnalyzer(config, textAnalyticsClientFactoryMock.Object); var comment = new Comment("test-post-id", message, "John Doe"); var analyzedComment = await textAnalyzer.AnalyzeAsync(comment).ConfigureAwait(false); httpOperationResponse.Dispose(); Assert.Equal($"{score:0.00}", analyzedComment.Comment.Score); }
private async Task <MyUserState> ShareSingleChummer() { if (MyCharacterCache == null) { throw new ArgumentNullException(nameof(MyCharacterCache)); } string hash = string.Empty; try { using (var op_shareChummer = Timekeeper.StartSyncron("Share Chummer", null, CustomActivity.OperationType.DependencyOperation, MyCharacterCache.FilePath)) { MyUserState myState = new MyUserState(this); CharacterExtended ce = null; var client = StaticUtils.GetClient(); string sinnerid = ""; Guid SINid = Guid.Empty; try { async Task <CharacterExtended> GetCharacterExtended(CustomActivity parentActivity) { using (_ = Timekeeper.StartSyncron("Loading Chummerfile", parentActivity, CustomActivity.OperationType.DependencyOperation, MyCharacterCache.FilePath)) { Character c = PluginHandler.MainForm.OpenCharacters.FirstOrDefault(a => a.FileName == MyCharacterCache.FilePath); bool blnSuccess = true; if (c == null) { c = new Character { FileName = MyCharacterCache.FilePath }; using (frmLoading frmLoadingForm = new frmLoading { CharacterFile = MyCharacterCache.FilePath }) { frmLoadingForm.Reset(36); frmLoadingForm.TopMost = true; frmLoadingForm.Show(); myState.StatusText = "Loading chummer file..."; myState.CurrentProgress += 10; ReportProgress(myState.CurrentProgress, myState); blnSuccess = await c.Load(frmLoadingForm, false).ConfigureAwait(true); } } if (!blnSuccess) { throw new ArgumentNullException("Could not load Character file " + MyCharacterCache.FilePath + "."); } ce = new CharacterExtended(c, null, null, MyCharacterCache); if (ce?.MySINnerFile?.Id != null) { sinnerid = ce.MySINnerFile.Id.ToString(); } hash = ce?.MySINnerFile?.MyHash; return(ce); } } if (MyCharacterCache.MyPluginDataDic.TryGetValue("SINnerId", out object sinneridobj)) { sinnerid = sinneridobj?.ToString() ?? string.Empty; } else { ce = await GetCharacterExtended(op_shareChummer).ConfigureAwait(true); sinnerid = ce.MySINnerFile.Id.ToString(); hash = ce?.MySINnerFile?.MyHash ?? string.Empty; } if (string.IsNullOrEmpty(sinnerid) || !Guid.TryParse(sinnerid, out SINid)) { myState.StatusText = "SINner Id is unknown or not issued!"; ReportProgress(30, myState); } else { myState.StatusText = "SINner Id is " + SINid + "."; myState.CurrentProgress = 30; ReportProgress(myState.CurrentProgress, myState); } HttpOperationResponse <ResultSinnerGetSINById> checkresult = null; try { //check if char is already online and updated using (_ = Timekeeper.StartSyncron( "check if online", op_shareChummer, CustomActivity.OperationType.DependencyOperation, MyCharacterCache?.FilePath)) { checkresult = await client.GetSINByIdWithHttpMessagesAsync(SINid).ConfigureAwait(true); if (checkresult == null) { throw new ArgumentException("Could not parse result from SINners Webservice!"); } if (checkresult.Response.StatusCode != HttpStatusCode.NotFound) { if (checkresult.Body.CallSuccess != true) { if (checkresult.Body.MyException is Exception myException) { throw new ArgumentException( "Error from SINners Webservice: " + checkresult.Body.ErrorText, myException); } throw new ArgumentException("Error from SINners Webservice: " + checkresult.Body.ErrorText); } hash = checkresult.Body.MySINner.MyHash; } } var lastWriteTimeUtc = MyCharacterCache != null?File.GetLastWriteTimeUtc(MyCharacterCache.FilePath) : DateTime.MinValue; if (checkresult.Response.StatusCode == HttpStatusCode.NotFound || checkresult.Body.MySINner.LastChange < lastWriteTimeUtc) { if (ce == null) { myState.StatusText = "The Chummer is newer and has to be uploaded again."; myState.CurrentProgress = 30; ReportProgress(myState.CurrentProgress, myState); ce = await GetCharacterExtended(op_shareChummer).ConfigureAwait(true); } if (ce != null) { using (var op_uploadChummer = Timekeeper.StartSyncron( "Uploading Chummer", op_shareChummer, CustomActivity.OperationType.DependencyOperation, MyCharacterCache?.FilePath)) { myState.StatusText = "Checking SINner availability (and if necessary upload it)."; myState.CurrentProgress = 35; ReportProgress(myState.CurrentProgress, myState); myState.ProgressSteps = 10; var uploadtask = await ce.Upload(myState, op_uploadChummer).ConfigureAwait(true); SINid = ce.MySINnerFile.Id.Value; using (var result = await client.GetSINByIdWithHttpMessagesAsync(SINid).ConfigureAwait(true)) { if (result == null) { throw new ArgumentException("Could not parse result from SINners Webservice!"); } if (result.Body?.CallSuccess != true) { if (result.Body?.MyException is Exception myException) { throw new ArgumentException( "Error from SINners Webservice: " + result.Body?.ErrorText, myException); } throw new ArgumentException( "Error from SINners Webservice: " + result.Body?.ErrorText); } hash = result.Body.MySINner.MyHash; } } } } } finally { checkresult?.Dispose(); } } finally { ce?.Dispose(); } myState.StatusText = "SINner is online available."; myState.CurrentProgress = 90; ReportProgress(myState.CurrentProgress, myState); string url = client.BaseUri + "O"; url += "/" + hash; if (Settings.Default.OpenChummerFromSharedLinks) { url += "?open=true"; } myState.LinkText = url; ReportProgress(100, myState); RunWorkerCompleted(myState); return(myState); } } catch (Exception exception) { Log.Warn(exception); throw; } }