weather.service.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {Injectable} from '@angular/core';
  2. import {HttpClient} from '@angular/common/http';
  3. import {Observable} from 'rxjs';
  4. import {environment} from '@environments/environment';
  5. import {first, map} from 'rxjs/operators';
  6. @Injectable({
  7. providedIn: 'root'
  8. })
  9. export class WeatherService {
  10. private readonly baseURL = 'https://api.openweathermap.org/data/2.5/weather?q=';
  11. private readonly forcastURL = 'https://api.openweathermap.org/data/2.5/forecast?q=';
  12. private readonly appID = environment.appID;
  13. constructor(public http: HttpClient) {
  14. }
  15. getWeather(city: string, metric: 'metric' | 'imperial' = 'metric'): Observable<any> {
  16. return this.http.get(
  17. `${this.baseURL}${city}&units=${metric}&APPID=${this.appID}&lang=ES`).pipe();
  18. }
  19. getForecast(city: string, metric: 'metric' | 'imperial' = 'metric'): Observable<any> {
  20. return this.http.get(
  21. `${this.forcastURL}${city}&units=${metric}&APPID=${this.appID}`)
  22. .pipe(first(), map((weather) => weather['list']));
  23. }
  24. // [0].main
  25. // getWeatherState
  26. //
  27. // getCurrentTemp
  28. // Math.round(Number(weather.main.temp))
  29. //
  30. //
  31. // getCurrentHum
  32. // weather.main.humidity
  33. //
  34. //
  35. // getCurrentWind
  36. // Math.round(Math.round(weather.wind.speed))
  37. }