2023-02-14 01:23:13 +02:00
|
|
|
|
// Copyright (c) 2020 Tudor Stanciu
|
|
|
|
|
|
2023-01-31 02:17:54 +02:00
|
|
|
|
using Netmash.Extensions.Http;
|
2020-12-21 00:45:10 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-03-07 19:36:12 +02:00
|
|
|
|
using Tuitio.PublishedLanguage.Dto;
|
|
|
|
|
using Tuitio.Wrapper.Constants;
|
|
|
|
|
using Tuitio.Wrapper.Models;
|
2020-12-21 00:45:10 +02:00
|
|
|
|
|
2023-01-31 02:17:54 +02:00
|
|
|
|
namespace Tuitio.Wrapper.Services
|
2020-12-21 00:45:10 +02:00
|
|
|
|
{
|
2023-03-07 19:44:55 +02:00
|
|
|
|
internal class TuitioService : ITuitioService
|
2020-12-21 00:45:10 +02:00
|
|
|
|
{
|
|
|
|
|
private const string _contentType = "application/json";
|
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
|
|
2023-03-07 19:44:55 +02:00
|
|
|
|
public TuitioService(HttpClient httpClient, ServiceConfiguration configuration)
|
2020-12-21 00:45:10 +02:00
|
|
|
|
{
|
|
|
|
|
httpClient.BaseAddress = new Uri(configuration.BaseAddress);
|
|
|
|
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_contentType));
|
|
|
|
|
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 19:36:12 +02:00
|
|
|
|
public async Task<AccountLoginResult> Login(string userName, string password)
|
|
|
|
|
{
|
|
|
|
|
var route = string.Format(ApiRoutes.AccountLogin, userName, password);
|
|
|
|
|
var result = await _httpClient.ExecutePostRequest<AccountLoginResult>(route);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AccountLogoutResult> Logout(string token)
|
2020-12-21 00:45:10 +02:00
|
|
|
|
{
|
2023-03-07 19:36:12 +02:00
|
|
|
|
var route = string.Format(ApiRoutes.AccountLogout, token);
|
|
|
|
|
var result = await _httpClient.ExecutePostRequest<AccountLogoutResult>(route);
|
2020-12-21 00:45:10 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 19:36:12 +02:00
|
|
|
|
public async Task<AuthorizationResult> Authorize(string token)
|
2020-12-21 00:45:10 +02:00
|
|
|
|
{
|
|
|
|
|
var route = string.Format(ApiRoutes.Authorization, token);
|
2023-03-07 19:36:12 +02:00
|
|
|
|
var result = await _httpClient.ExecutePostRequest<AuthorizationResult>(route);
|
2020-12-21 00:45:10 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|