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

LFBicycleRest.cs 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Threading.Tasks;
  10. namespace cwebplusApp.Shared.Services {
  11. // REST interface responsible to submit lost or found reports and get the available master data.
  12. public class LFBicycleRest : ILFBicycleRest {
  13. private static readonly string VERSION = "v1";
  14. private HttpClient httpClient;
  15. [Inject]
  16. public IConfiguration Configuration { get; set; }
  17. public void Initialize(IConfiguration configuration) {
  18. this.Configuration = configuration;
  19. string hostBaseUrl = Configuration.GetValue<string>("host_base_url");
  20. if (!String.IsNullOrEmpty(hostBaseUrl)) {
  21. this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) };
  22. }
  23. }
  24. public async Task<List<ColorItem>> GetColors() {
  25. if (httpClient != null) {
  26. string subResourceUrl = Configuration.GetValue<string>("subresource_url_colors");
  27. if (!String.IsNullOrEmpty(subResourceUrl)) {
  28. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  29. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  30. ColorItem[] colors = JsonNet.Deserialize<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
  31. return new List<ColorItem>(colors);
  32. }
  33. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  34. }
  35. }
  36. throw new HttpRequestException("HTTP client not initialized!");
  37. }
  38. public async Task<List<BicycleType>> GetBicycleTypes() {
  39. if (httpClient != null) {
  40. string subResourceUrl = Configuration.GetValue<string>("subresource_url_types");
  41. if (!String.IsNullOrEmpty(subResourceUrl)) {
  42. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  43. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  44. BicycleType[] bicycleTypes = JsonNet.Deserialize<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
  45. return new List<BicycleType>(bicycleTypes);
  46. }
  47. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  48. }
  49. }
  50. throw new HttpRequestException("HTTP client not initialized!");
  51. }
  52. public async Task<List<Brand>> GetBrands() {
  53. if (httpClient != null) {
  54. string subResourceUrl = Configuration.GetValue<string>("subresource_url_brands");
  55. if (!String.IsNullOrEmpty(subResourceUrl)) {
  56. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
  57. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  58. Brand[] brands = JsonNet.Deserialize<Brand[]>(await httpResult.Content.ReadAsStringAsync());
  59. return new List<Brand>(brands);
  60. }
  61. throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
  62. }
  63. }
  64. throw new HttpRequestException("HTTP client not initialized!");
  65. }
  66. }
  67. }