-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,812 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.dart_tool/ | ||
.packages | ||
|
||
# Generated files | ||
**.g.dart | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Open Meteo API - `fpdart` | ||
This is a re-implementation using `fpdart` and functional programming of the [Open Meteo API](https://github.com/felangel/bloc/tree/master/examples/flutter_weather/packages/open_meteo_api) from the [flutter_weather](https://bloclibrary.dev/#/flutterweathertutorial) app example in the [bloc](https://pub.dev/packages/bloc) package. | ||
|
||
The goal is to show a comparison between usual dart code and functional code written using `fpdart`. | ||
|
||
## Structure | ||
The example is simple but comprehensive. | ||
|
||
The Open Meteo API implementation is only 1 file. The original source is [open_meteo_api_client.dart](./lib/src/open_meteo_api_client.dart) (copy of the [bloc package implementation](https://github.com/felangel/bloc/blob/master/examples/flutter_weather/packages/open_meteo_api/lib/src/open_meteo_api_client.dart)). | ||
|
||
Inside [lib/src/fpdart](./lib/src/fpdart/) you can then find the refactoring using functional programming and `fpdart`: | ||
- [open_meteo_api_client_fpdart.dart](./lib/src/fpdart/open_meteo_api_client_fpdart.dart): implementation of the Open Meteo API with `fpdart` | ||
- [location_failure.dart](./lib/src/fpdart/location_failure.dart): failure classes for the `locationSearch` request | ||
- [weather_failure.dart](./lib/src/fpdart/weather_failure.dart): failure classes for the `getWeather` request | ||
|
||
### Test | ||
Also the [test](./test/) has been rewritten based on the `fpdart` refactoring: | ||
- [open_meteo_api_client_test.dart](./test/open_meteo_api_client_test.dart): Original Open Meteo API test implementation | ||
- [open_meteo_api_client_test_fpdart.dart](./test/open_meteo_api_client_test_fpdart.dart): Testing for the new implementation using `fpdart` and functional programming | ||
|
||
## Types used from `fpdart` | ||
- `TaskEither`: Used instead of `Future` to make async request that may fail | ||
- `Either`: Used to validate the response from the API with either an error or a valid value | ||
- `Option`: Used to get values that may be missing | ||
- `lookup` in a `Map`: getting a value by key in a `Map` may return nothing if the key is not found | ||
- `head` in a `List`: The list may be empty, so getting the first element (called _"head"_) may return nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
include: package:very_good_analysis/analysis_options.3.0.2.yaml | ||
analyzer: | ||
exclude: | ||
- lib/**/*.g.dart | ||
|
||
linter: | ||
rules: | ||
public_member_api_docs: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
targets: | ||
$default: | ||
builders: | ||
source_gen|combining_builder: | ||
options: | ||
ignore_for_file: | ||
- implicit_dynamic_parameter | ||
json_serializable: | ||
options: | ||
field_rename: snake | ||
create_to_json: false | ||
checked: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
library open_meteo_api; | ||
|
||
export 'src/fpdart/open_meteo_api_client_fpdart.dart'; | ||
export 'src/models/models.dart'; | ||
export 'src/open_meteo_api_client.dart'; |
61 changes: 61 additions & 0 deletions
61
example/open_meteo_api/lib/src/fpdart/location_failure.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:http/http.dart' as http; | ||
|
||
/// Abstract class which represents a failure in the `locationSearch` request. | ||
abstract class OpenMeteoApiFpdartLocationFailure {} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when **http request** fails | ||
class LocationHttpRequestFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure { | ||
const LocationHttpRequestFpdartFailure(this.object, this.stackTrace); | ||
final Object object; | ||
final StackTrace stackTrace; | ||
} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when request is not successful | ||
/// (`status != 200`) | ||
class LocationRequestFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure { | ||
const LocationRequestFpdartFailure(this.response); | ||
final http.Response response; | ||
} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when location response | ||
/// cannot be decoded from json. | ||
class LocationInvalidJsonDecodeFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure { | ||
const LocationInvalidJsonDecodeFpdartFailure(this.body); | ||
final String body; | ||
} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when location response is not a valid [Map]. | ||
class LocationInvalidMapFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure { | ||
const LocationInvalidMapFpdartFailure(this.json); | ||
final dynamic json; | ||
} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when location information | ||
/// is not found (missing expected key). | ||
class LocationKeyNotFoundFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure {} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when location data is not a valid [List]. | ||
class LocationInvalidListFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure { | ||
const LocationInvalidListFpdartFailure(this.value); | ||
final dynamic value; | ||
} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when location for provided location | ||
/// is not found (missing data). | ||
class LocationDataNotFoundFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure {} | ||
|
||
/// [OpenMeteoApiFpdartLocationFailure] when the response is not | ||
/// a valid [Location] | ||
class LocationFormattingFpdartFailure | ||
implements OpenMeteoApiFpdartLocationFailure { | ||
const LocationFormattingFpdartFailure(this.object, this.stackTrace); | ||
final Object object; | ||
final StackTrace stackTrace; | ||
} |
134 changes: 134 additions & 0 deletions
134
example/open_meteo_api/lib/src/fpdart/open_meteo_api_client_fpdart.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:open_meteo_api/open_meteo_api.dart'; | ||
import 'package:open_meteo_api/src/fpdart/location_failure.dart'; | ||
import 'package:open_meteo_api/src/fpdart/weather_failure.dart'; | ||
|
||
class OpenMeteoApiClientFpdart { | ||
OpenMeteoApiClientFpdart({http.Client? httpClient}) | ||
: _httpClient = httpClient ?? http.Client(); | ||
|
||
static const _baseUrlWeather = 'api.open-meteo.com'; | ||
static const _baseUrlGeocoding = 'geocoding-api.open-meteo.com'; | ||
|
||
final http.Client _httpClient; | ||
|
||
/// Finds a [Location] `/v1/search/?name=(query)`. | ||
TaskEither<OpenMeteoApiFpdartLocationFailure, Location> locationSearch( | ||
String query) => | ||
TaskEither<OpenMeteoApiFpdartLocationFailure, http.Response>.tryCatch( | ||
() => _httpClient.get( | ||
Uri.https( | ||
_baseUrlGeocoding, | ||
'/v1/search', | ||
{'name': query, 'count': '1'}, | ||
), | ||
), | ||
LocationHttpRequestFpdartFailure.new, | ||
) | ||
.chainEither( | ||
(response) => | ||
_validResponseBody(response, LocationRequestFpdartFailure.new), | ||
) | ||
.chainEither( | ||
(body) => Either.tryCatch( | ||
() => jsonDecode(body), | ||
(_, __) => LocationInvalidJsonDecodeFpdartFailure(body), | ||
), | ||
) | ||
.chainEither( | ||
(json) => Either<OpenMeteoApiFpdartLocationFailure, | ||
Map<dynamic, dynamic>>.safeCast( | ||
json, | ||
LocationInvalidMapFpdartFailure.new, | ||
), | ||
) | ||
.chainEither( | ||
(body) => body | ||
.lookup('results') | ||
.toEither(LocationKeyNotFoundFpdartFailure.new), | ||
) | ||
.chainEither( | ||
(currentWeather) => Either<OpenMeteoApiFpdartLocationFailure, | ||
List<dynamic>>.safeCast( | ||
currentWeather, | ||
LocationInvalidListFpdartFailure.new, | ||
), | ||
) | ||
.chainEither( | ||
(results) => | ||
results.head.toEither(LocationDataNotFoundFpdartFailure.new), | ||
) | ||
.chainEither( | ||
(weather) => Either.tryCatch( | ||
() => Location.fromJson(weather as Map<String, dynamic>), | ||
LocationFormattingFpdartFailure.new, | ||
), | ||
); | ||
|
||
/// Fetches [Weather] for a given [latitude] and [longitude]. | ||
TaskEither<OpenMeteoApiFpdartWeatherFailure, Weather> getWeather({ | ||
required double latitude, | ||
required double longitude, | ||
}) => | ||
TaskEither<OpenMeteoApiFpdartWeatherFailure, http.Response>.tryCatch( | ||
() async => _httpClient.get( | ||
Uri.https( | ||
_baseUrlWeather, | ||
'v1/forecast', | ||
{ | ||
'latitude': '$latitude', | ||
'longitude': '$longitude', | ||
'current_weather': 'true' | ||
}, | ||
), | ||
), | ||
WeatherHttpRequestFpdartFailure.new, | ||
) | ||
.chainEither( | ||
(response) => | ||
_validResponseBody(response, WeatherRequestFpdartFailure.new), | ||
) | ||
.chainEither( | ||
(body) => Either.safeCastStrict< | ||
OpenMeteoApiFpdartWeatherFailure, | ||
Map<dynamic, dynamic>, | ||
String>(body, WeatherInvalidMapFpdartFailure.new), | ||
) | ||
.chainEither( | ||
(body) => body | ||
.lookup('current_weather') | ||
.toEither(WeatherKeyNotFoundFpdartFailure.new), | ||
) | ||
.chainEither( | ||
(currentWeather) => Either<OpenMeteoApiFpdartWeatherFailure, | ||
List<dynamic>>.safeCast( | ||
currentWeather, | ||
WeatherInvalidListFpdartFailure.new, | ||
), | ||
) | ||
.chainEither( | ||
(results) => | ||
results.head.toEither(WeatherDataNotFoundFpdartFailure.new), | ||
) | ||
.chainEither( | ||
(weather) => Either.tryCatch( | ||
() => Weather.fromJson(weather as Map<String, dynamic>), | ||
WeatherFormattingFpdartFailure.new, | ||
), | ||
); | ||
|
||
/// Verify that the response status code is 200, | ||
/// and extract the response's body. | ||
Either<E, String> _validResponseBody<E>( | ||
http.Response response, | ||
E Function(http.Response) onError, | ||
) => | ||
Either<E, http.Response>.fromPredicate( | ||
response, | ||
(r) => r.statusCode == 200, | ||
onError, | ||
).map((r) => r.body); | ||
} |
Oops, something went wrong.