| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using cwebplusApp.Shared.Models;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace cwebplusApp.Shared.Services {
-
- // REST interface responsible to submit lost or found reports and get the available master data.
- public class BicycleRestService : IBicycleRestService {
-
- private static readonly string VERSION = "v1";
-
- private HttpClient httpClient;
- private IConfiguration configuration;
- private OnlineStatusProvider onlineStatusProvider;
- private ReportRepositoryService reportRepositoryService;
- private ReportDataProvider reportDataProvider;
-
- public BicycleRestService(ReportRepositoryService _reportRepositoryService, OnlineStatusProvider _onlineStatusProvider,
- ReportDataProvider _reportDataProvider) {
- this.reportRepositoryService = _reportRepositoryService;
- this.onlineStatusProvider = _onlineStatusProvider;
- this.reportDataProvider = _reportDataProvider;
- }
-
- public void Initialize(IConfiguration _configuration) {
- this.configuration = _configuration;
-
- string hostBaseUrl = configuration.GetValue<string>("host_base_url");
- if (!String.IsNullOrEmpty(hostBaseUrl)) {
- this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) };
- }
- }
-
- public async Task<List<ColorItem>> GetColors() {
- if (httpClient != null) {
- string subResourceUrl = configuration.GetValue<string>("subresource_url_colors");
- if (!String.IsNullOrEmpty(subResourceUrl)) {
- HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
-
- if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
- ColorItem[] colors = JsonConvert.DeserializeObject<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
- return new List<ColorItem>(colors);
- }
- throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- }
-
- public async Task<List<BicycleType>> GetBicycleTypes() {
- if (httpClient != null) {
- string subResourceUrl = configuration.GetValue<string>("subresource_url_types");
- if (!String.IsNullOrEmpty(subResourceUrl)) {
- HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
-
- if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
- BicycleType[] bicycleTypes = JsonConvert.DeserializeObject<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
- return new List<BicycleType>(bicycleTypes);
- }
- throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- }
-
- public async Task<List<Brand>> GetBrands() {
- if (httpClient != null) {
- string subResourceUrl = configuration.GetValue<string>("subresource_url_brands");
- if (!String.IsNullOrEmpty(subResourceUrl)) {
- HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
-
- if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
- Brand[] brands = JsonConvert.DeserializeObject<Brand[]>(await httpResult.Content.ReadAsStringAsync());
- return new List<Brand>(brands);
- }
- throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- }
-
- public async Task<ReportResponse> SendFoundReport(Report report) {
- string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
- return await SendReport(report, subResourceUrl, new FoundReportRepositoryItem((FoundReport)report, 0));
- }
-
- public async Task<ReportResponse> SendFoundReport(FoundReportRepositoryItem reportRepositoryItem) {
- string subResourceUrl = configuration.GetValue<string>("subresource_url_foundreport");
- return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
- }
-
- public async Task<ReportResponse> SendMissingReport(Report report) {
- string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
- return await SendReport(report, subResourceUrl, new MissingReportRepositoryItem((MissingReport)report, 0));
- }
-
- public async Task<ReportResponse> SendMissingReport(MissingReportRepositoryItem reportRepositoryItem) {
- string subResourceUrl = configuration.GetValue<string>("subresource_url_missingreport");
- return await SendReport(reportRepositoryItem.Report, subResourceUrl, reportRepositoryItem);
- }
-
- public async Task<int> TrySendPendingReports() {
- int sent = 0;
- if (onlineStatusProvider.Online) {
- sent = await TrySendFoundPendingReports();
- sent += await TrySendMissingPendingReports();
- }
- return sent;
- }
-
- public async Task<int> TrySendFoundPendingReports() {
- int sent = 0;
- List<FoundReportRepositoryItem> pendingFoundReports = await reportRepositoryService.GetPendingFoundReports();
- foreach (FoundReportRepositoryItem item in pendingFoundReports) {
- try {
- ReportResponse response = await SendFoundReport(item);
- if (HttpStatusCode.OK == response.StatusCode) {
- sent++;
- }
- } catch (Exception) {
- continue;
- }
- }
- return sent;
- }
-
- public async Task<int> TrySendMissingPendingReports() {
- int sent = 0;
- List<MissingReportRepositoryItem> pendingMissingReports = await reportRepositoryService.GetPendingMissingReports();
- foreach (MissingReportRepositoryItem item in pendingMissingReports) {
- try {
- ReportResponse response = await SendMissingReport(item);
- if (HttpStatusCode.OK == response.StatusCode) {
- sent++;
- }
- } catch (Exception) {
- continue;
- }
- }
- return sent;
- }
-
- protected async Task<ReportResponse> SendReport(Report report, string subResourceUrl, ReportRepositoryItem reportRepositoryItem) {
- ReportResponse response = null;
- if (onlineStatusProvider.Online && ReportRepositoryItem.State.PENDING.Equals(reportRepositoryItem.Status)) {
- if (httpClient != null) {
- if (!String.IsNullOrEmpty(subResourceUrl)) {
- string reportJson = JsonConvert.SerializeObject(report);
- HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json");
- HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content);
- string msg = await httpResult.Content.ReadAsStringAsync();
- response = JsonConvert.DeserializeObject<ReportResponse>(msg);
- response.StatusCode = httpResult.StatusCode;
- if (HttpStatusCode.OK == response.StatusCode) {
- reportRepositoryItem.ServerRefNbr = response.Data[0];
- reportRepositoryItem.Status = ReportRepositoryItem.State.TRANSMITTED;
- await reportRepositoryService.SaveReport(reportRepositoryItem);
- }
- return response;
- }
- }
- throw new HttpRequestException("HTTP client not initialized!");
- }
- await reportRepositoryService.SaveReport(reportRepositoryItem);
- return response;
- }
-
-
- }
- }
|