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 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace cwebplusApp.Shared.Services {
  12. // REST interface responsible to submit lost or found reports and get the available master data.
  13. public class BicycleRestService : IBicycleRestService {
  14. private static readonly string VERSION = "v1";
  15. private HttpClient httpClient;
  16. private IConfiguration configuration;
  17. private readonly OnlineStatusProvider onlineStatusProvider;
  18. private readonly ReportRepositoryService reportRepositoryService;
  19. public BicycleRestService(ReportRepositoryService _reportRepositoryService, OnlineStatusProvider _onlineStatusProvider) {
  20. this.reportRepositoryService = _reportRepositoryService;
  21. this.onlineStatusProvider = _onlineStatusProvider;
  22. }
  23. public void Initialize(IConfiguration _configuration) {
  24. this.configuration = _configuration;
  25. string hostBaseUrl = configuration.GetValue<string>("host_base_url");
  26. if (!String.IsNullOrEmpty(hostBaseUrl)) {
  27. this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) };
  28. }
  29. }
  30. public async Task<List<ColorItem>> GetColors() {
  31. if (httpClient != null) {
  32. string subResourceUrl = configuration.GetValue<string>("subresource_url_colors");
  33. if (!String.IsNullOrEmpty(subResourceUrl)) {
  34. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  35. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  36. ColorItem[] colors = JsonConvert.DeserializeObject<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
  37. return new List<ColorItem>(colors);
  38. }
  39. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  40. }
  41. }
  42. throw new HttpRequestException("HTTP client not initialized!");
  43. }
  44. public async Task<List<BicycleType>> GetBicycleTypes() {
  45. if (httpClient != null) {
  46. string subResourceUrl = configuration.GetValue<string>("subresource_url_types");
  47. if (!String.IsNullOrEmpty(subResourceUrl)) {
  48. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  49. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  50. BicycleType[] bicycleTypes = JsonConvert.DeserializeObject<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
  51. return new List<BicycleType>(bicycleTypes);
  52. }
  53. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  54. }
  55. }
  56. throw new HttpRequestException("HTTP client not initialized!");
  57. }
  58. public async Task<List<Brand>> GetBrands() {
  59. if (httpClient != null) {
  60. string subResourceUrl = configuration.GetValue<string>("subresource_url_brands");
  61. if (!String.IsNullOrEmpty(subResourceUrl)) {
  62. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  63. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  64. Brand[] brands = JsonConvert.DeserializeObject<Brand[]>(await httpResult.Content.ReadAsStringAsync());
  65. return new List<Brand>(brands);
  66. }
  67. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  68. }
  69. }
  70. throw new HttpRequestException("HTTP client not initialized!");
  71. }
  72. public async Task<List<SearchService>> GetSearchServices() {
  73. if (httpClient != null) {
  74. string subResourceUrl = configuration.GetValue<string>("subresource_url_searchservices");
  75. if (!String.IsNullOrEmpty(subResourceUrl)) {
  76. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  77. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  78. SearchService[] searchServices = JsonConvert.DeserializeObject<SearchService[]>(await httpResult.Content.ReadAsStringAsync());
  79. return new List<SearchService>(searchServices);
  80. }
  81. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  82. }
  83. }
  84. throw new HttpRequestException("HTTP client not initialized!");
  85. }
  86. public async Task<ReportResponse> SendFoundReport(Report report) {
  87. string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
  88. return await SendReport(report, subResourceUrl, new FoundReportRepositoryItem((FoundReport)report, 0));
  89. }
  90. public async Task<ReportResponse> SendFoundReport(FoundReportRepositoryItem reportRepositoryItem) {
  91. string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
  92. return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
  93. }
  94. public async Task<ReportResponse> SendMissingReport(Report report) {
  95. string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
  96. return await SendReport(report, subResourceUrl, new MissingReportRepositoryItem((MissingReport)report, 0));
  97. }
  98. public async Task<ReportResponse> SendMissingReport(MissingReportRepositoryItem reportRepositoryItem) {
  99. string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
  100. return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
  101. }
  102. public async Task<int> TrySendPendingReports() {
  103. int sent = 0;
  104. if (onlineStatusProvider.Online) {
  105. sent = await TrySendFoundPendingReports();
  106. sent += await TrySendMissingPendingReports();
  107. }
  108. return sent;
  109. }
  110. public async Task<int> TrySendFoundPendingReports() {
  111. int sent = 0;
  112. List<FoundReportRepositoryItem> pendingFoundReports = await reportRepositoryService.GetPendingFoundReports();
  113. foreach (FoundReportRepositoryItem item in pendingFoundReports) {
  114. try {
  115. ReportResponse response = await SendFoundReport(item);
  116. if (HttpStatusCode.OK == response.StatusCode) {
  117. sent++;
  118. }
  119. } catch (Exception) {
  120. continue;
  121. }
  122. }
  123. return sent;
  124. }
  125. public async Task<int> TrySendMissingPendingReports() {
  126. int sent = 0;
  127. List<MissingReportRepositoryItem> pendingMissingReports = await reportRepositoryService.GetPendingMissingReports();
  128. foreach (MissingReportRepositoryItem item in pendingMissingReports) {
  129. try {
  130. ReportResponse response = await SendMissingReport(item);
  131. if (HttpStatusCode.OK == response.StatusCode) {
  132. sent++;
  133. }
  134. } catch (Exception) {
  135. continue;
  136. }
  137. }
  138. return sent;
  139. }
  140. protected async Task<ReportResponse> SendReport(Report report, string subResourceUrl, ReportRepositoryItem reportRepositoryItem) {
  141. ReportResponse response = null;
  142. if (onlineStatusProvider.Online && ReportRepositoryItem.State.PENDING.Equals(reportRepositoryItem.Status)) {
  143. if (httpClient != null) {
  144. if (!String.IsNullOrEmpty(subResourceUrl)) {
  145. string reportJson = JsonConvert.SerializeObject(report);
  146. HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json");
  147. HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content);
  148. string msg = await httpResult.Content.ReadAsStringAsync();
  149. response = JsonConvert.DeserializeObject<ReportResponse>(msg);
  150. response.StatusCode = httpResult.StatusCode;
  151. if (HttpStatusCode.OK == response.StatusCode) {
  152. reportRepositoryItem.ServerRefNbr = response.Data[0];
  153. reportRepositoryItem.Status = ReportRepositoryItem.State.TRANSMITTED;
  154. await reportRepositoryService.SaveReport(reportRepositoryItem);
  155. }
  156. return response;
  157. }
  158. }
  159. throw new HttpRequestException("HTTP client not initialized!");
  160. }
  161. await reportRepositoryService.SaveReport(reportRepositoryItem);
  162. return response;
  163. }
  164. }
  165. }