PWA Fundvelo der Caritas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BicycleRestService.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using cwebplusApp.Shared.Models;
  2. using Microsoft.Extensions.Configuration;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace cwebplusApp.Shared.Services {
  11. // REST interface responsible to submit lost or found reports and get the available master data.
  12. public class BicycleRestService : IBicycleRestService {
  13. private static readonly string VERSION = "v1";
  14. private HttpClient httpClient;
  15. public IConfiguration Configuration { get; set; }
  16. public OnlineStatusProvider OnlineStatusProvider { get; set; }
  17. public void Initialize(IConfiguration configuration, OnlineStatusProvider onlineStatusProvider) {
  18. this.Configuration = configuration;
  19. this.OnlineStatusProvider = onlineStatusProvider;
  20. string hostBaseUrl = Configuration.GetValue<string>("host_base_url");
  21. if (!String.IsNullOrEmpty(hostBaseUrl)) {
  22. this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) };
  23. }
  24. }
  25. public async Task<List<ColorItem>> GetColors() {
  26. if (httpClient != null) {
  27. string subResourceUrl = Configuration.GetValue<string>("subresource_url_colors");
  28. if (!String.IsNullOrEmpty(subResourceUrl)) {
  29. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  30. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  31. ColorItem[] colors = JsonConvert.DeserializeObject<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
  32. return new List<ColorItem>(colors);
  33. }
  34. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  35. }
  36. }
  37. throw new HttpRequestException("HTTP client not initialized!");
  38. }
  39. public async Task<List<BicycleType>> GetBicycleTypes() {
  40. if (httpClient != null) {
  41. string subResourceUrl = Configuration.GetValue<string>("subresource_url_types");
  42. if (!String.IsNullOrEmpty(subResourceUrl)) {
  43. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  44. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  45. BicycleType[] bicycleTypes = JsonConvert.DeserializeObject<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
  46. return new List<BicycleType>(bicycleTypes);
  47. }
  48. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  49. }
  50. }
  51. throw new HttpRequestException("HTTP client not initialized!");
  52. }
  53. public async Task<List<Brand>> GetBrands() {
  54. if (httpClient != null) {
  55. string subResourceUrl = Configuration.GetValue<string>("subresource_url_brands");
  56. if (!String.IsNullOrEmpty(subResourceUrl)) {
  57. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  58. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  59. Brand[] brands = JsonConvert.DeserializeObject<Brand[]>(await httpResult.Content.ReadAsStringAsync());
  60. return new List<Brand>(brands);
  61. }
  62. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  63. }
  64. }
  65. throw new HttpRequestException("HTTP client not initialized!");
  66. }
  67. public async Task<ReportResponse> SendFoundReport(Report report) {
  68. string subResourceUrl = Configuration.GetValue<string>("subresource_url_foundreport");
  69. return await SendReport(report, subResourceUrl);
  70. }
  71. public async Task<ReportResponse> SendMissingReport(Report report) {
  72. string subResourceUrl = Configuration.GetValue<string>("subresource_url_missingreport");
  73. return await SendReport(report, subResourceUrl);
  74. }
  75. protected async Task<ReportResponse> SendReport(Report report, string subResourceUrl) {
  76. if (OnlineStatusProvider.Online) {
  77. if (httpClient != null) {
  78. if (!String.IsNullOrEmpty(subResourceUrl)) {
  79. string reportJson = JsonConvert.SerializeObject(report);
  80. HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json");
  81. HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content);
  82. string msg = await httpResult.Content.ReadAsStringAsync();
  83. ReportResponse response = JsonConvert.DeserializeObject<ReportResponse>(msg);
  84. response.StatusCode = httpResult.StatusCode;
  85. return response;
  86. }
  87. }
  88. throw new HttpRequestException("HTTP client not initialized!");
  89. } else {
  90. //TODO: Save to app storage
  91. return null;
  92. }
  93. }
  94. }
  95. }