Advertisement
tommarek_CZE

ASP.NET Weather Example API for HiddenDevs C# application

May 5th, 2024
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System.Net.Http;
  7. using Newtonsoft.Json;
  8.  
  9. namespace testWebAplication
  10. {
  11.     [ApiController]
  12.     [Route("[controller]")]
  13.     public class WeatherForecastController : ControllerBase
  14.     {
  15.  
  16.         [HttpGet(Name = "GetWeatherForecast")]
  17.  
  18.         // Asyncrhonous function thats return ASP.NET api data from outside API
  19.         public async Task<IEnumerable<WeatherForecast>> Get()
  20.         {
  21.             var apiUrl = "10.10.0.45/getSensorsData";
  22.             var weatherForecasts = await GetData(apiUrl);
  23.  
  24.             return weatherForecasts;
  25.         }
  26.  
  27.         // Asynchronous function that fetch all data from outside api and returns it
  28.         private async Task<IEnumerable<WeatherForecast>> GetData(string apiUrl)
  29.         {
  30.             // Create http client that fetches the data from outside api and awaites reponse
  31.             using (HttpClient client = new HttpClient())
  32.             {
  33.                 HttpResponseMessage response = await client.GetAsync(apiUrl);
  34.  
  35.                 // If response retiturn succes code, desearize and assing fetched data from the outside api
  36.                 if (response.IsSuccessStatusCode)
  37.                 {
  38.                     // Get json from response and deseralize it
  39.                     string json = await response.Content.ReadAsStringAsync();
  40.                     var data = JsonConvert.DeserializeObject<Data>(json);
  41.  
  42.                     // Assing all extracted varriables to the api varriables
  43.                     var weatherForecasts = data.extractedData.Select(item => new WeatherForecast
  44.                     {
  45.                         localDate = DateOnly.FromDateTime(DateTime.Now),
  46.                         localTime = TimeOnly.FromDateTime(DateTime.Now),
  47.                         lat = Convert.ToInt32(item.Name == "lat" ? item.Varriable : null),
  48.                         lon = Convert.ToInt32(item.Name == "lon" ? item.Varriable : null),
  49.                         country = item.Name == "country" ? Convert.ToString(item.Varriable) : null,
  50.                         region = item.Name == "region" ? Convert.ToString(item.Varriable) : null,
  51.                         city = item.Name == "city" ? Convert.ToString(item.Varriable) : null,
  52.                         celsiusTemperature = item.Name == "celsiusTemperature" ? Convert.ToInt32(item.Varriable) : 0,
  53.                         windDegree = item.Name == "windDegree" ? Convert.ToInt32(item.Varriable) : 0,
  54.                         windDir = item.Name == "windDir" ? Convert.ToString(item.Varriable) : null,
  55.                         co = item.Name == "co" ? Convert.ToInt32(item.Varriable) : 0,
  56.                         o3 = item.Name == "o3" ? Convert.ToInt32(item.Varriable) : 0,
  57.                         windKm = item.Name == "windKm" ? Convert.ToInt32(item.Varriable) : 0,
  58.                         clouds = item.Name == "clouds" ? Convert.ToInt32(item.Varriable) : 0,
  59.                         pm10 = item.Name == "pm10" ? Convert.ToInt32(item.Varriable) : 0,
  60.                         uvSensor = item.Name == "uvSensor" ? Convert.ToInt32(item.Varriable) : 0,
  61.                         condition = item.Name == "condition" ? Convert.ToString(item.Varriable) : null
  62.                     });
  63.  
  64.                     return weatherForecasts;
  65.                 }
  66.                 else
  67.                 {
  68.                     return null;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     // Define classes for json deserialization
  75.     public class Data
  76.     {
  77.         [JsonProperty("weatherData")]
  78.         public apiData[] extractedData { get; set; }
  79.     }
  80.  
  81.     public class apiData
  82.     {
  83.         [JsonProperty("name")]
  84.         public string Name { get; set; }
  85.  
  86.         [JsonProperty("varriable")]
  87.         public object Varriable { get; set; }
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement