using cwebplusApp.Shared.Models; using Json.Net; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Globalization; using System.Net.Http; 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 LFBicycleRest : ILFBicycleRest { 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 = JsonNet.Deserialize(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 = JsonNet.Deserialize(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 = JsonNet.Deserialize(await httpResult.Content.ReadAsStringAsync()); return new List(brands); } throw new HttpRequestException("HTTP error " + httpResult.StatusCode); } } throw new HttpRequestException("HTTP client not initialized!"); } } }