import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { environment } from '../../environments/environment' ; import { ICurrentWeather } from '../interfaces' ; interface ICurrentWeatherData { weather : [ { description : string ; icon : string ; } ] ; main : { temp : number ; } ; sys : { country : string ; } ; dt : number ; name : string ; } export interface IWeatherService { getCurrentWeather ( city , country ) : Observable < ICurrentWeather > ; } @ Injectable ( { providedIn : 'root' , } ) export class WeatherService implements IWeatherService { constructor ( private httpClient ) { } getCurrentWeather ( city , country ) { return this . httpClient . get < ICurrentWeatherData > ( `${ environment . baseUrl }api.openweathermap.org/data/2.5/weather?` + `q=${ city },${ country }&appid=${ environment . appId }` ) . pipe ( map ( data => this . transformToICurrentWeather ( data ) ) ) ; } private transformToICurrentWeather ( data ) { return { city : data . name , country : data . sys . country , date : data . dt * 1000 , image : `http://openweathermap.org/img/w/${ data . weather [ 0 ] . icon }.png` , temperature : this . convertKelvinToFahrenheit ( data . main . temp ) , description : data . weather [ 0 ] . description , } ; } private convertKelvinToFahrenheit ( kelvin ) { return kelvin * 9 / 5 - 459.67 ; } }