public async Task HttpClientTimeoutReturnsErrorResponse(string content) { // Setup the RetryDelegatingHandler to retry very quickly. var retryStrategy = new FixedIntervalRetryStrategy(Int32.MaxValue, TimeSpan.FromTicks(1)); var retryPolicy = new RetryPolicy(new HttpStatusCodeErrorDetectionStrategy(), retryStrategy); var fakeHttpHandler = new FakeHttpHandler { TweakResponse = (response) => { response.Content = content == null ? null : new StringContent(String.Empty); } }; var retryHandler = new RetryDelegatingHandler(retryPolicy, fakeHttpHandler); // Setup HttpClient to timeout after 500 milliseconds. var httpClient = new HttpClient(retryHandler, false) { Timeout = TimeSpan.FromMilliseconds(500) }; // Make a request using the HttpClient. var fakeClient = new FakeServiceClient(httpClient); var responseMessage = fakeClient.DoStuffSync(); Assert.NotNull(responseMessage); Assert.True(fakeHttpHandler.NumberOfTimesFailedSoFar > 1); Assert.Equal(HttpStatusCode.InternalServerError, responseMessage.StatusCode); }
public async Task <IActionResult> Sentiment( [HttpTrigger(AuthorizationLevel.Function, "post", Route = "teams/sentiment")] HttpRequest req, ILogger log) { try { var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var teamsInfo = JsonConvert.DeserializeObject <TeamsInfo>(requestBody); var teamsConnector = MicrosoftTeamsConnector.Create(options.TeamsConnection); var retryStrategy = new FixedIntervalRetryStrategy(2, TimeSpan.FromSeconds(2)); var retryPolicy = new RetryPolicy(new HttpStatusCodeErrorDetectionStrategy(), retryStrategy); teamsConnector.SetRetryPolicy(retryPolicy); var teams = await teamsConnector.GetAllTeamsAsync(); var klab = teams.Value.Single(t => t.DisplayName == teamsInfo.Team); var channels = await teamsConnector.GetChannelsForGroupAsync(klab.Id); var general = channels.Value.Single(c => c.DisplayName == teamsInfo.Channel); string lastMessage; try { var messages = await teamsConnector.GetMessagesFromChannelAsync(klab.Id, general.Id); //while (messages.NextPageLink != null) // messages = await teamsConnector.GetMessagesFromChannelNextAsync(messages.NextPageLink); lastMessage = messages.First().Body.Content;//.StripHtml(); var doc = new HtmlDocument(); doc.LoadHtml(lastMessage); lastMessage = doc.DocumentNode.InnerText; } catch (CloudException cloudException) { // provide below information when reporting runtime issues // "x-ms-client-request-id", RequestUri and Method var requestUri = cloudException.Request.RequestUri.ToString(); var clientRequestId = cloudException.Request.Headers["x-ms-client-request-id"].First(); var statusCode = cloudException.Response.StatusCode.ToString(); var reasonPhrase = cloudException.Response.ReasonPhrase; log.LogError($"RequestUri '{requestUri}' ClientRequestId '{clientRequestId}' StatusCode '{statusCode}' ReasonPhrase '{reasonPhrase}'"); throw; } var cognitiveTextAnalyticsService = TextAnalyticsConnector.Create(options.TextAnalyticsConnection); cognitiveTextAnalyticsService.SetRetryPolicy(new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(3)); var sentimentScore = await cognitiveTextAnalyticsService.Sentiment.DetectSentimentV2Async(new MultiLanguageInput { Language = "it", Text = lastMessage }); return(new OkObjectResult(sentimentScore)); } catch (Exception e) { return(new ExceptionResult(e, true)); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { // Replace with your connection string for MicrosoftTeams var teamsConnector = MicrosoftTeamsConnector.Create("endpoint=https://47e705ee2f46a8cd.12.common.logic-westcentralus.azure-apihub.net/apim/teams/b30df2dac2084c59a04f560980816892;auth=managed"); // Add resiliency to outbound calls // For retry policy documentation see // https://docs.microsoft.com/en-us/dotnet/api/microsoft.rest.transientfaulthandling?view=azure-dotnet var retryStrategy = new FixedIntervalRetryStrategy(Int32.MaxValue, TimeSpan.FromTicks(1)); var retryPolicy = new RetryPolicy(new HttpStatusCodeErrorDetectionStrategy(), retryStrategy); teamsConnector.SetRetryPolicy(retryPolicy); var teams = await teamsConnector.GetAllTeamsAsync(); var team = teams.Value.FirstOrDefault(t => t.DisplayName.Equals("Kingfisher FY21")); var channels = await teamsConnector.GetChannelsForGroupAsync(team.Id); var channel = channels.Value.FirstOrDefault(c => c.DisplayName.Equals("ipaas RFP")); string lastMessage = string.Empty; try { var messages = await teamsConnector.GetMessagesFromChannelAsync(team.Id, channel.Id); lastMessage = messages.First().Body.Content; // Paging - Some of the operations might need paging // Example in the above case // if(messages.NextPageLink != null) // var moreMessages = await teamsConnector.GetMessagesFromChannelNextAsync(messages.NextPageLink); } catch (CloudException cloudException) { // provide below information when reporting runtime issues // "x-ms-client-request-id", RequestUri and Method var requestUri = cloudException.Request.RequestUri.ToString(); var clientRequestId = cloudException.Request.Headers["x-ms-client-request-id"].First(); var statusCode = cloudException.Response.StatusCode.ToString(); var reasonPhrase = cloudException.Response.ReasonPhrase; log.LogError("RequestUri '{0}' ClientRequestId '{1}' StatusCode '{2}' ReasonPhrase '{3}'", requestUri, clientRequestId, statusCode, reasonPhrase); throw cloudException; } // Replace with your connection string for TextAnalytics var cognitiveTextAnalyticsService = TextAnalyticsConnector.Create("endpoint=https://47e705ee2f46a8cd.12.common.logic-westcentralus.azure-apihub.net/apim/cognitiveservicestextanalytics/d655a1436b414876957f6638a3bd4276;auth=managed"); cognitiveTextAnalyticsService.SetRetryPolicy(new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(3)); var sentimentScore = await cognitiveTextAnalyticsService.Sentiment.DetectSentimentV2Async(new MultiLanguageInput { Language = "en", Text = lastMessage }); return(new OkObjectResult(sentimentScore)); }