diff --git a/lib/data/weather_api_client.dart b/lib/data/weather_api_client.dart new file mode 100644 index 0000000..ae09258 --- /dev/null +++ b/lib/data/weather_api_client.dart @@ -0,0 +1,31 @@ +import 'package:flutter_weatherapp_with_bloc/models/weather.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; + +class WeatherApiClient { + static const baseUrl = "https://www.metaweather.com"; + final http.Client httpClient = http.Client(); + + Future getLocationID(String sehirAdi) async { + final sehirURL = baseUrl + "/api/location/search/?query=" + sehirAdi; + final gelenCevap = await httpClient.get(sehirURL); + + if (gelenCevap.statusCode != 200) { + throw Exception("Veri Getirilemedi"); + } + + final gelenCevapJSON = (jsonDecode(gelenCevap.body)) as List; + return gelenCevapJSON[0]["woeid"]; + } + + Future getWeather(int sehirID) async { + final havaDurumuURL = baseUrl + "/api/location/$sehirID"; + final havaDurumuGelenCevap = await httpClient.get(havaDurumuURL); + if (havaDurumuGelenCevap.statusCode != 200) { + throw Exception("Hava durumu Getirilemedi"); + } + + final havaDurumuCevapJSON = jsonDecode(havaDurumuGelenCevap.body); + return Weather.fromJson(havaDurumuCevapJSON); + } +} diff --git a/lib/data/weather_repository.dart b/lib/data/weather_repository.dart index 970fc88..7928862 100644 --- a/lib/data/weather_repository.dart +++ b/lib/data/weather_repository.dart @@ -1,5 +1,10 @@ import 'package:flutter_weatherapp_with_bloc/models/weather.dart'; +import '../locator.dart'; +import 'weather_api_client.dart'; + class WeatherRepository { + WeatherApiClient weatherApiClient = locator(); + Future getWeather(String sehir) async {} } diff --git a/lib/locator.dart b/lib/locator.dart index 97321b6..28f0f6e 100644 --- a/lib/locator.dart +++ b/lib/locator.dart @@ -1,9 +1,11 @@ import 'package:get_it/get_it.dart'; +import 'data/weather_api_client.dart'; import 'data/weather_repository.dart'; GetIt locator = GetIt(); void setupLocator() { locator.registerLazySingleton(() => WeatherRepository()); + locator.registerLazySingleton(() => WeatherApiClient()); } diff --git a/lib/models/weather.dart b/lib/models/weather.dart index f7d7c36..db55678 100644 --- a/lib/models/weather.dart +++ b/lib/models/weather.dart @@ -1 +1,184 @@ -class Weather {} +// To parse this JSON data, do +// +// final weather = weatherFromJson(jsonString); +import 'dart:convert'; + +Weather weatherFromJson(String str) => Weather.fromJson(json.decode(str)); + +String weatherToJson(Weather data) => json.encode(data.toJson()); + +class Weather { + List consolidatedWeather; + DateTime time; + DateTime sunRise; + DateTime sunSet; + String timezoneName; + Parent parent; + List sources; + String title; + String locationType; + int woeid; + String lattLong; + String timezone; + + Weather({ + this.consolidatedWeather, + this.time, + this.sunRise, + this.sunSet, + this.timezoneName, + this.parent, + this.sources, + this.title, + this.locationType, + this.woeid, + this.lattLong, + this.timezone, + }); + + factory Weather.fromJson(Map json) => new Weather( + consolidatedWeather: json["consolidated_weather"] == null + ? null + : new List.from(json["consolidated_weather"] + .map((x) => ConsolidatedWeather.fromJson(x))), + time: json["time"] == null ? null : DateTime.parse(json["time"]), + sunRise: + json["sun_rise"] == null ? null : DateTime.parse(json["sun_rise"]), + sunSet: + json["sun_set"] == null ? null : DateTime.parse(json["sun_set"]), + timezoneName: + json["timezone_name"] == null ? null : json["timezone_name"], + parent: json["parent"] == null ? null : Parent.fromJson(json["parent"]), + sources: json["sources"] == null + ? null + : new List.from(json["sources"].map((x) => x)), + title: json["title"] == null ? null : json["title"], + locationType: + json["location_type"] == null ? null : json["location_type"], + woeid: json["woeid"] == null ? null : json["woeid"], + lattLong: json["latt_long"] == null ? null : json["latt_long"], + timezone: json["timezone"] == null ? null : json["timezone"], + ); + + Map toJson() => { + "consolidated_weather": consolidatedWeather == null + ? null + : new List.from( + consolidatedWeather.map((x) => x.toJson())), + "time": time == null ? null : time.toIso8601String(), + "sun_rise": sunRise == null ? null : sunRise.toIso8601String(), + "sun_set": sunSet == null ? null : sunSet.toIso8601String(), + "timezone_name": timezoneName == null ? null : timezoneName, + "parent": parent == null ? null : parent.toJson(), + "sources": sources == null + ? null + : new List.from(sources.map((x) => x)), + "title": title == null ? null : title, + "location_type": locationType == null ? null : locationType, + "woeid": woeid == null ? null : woeid, + "latt_long": lattLong == null ? null : lattLong, + "timezone": timezone == null ? null : timezone, + }; +} + +class ConsolidatedWeather { + int id; + String weatherStateName; + String weatherStateAbbr; + String windDirectionCompass; + DateTime created; + DateTime applicableDate; + double minTemp; + double maxTemp; + double theTemp; + double windSpeed; + double windDirection; + double airPressure; + int humidity; + double visibility; + int predictability; + + ConsolidatedWeather({ + this.id, + this.weatherStateName, + this.weatherStateAbbr, + this.windDirectionCompass, + this.created, + this.applicableDate, + this.minTemp, + this.maxTemp, + this.theTemp, + this.windSpeed, + this.windDirection, + this.airPressure, + this.humidity, + this.visibility, + this.predictability, + }); + + factory ConsolidatedWeather.fromJson(Map json) => + new ConsolidatedWeather( + id: json["id"] == null ? null : json["id"], + weatherStateName: json["weather_state_name"] == null + ? null + : json["weather_state_name"], + weatherStateAbbr: json["weather_state_abbr"] == null + ? null + : json["weather_state_abbr"], + windDirectionCompass: json["wind_direction_compass"] == null + ? null + : json["wind_direction_compass"], + created: + json["created"] == null ? null : DateTime.parse(json["created"]), + applicableDate: json["applicable_date"] == null + ? null + : DateTime.parse(json["applicable_date"]), + minTemp: json["min_temp"] == null ? null : json["min_temp"].toDouble(), + maxTemp: json["max_temp"] == null ? null : json["max_temp"].toDouble(), + theTemp: json["the_temp"] == null ? null : json["the_temp"].toDouble(), + windSpeed: + json["wind_speed"] == null ? null : json["wind_speed"].toDouble(), + windDirection: json["wind_direction"] == null + ? null + : json["wind_direction"].toDouble(), + airPressure: json["air_pressure"] == null + ? null + : json["air_pressure"].toDouble(), + humidity: json["humidity"] == null ? null : json["humidity"], + visibility: + json["visibility"] == null ? null : json["visibility"].toDouble(), + predictability: + json["predictability"] == null ? null : json["predictability"], + ); + + Map toJson() => { + "id": id == null ? null : id, + "weather_state_name": + weatherStateName == null ? null : weatherStateName, + "weather_state_abbr": + weatherStateAbbr == null ? null : weatherStateAbbr, + "wind_direction_compass": + windDirectionCompass == null ? null : windDirectionCompass, + "created": created == null ? null : created.toIso8601String(), + "applicable_date": applicableDate == null + ? null + : "${applicableDate.year.toString().padLeft(4, '0')}-${applicableDate.month.toString().padLeft(2, '0')}-${applicableDate.day.toString().padLeft(2, '0')}", + "min_temp": minTemp == null ? null : minTemp, + "max_temp": maxTemp == null ? null : maxTemp, + "the_temp": theTemp == null ? null : theTemp, + "wind_speed": windSpeed == null ? null : windSpeed, + "wind_direction": windDirection == null ? null : windDirection, + "air_pressure": airPressure == null ? null : airPressure, + "humidity": humidity == null ? null : humidity, + "visibility": visibility == null ? null : visibility, + "predictability": predictability == null ? null : predictability, + }; +} + +class Parent { + Parent(); + + factory Parent.fromJson(Map json) => new Parent(); + + Map toJson() => {}; +} diff --git a/pubspec.lock b/pubspec.lock index 4d09f29..027a693 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -74,6 +74,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.3+1" + http: + dependency: "direct main" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.0+2" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.3" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index add1c7a..3aa4fb9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,6 +26,7 @@ dependencies: flutter_bloc: ^0.17.0 equatable: ^0.2.6 get_it: ^1.0.3+1 + http: ^0.12.0+2 dev_dependencies: flutter_test: