PWA Fundvelo der Caritas.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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<List<ZipCity>> GetZipCities() {
  87. if (httpClient != null) {
  88. string subResourceUrl = configuration.GetValue<string>("subresource_url_zipcities");
  89. if (!String.IsNullOrEmpty(subResourceUrl)) {
  90. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  91. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  92. string[] zipCitiesStr = JsonConvert.DeserializeObject<string[]>(await httpResult.Content.ReadAsStringAsync());
  93. List<ZipCity> zipCities = new ();
  94. for (int i = 0; i < zipCitiesStr.Length; i++) {
  95. zipCities.Add(new ZipCity(zipCitiesStr[i]));
  96. }
  97. return zipCities;
  98. }
  99. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  100. }
  101. }
  102. throw new HttpRequestException("HTTP client not initialized!");
  103. }
  104. public async Task<ReportResponse> SendFoundReport(Report report) {
  105. string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
  106. return await SendReport(report, subResourceUrl, new FoundReportRepositoryItem((FoundReport)report, 0));
  107. }
  108. public async Task<ReportResponse> SendFoundReport(FoundReportRepositoryItem reportRepositoryItem) {
  109. string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
  110. return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
  111. }
  112. public async Task<ReportResponse> SendMissingReport(Report report) {
  113. string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
  114. return await SendReport(report, subResourceUrl, new MissingReportRepositoryItem((MissingReport)report, 0));
  115. }
  116. public async Task<ReportResponse> SendMissingReport(MissingReportRepositoryItem reportRepositoryItem) {
  117. string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
  118. return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
  119. }
  120. public async Task<int> TrySendPendingReports() {
  121. int sent = 0;
  122. if (onlineStatusProvider.Online) {
  123. sent = await TrySendFoundPendingReports();
  124. sent += await TrySendMissingPendingReports();
  125. }
  126. return sent;
  127. }
  128. public async Task<int> TrySendFoundPendingReports() {
  129. int sent = 0;
  130. List<FoundReportRepositoryItem> pendingFoundReports = await reportRepositoryService.GetPendingFoundReports();
  131. foreach (FoundReportRepositoryItem item in pendingFoundReports) {
  132. try {
  133. ReportResponse response = await SendFoundReport(item);
  134. if (HttpStatusCode.OK == response.StatusCode) {
  135. sent++;
  136. }
  137. } catch (Exception) {
  138. continue;
  139. }
  140. }
  141. return sent;
  142. }
  143. public async Task<int> TrySendMissingPendingReports() {
  144. int sent = 0;
  145. List<MissingReportRepositoryItem> pendingMissingReports = await reportRepositoryService.GetPendingMissingReports();
  146. foreach (MissingReportRepositoryItem item in pendingMissingReports) {
  147. try {
  148. ReportResponse response = await SendMissingReport(item);
  149. if (HttpStatusCode.OK == response.StatusCode) {
  150. sent++;
  151. }
  152. } catch (Exception) {
  153. continue;
  154. }
  155. }
  156. return sent;
  157. }
  158. protected async Task<ReportResponse> SendReport(Report report, string subResourceUrl, ReportRepositoryItem reportRepositoryItem) {
  159. ReportResponse response = null;
  160. if (onlineStatusProvider.Online && ReportRepositoryItem.State.PENDING.Equals(reportRepositoryItem.Status)) {
  161. if (httpClient != null) {
  162. if (!String.IsNullOrEmpty(subResourceUrl)) {
  163. string reportJson = JsonConvert.SerializeObject(report);
  164. HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json");
  165. HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content);
  166. string msg = await httpResult.Content.ReadAsStringAsync();
  167. response = JsonConvert.DeserializeObject<ReportResponse>(msg);
  168. response.StatusCode = httpResult.StatusCode;
  169. if (HttpStatusCode.OK == response.StatusCode) {
  170. reportRepositoryItem.ServerRefNbr = response.Data[0];
  171. reportRepositoryItem.Status = ReportRepositoryItem.State.TRANSMITTED;
  172. await reportRepositoryService.SaveReport(reportRepositoryItem);
  173. }
  174. return response;
  175. }
  176. }
  177. throw new HttpRequestException("HTTP client not initialized!");
  178. }
  179. await reportRepositoryService.SaveReport(reportRepositoryItem);
  180. return response;
  181. }
  182. }
  183. }