| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using cwebplusApp.Shared.Models;
- 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;
- public IConfiguration Configuration { get; set; }
- public OnlineStatusProvider OnlineStatusProvider { get; set; }
-
- public void Initialize(IConfiguration configuration, OnlineStatusProvider onlineStatusProvider) {
- this.Configuration = configuration;
- this.OnlineStatusProvider = onlineStatusProvider;
- string hostBaseUrl = Configuration.GetValue<string>("host_base_url");
- if (!String.IsNullOrEmpty(hostBaseUrl)) {
- this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) };
- }
- }
-
- public async Task<List<ColorItem>> GetColors() {
- if (httpClient != null) {
- string subResourceUrl = Configuration.GetValue<string>("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<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
- return new List<ColorItem>(colors);
- }
- throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- }
-
- public async Task<List<BicycleType>> GetBicycleTypes() {
- if (httpClient != null) {
- string subResourceUrl = Configuration.GetValue<string>("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<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
- return new List<BicycleType>(bicycleTypes);
- }
- throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- }
-
- public async Task<List<Brand>> GetBrands() {
- if (httpClient != null) {
- string subResourceUrl = Configuration.GetValue<string>("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<Brand[]>(await httpResult.Content.ReadAsStringAsync());
- return new List<Brand>(brands);
- }
- throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- }
-
- public async Task<ReportResponse> SendFoundReport(Report report) {
- string subResourceUrl = Configuration.GetValue<string>("subresource_url_foundreport");
- return await SendReport(report, subResourceUrl);
- }
-
- public async Task<ReportResponse> SendMissingReport(Report report) {
- string subResourceUrl = Configuration.GetValue<string>("subresource_url_missingreport");
- return await SendReport(report, subResourceUrl);
- }
-
- protected async Task<ReportResponse> SendReport(Report report, string subResourceUrl) {
- if (OnlineStatusProvider.Online) {
- 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<ReportResponse>(msg);
- response.StatusCode = httpResult.StatusCode;
- return response;
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- } else {
- //TODO: Save to app storage
- return null;
- }
- }
- }
- }
|