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.9KB

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