From b111ce2c2c1e979bcdfba8a8cd97a8c400d73e3c Mon Sep 17 00:00:00 2001 From: jedrzejps Date: Thu, 27 Jan 2022 18:04:07 +0100 Subject: [PATCH] added service for moq --- .../Controllers/SearchController.cs | 13 +++++---- .../BitSearch.API/Services/IAnaliseService.cs | 13 +++++++++ .../BitSearch.API/Services/MoqAnalise.cs | 18 ++++++++++++ .../BitSearch.API/Services/RealAnalise.cs | 28 +++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 BitSearch/BitSearch.API/Services/IAnaliseService.cs create mode 100644 BitSearch/BitSearch.API/Services/MoqAnalise.cs create mode 100644 BitSearch/BitSearch.API/Services/RealAnalise.cs diff --git a/BitSearch/BitSearch.API/Controllers/SearchController.cs b/BitSearch/BitSearch.API/Controllers/SearchController.cs index c6be82f..507419d 100644 --- a/BitSearch/BitSearch.API/Controllers/SearchController.cs +++ b/BitSearch/BitSearch.API/Controllers/SearchController.cs @@ -1,4 +1,5 @@ using BitSearch.API.Models; +using BitSearch.API.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Configuration; @@ -24,12 +25,15 @@ namespace BitSearch.API.Controllers private readonly ILogger _logger; private readonly IConfiguration Configuration; private readonly IHttpClientFactory _httpClientFactory; + private readonly IAnaliseService _analiseService; - public SearchController(ILogger logger, IConfiguration configuration, IHttpClientFactory httpClientFactory) + public SearchController(ILogger logger, IConfiguration configuration, IHttpClientFactory httpClientFactory, + IAnaliseService analiseService) { _logger = logger; _httpClientFactory = httpClientFactory; Configuration = configuration; + _analiseService = analiseService; } [HttpGet("ranking")] @@ -94,12 +98,9 @@ namespace BitSearch.API.Controllers { var tweetsDto = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); tweetsDto.data = tweetsDto.data.Where(d => d.lang == "en"); - var microServiceClient = _httpClientFactory.CreateClient(); - var microServiceRequest = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1:5000/analysis"); - microServiceRequest.Content = JsonContent.Create(tweetsDto); - var microServiceResponse = await microServiceClient.SendAsync(microServiceRequest); + var analise = await _analiseService.AnaliseTweets(tweetsDto); - return Ok(await microServiceResponse.Content.ReadAsStringAsync()); + return Ok(analise); //return Ok(tweetsDto); } diff --git a/BitSearch/BitSearch.API/Services/IAnaliseService.cs b/BitSearch/BitSearch.API/Services/IAnaliseService.cs new file mode 100644 index 0000000..117f0b6 --- /dev/null +++ b/BitSearch/BitSearch.API/Services/IAnaliseService.cs @@ -0,0 +1,13 @@ +using BitSearch.API.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BitSearch.API.Services +{ + public interface IAnaliseService + { + Task AnaliseTweets(TweeterResponse tweets); + } +} diff --git a/BitSearch/BitSearch.API/Services/MoqAnalise.cs b/BitSearch/BitSearch.API/Services/MoqAnalise.cs new file mode 100644 index 0000000..c092057 --- /dev/null +++ b/BitSearch/BitSearch.API/Services/MoqAnalise.cs @@ -0,0 +1,18 @@ +using BitSearch.API.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BitSearch.API.Services +{ + public class MoqAnalise : IAnaliseService + { + public Task AnaliseTweets(TweeterResponse tweets) + { + var moqResponse = "result: {'pos_prec': 0.875, 'neg_prec': 0.125' }"; + + return Task.FromResult(moqResponse); + } + } +} diff --git a/BitSearch/BitSearch.API/Services/RealAnalise.cs b/BitSearch/BitSearch.API/Services/RealAnalise.cs new file mode 100644 index 0000000..7722d89 --- /dev/null +++ b/BitSearch/BitSearch.API/Services/RealAnalise.cs @@ -0,0 +1,28 @@ +using BitSearch.API.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Json; +using System.Threading.Tasks; + +namespace BitSearch.API.Services +{ + public class RealAnalise : IAnaliseService + { + private readonly IHttpClientFactory _httpClientFactory; + public RealAnalise(IHttpClientFactory httpClientFactor) + { + _httpClientFactory = httpClientFactor; + } + public async Task AnaliseTweets(TweeterResponse tweets) + { + var microServiceClient = _httpClientFactory.CreateClient(); + var microServiceRequest = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1:5000/analysis"); + microServiceRequest.Content = JsonContent.Create(tweets); + var microServiceResponse = await microServiceClient.SendAsync(microServiceRequest); + + return await microServiceResponse.Content.ReadAsStringAsync(); + } + } +}