PWA Fundvelo der Caritas.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LFBicycleRest.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using cwebplusApp.Shared.Models;
  2. using Json.Net;
  3. using Microsoft.AspNetCore.Components;
  4. using Microsoft.Extensions.Configuration;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace cwebplusApp.Shared.Services {
  13. // REST interface responsible to submit lost or found reports and get the available master data.
  14. public class LFBicycleRest : ILFBicycleRest {
  15. private static readonly string VERSION = "v1";
  16. private HttpClient httpClient;
  17. [Inject]
  18. public IConfiguration Configuration { get; set; }
  19. public void Initialize(IConfiguration configuration) {
  20. this.Configuration = configuration;
  21. string hostBaseUrl = Configuration.GetValue<string>("host_base_url");
  22. if (!String.IsNullOrEmpty(hostBaseUrl)) {
  23. this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) };
  24. }
  25. }
  26. public async Task<List<ColorItem>> GetColors() {
  27. if (httpClient != null) {
  28. string subResourceUrl = Configuration.GetValue<string>("subresource_url_colors");
  29. if (!String.IsNullOrEmpty(subResourceUrl)) {
  30. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  31. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  32. ColorItem[] colors = JsonNet.Deserialize<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
  33. return new List<ColorItem>(colors);
  34. }
  35. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  36. }
  37. }
  38. throw new HttpRequestException("HTTP client not initialized!");
  39. }
  40. public async Task<List<BicycleType>> GetBicycleTypes() {
  41. if (httpClient != null) {
  42. string subResourceUrl = Configuration.GetValue<string>("subresource_url_types");
  43. if (!String.IsNullOrEmpty(subResourceUrl)) {
  44. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  45. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  46. BicycleType[] bicycleTypes = JsonNet.Deserialize<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
  47. return new List<BicycleType>(bicycleTypes);
  48. }
  49. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  50. }
  51. }
  52. throw new HttpRequestException("HTTP client not initialized!");
  53. }
  54. public async Task<List<Brand>> GetBrands() {
  55. if (httpClient != null) {
  56. string subResourceUrl = Configuration.GetValue<string>("subresource_url_brands");
  57. if (!String.IsNullOrEmpty(subResourceUrl)) {
  58. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  59. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  60. Brand[] brands = JsonNet.Deserialize<Brand[]>(await httpResult.Content.ReadAsStringAsync());
  61. return new List<Brand>(brands);
  62. }
  63. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  64. }
  65. }
  66. throw new HttpRequestException("HTTP client not initialized!");
  67. }
  68. public async Task<ReportResponse> SendFoundReport(FoundReport report) {
  69. if (httpClient != null) {
  70. string subResourceUrl = Configuration.GetValue<string>("subresource_url_foundreport");
  71. if (!String.IsNullOrEmpty(subResourceUrl)) {
  72. string reportJson = JsonNet.Serialize(report);
  73. HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json");
  74. HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content);
  75. string msg = await httpResult.Content.ReadAsStringAsync();
  76. ReportResponse response = JsonNet.Deserialize<ReportResponse>(msg);
  77. response.StatusCode = httpResult.StatusCode;
  78. //ReportResponse response = new();
  79. //response.StatusCode = System.Net.HttpStatusCode.OK;
  80. //Thread.Sleep(2000);
  81. return response;
  82. }
  83. }
  84. throw new HttpRequestException("HTTP client not initialized!");
  85. }
  86. public async Task<ReportResponse> SendMissingReport(MissingReport report) {
  87. if (httpClient != null) {
  88. string subResourceUrl = Configuration.GetValue<string>("subresource_url_missingreport");
  89. if (!String.IsNullOrEmpty(subResourceUrl)) {
  90. string reportJson = JsonNet.Serialize(report);
  91. HttpContent content = new StringContent(reportJson, Encoding.UTF8, "application/json");
  92. HttpResponseMessage httpResult = await httpClient.PostAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName), content);
  93. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  94. ReportResponse reponse = JsonNet.Deserialize<ReportResponse>(await httpResult.Content.ReadAsStringAsync());
  95. return reponse;
  96. }
  97. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  98. }
  99. }
  100. throw new HttpRequestException("HTTP client not initialized!");
  101. }
  102. }
  103. }