Compare commits

...

3 Commits

Author SHA1 Message Date
jedrzejps
659e3bd4d9 Merge branch 'master' into feature/service_mock 2022-01-27 18:21:21 +01:00
jedrzejps
7e3cd2142a DI 2022-01-27 18:19:47 +01:00
jedrzejps
b111ce2c2c added service for moq 2022-01-27 18:04:07 +01:00
5 changed files with 68 additions and 6 deletions

View File

@ -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<SearchController> _logger;
private readonly IConfiguration Configuration;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IAnaliseService _analiseService;
public SearchController(ILogger<SearchController> logger, IConfiguration configuration, IHttpClientFactory httpClientFactory)
public SearchController(ILogger<SearchController> 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<TweeterResponse>(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);
}

View File

@ -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<string> AnaliseTweets(TweeterResponse tweets);
}
}

View File

@ -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<string> AnaliseTweets(TweeterResponse tweets)
{
var moqResponse = "result: {'pos_prec': 0.875, 'neg_prec': 0.125' }";
return Task.FromResult<string>(moqResponse);
}
}
}

View File

@ -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<string> 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();
}
}
}

View File

@ -1,3 +1,4 @@
using BitSearch.API.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
@ -28,6 +29,7 @@ namespace BitSearch.API
{
services.AddControllers();
services.AddScoped<IAnaliseService, RealAnalise>();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "BitSearch.API", Version = "v1" });