using cwebplusApp.Shared.Models; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace cwebplusApp.Shared.Services { // REST interface responsible to submit lost or found reports and get the available master data. public class BicycleRestService : IBicycleRestService { private static readonly string VERSION = "v1"; private HttpClient httpClient; [Inject] public IConfiguration Configuration { get; set; } public void Initialize(IConfiguration configuration) { this.Configuration = configuration; string hostBaseUrl = Configuration.GetValue("host_base_url"); if (!String.IsNullOrEmpty(hostBaseUrl)) { this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) }; } } public async Task> GetColors() { if (httpClient != null) { string subResourceUrl = Configuration.GetValue("subresource_url_colors"); if (!String.IsNullOrEmpty(subResourceUrl)) { HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName)); if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) { ColorItem[] colors = JsonConvert.DeserializeObject(await httpResult.Content.ReadAsStringAsync()); return new List(colors); } throw new HttpRequestException("HTTP error " + httpResult.StatusCode); } } throw new HttpRequestException("HTTP client not initialized!"); } public async Task> GetBicycleTypes() { if (httpClient != null) { string subResourceUrl = Configuration.GetValue("subresource_url_types"); if (!String.IsNullOrEmpty(subResourceUrl)) { HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName)); if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) { BicycleType[] bicycleTypes = JsonConvert.DeserializeObject(await httpResult.Content.ReadAsStringAsync()); return new List(bicycleTypes); } throw new HttpRequestException("HTTP error " + httpResult.StatusCode); } } throw new HttpRequestException("HTTP client not initialized!"); } public async Task> GetBrands() { if (httpClient != null) { string subResourceUrl = Configuration.GetValue("subresource_url_brands"); if (!String.IsNullOrEmpty(subResourceUrl)) { HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName)); if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) { Brand[] brands = JsonConvert.DeserializeObject(await httpResult.Content.ReadAsStringAsync()); return new List(brands); } throw new HttpRequestException("HTTP error " + httpResult.StatusCode); } } throw new HttpRequestException("HTTP client not initialized!"); } public async Task SendFoundReport(Report report) { string subResourceUrl = Configuration.GetValue("subresource_url_foundreport"); return await SendReport(report, subResourceUrl); } public async Task SendMissingReport(Report report) { string subResourceUrl = Configuration.GetValue("subresource_url_missingreport"); return await SendReport(report, subResourceUrl); } protected async Task SendReport(Report report, string subResourceUrl) { if (httpClient != null) { if (!String.IsNullOrEmpty(subResourceUrl)) { string reportJson = JsonConvert.SerializeObject(report); HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json"); HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content); string msg = await httpResult.Content.ReadAsStringAsync(); ReportResponse response = JsonConvert.DeserializeObject(msg); response.StatusCode = httpResult.StatusCode; return response; } } throw new HttpRequestException("HTTP client not initialized!"); } } }