BlazorWeather/WeatherClient/WeatherService.cs

46 строки
2.5 KiB
C#
Исходник Обычный вид История

2021-05-01 03:48:05 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http.Json;
2021-05-04 12:21:54 +03:00
namespace WeatherClient
2021-05-01 03:48:05 +03:00
{
public class WeatherService : IWeatherService
{
static List<Location> locations = new()
{
2021-05-05 03:06:12 +03:00
new Location { Name = "Boston", Country = "USA", Coordinate = new Coordinate(42.3601, -71.0589) },
2021-05-05 00:40:20 +03:00
new Location { Name = "Redmond", Country = "USA", Coordinate = new Coordinate(47.6740, -122.1215) },
new Location { Name = "St. Louis", Country = "USA", Coordinate = new Coordinate(38.6270, -90.1994) },
new Location { Name = "NYC", Country = "USA", Coordinate = new Coordinate(40.7128, -74.0060) },
2021-05-04 12:21:54 +03:00
new Location { Name = "Amsterdam", Country = "Netherlands", Coordinate = new Coordinate(52.3676, 4.9041) },
new Location { Name = "Seoul", Country = "South Korea", Coordinate = new Coordinate(37.5665, 126.9780) },
2021-05-05 00:40:20 +03:00
new Location { Name = "Johannesburg", Country = "South Africa", Coordinate = new Coordinate(-26.2041, 28.0473) },
new Location { Name = "Rio de Janeiro", Country = "Brazil", Coordinate = new Coordinate(-22.9068, -43.1729) },
new Location { Name = "Madrid", Country = "Spain", Coordinate = new Coordinate(40.4168, -3.7038) },
new Location { Name = "Buenos Aires", Country = "Argentina", Coordinate = new Coordinate(-34.6037, -58.3816) },
new Location { Name = "Punta Cana", Country = "Dominican Republic", Coordinate = new Coordinate(18.5601, -68.3725) },
2021-05-04 12:21:54 +03:00
new Location { Name = "Hyderabad", Country = "India", Coordinate = new Coordinate(17.3850, 78.4867) },
2021-05-05 00:40:20 +03:00
new Location { Name = "San Francisco", Country = "USA", Coordinate = new Coordinate(37.7749, -122.4194) },
new Location { Name = "Nairobi", Country = "Kenya", Coordinate = new Coordinate(-1.2921, 36.8219) },
2021-05-04 12:21:54 +03:00
new Location { Name = "Lagos", Country = "Nigeria", Coordinate = new Coordinate(6.5244, 3.3792) }
2021-05-01 03:48:05 +03:00
};
private readonly HttpClient httpClient;
public WeatherService(HttpClient httpClient)
{
this.httpClient = httpClient;
}
2021-05-05 03:06:12 +03:00
public Task<IEnumerable<Location>> GetLocations(string query = null)
=> Task.FromResult(query == null ? locations : locations.Where(l => l.Name.Contains(query)));
2021-05-01 03:48:05 +03:00
public Task<WeatherResponse> GetWeather(Coordinate location)
=> httpClient.GetFromJsonAsync<WeatherResponse>($"/weather/{location}");
}
}