PWA Fundvelo der Caritas.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BicycleRestService.cs 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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<ReportResponse> SendFoundReport(Report report) {
  76. string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
  77. return await SendReport(report, subResourceUrl, new FoundReportRepositoryItem((FoundReport)report, ReportRepositoryService.GetCurrentTimeInMillis()));
  78. }
  79. public async Task<ReportResponse> SendFoundReport(FoundReportRepositoryItem reportRepositoryItem) {
  80. string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
  81. return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
  82. }
  83. public async Task<ReportResponse> SendMissingReport(Report report) {
  84. string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
  85. return await SendReport(report, subResourceUrl, new MissingReportRepositoryItem((MissingReport)report, ReportRepositoryService.GetCurrentTimeInMillis()));
  86. }
  87. public async Task<ReportResponse> SendMissingReport(MissingReportRepositoryItem reportRepositoryItem) {
  88. string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
  89. return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
  90. }
  91. public async Task<int> TrySendPendingReports() {
  92. int sent = 0;
  93. if (onlineStatusProvider.Online) {
  94. sent = await TrySendFoundPendingReports();
  95. sent += await TrySendMissingPendingReports();
  96. }
  97. return sent;
  98. }
  99. public async Task<int> TrySendFoundPendingReports() {
  100. int sent = 0;
  101. List<FoundReportRepositoryItem> pendingFoundReports = await reportRepositoryService.GetPendingFoundReports();
  102. foreach (FoundReportRepositoryItem item in pendingFoundReports) {
  103. try {
  104. ReportResponse response = await SendFoundReport(item);
  105. if (HttpStatusCode.OK == response.StatusCode) {
  106. sent++;
  107. }
  108. } catch (Exception) {
  109. continue;
  110. }
  111. }
  112. return sent;
  113. }
  114. public async Task<int> TrySendMissingPendingReports() {
  115. int sent = 0;
  116. List<MissingReportRepositoryItem> pendingMissingReports = await reportRepositoryService.GetPendingMissingReports();
  117. foreach (MissingReportRepositoryItem item in pendingMissingReports) {
  118. try {
  119. ReportResponse response = await SendMissingReport(item);
  120. if (HttpStatusCode.OK == response.StatusCode) {
  121. sent++;
  122. }
  123. } catch (Exception) {
  124. continue;
  125. }
  126. }
  127. return sent;
  128. }
  129. protected async Task<ReportResponse> SendReport(Report report, string subResourceUrl, ReportRepositoryItem reportRepositoryItem) {
  130. ReportResponse response = null;
  131. if (onlineStatusProvider.Online && !reportRepositoryItem.Transmitted) {
  132. if (httpClient != null) {
  133. if (!String.IsNullOrEmpty(subResourceUrl)) {
  134. string reportJson = JsonConvert.SerializeObject(report);
  135. HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json");
  136. HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content);
  137. string msg = await httpResult.Content.ReadAsStringAsync();
  138. response = JsonConvert.DeserializeObject<ReportResponse>(msg);
  139. response.StatusCode = httpResult.StatusCode;
  140. if (HttpStatusCode.OK == response.StatusCode) {
  141. reportRepositoryItem.ServerRefNbr = response.Data[0];
  142. reportRepositoryItem.Transmitted = true;
  143. await reportRepositoryService.SaveReport(reportRepositoryItem);
  144. }
  145. return response;
  146. }
  147. }
  148. throw new HttpRequestException("HTTP client not initialized!");
  149. }
  150. await reportRepositoryService.SaveReport(reportRepositoryItem);
  151. return response;
  152. }
  153. }
  154. }