Skip to content

Commit

Permalink
api kaynagının hazırlanması ve model sınıfın olusturulması
Browse files Browse the repository at this point in the history
  • Loading branch information
emrealtunbilek committed Jun 18, 2019
1 parent 1dc216e commit faf1d33
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 1 deletion.
31 changes: 31 additions & 0 deletions lib/data/weather_api_client.dart
Original file line number Diff line number Diff line change
@@ -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<int> 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<Weather> 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);
}
}
5 changes: 5 additions & 0 deletions lib/data/weather_repository.dart
Original file line number Diff line number Diff line change
@@ -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<WeatherApiClient>();

Future<Weather> getWeather(String sehir) async {}
}
2 changes: 2 additions & 0 deletions lib/locator.dart
Original file line number Diff line number Diff line change
@@ -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());
}
185 changes: 184 additions & 1 deletion lib/models/weather.dart
Original file line number Diff line number Diff line change
@@ -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> consolidatedWeather;
DateTime time;
DateTime sunRise;
DateTime sunSet;
String timezoneName;
Parent parent;
List<dynamic> 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<String, dynamic> json) => new Weather(
consolidatedWeather: json["consolidated_weather"] == null
? null
: new List<ConsolidatedWeather>.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<dynamic>.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<String, dynamic> toJson() => {
"consolidated_weather": consolidatedWeather == null
? null
: new List<dynamic>.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<dynamic>.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<String, dynamic> 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<String, dynamic> 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<String, dynamic> json) => new Parent();

Map<String, dynamic> toJson() => {};
}
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit faf1d33

Please sign in to comment.