-
Notifications
You must be signed in to change notification settings - Fork 46
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
1 parent
81d4b0d
commit 06fff82
Showing
7 changed files
with
65 additions
and
23 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
include: package:lints/recommended.yaml | ||
|
||
linter: | ||
rules: | ||
annotate_overrides: true | ||
|
||
analyzer: | ||
language: | ||
strict-casts: true | ||
strict-inference: true | ||
strict-raw-types: 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 |
---|---|---|
@@ -1,51 +1,74 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:poke_api/constants.dart'; | ||
import 'package:poke_api/pokemon.dart'; | ||
import 'package:poke_api/pokemon_error.dart'; | ||
|
||
abstract interface class HttpClient { | ||
String get(Uri uri); | ||
Effect<void, PokemonError, String> get(Uri uri); | ||
} | ||
|
||
Effect<(HttpClient, JsonCodec), PokemonError, Pokemon> program( | ||
class Http implements HttpClient { | ||
@override | ||
Effect<void, PokemonError, String> get(Uri uri) => Effect.gen( | ||
($) async { | ||
final response = await $.async(Effect.tryCatch( | ||
execute: () => http.get(uri), | ||
onError: (error, stackTrace) => const GetPokemonRequestError(), | ||
)); | ||
|
||
return response.body; | ||
}, | ||
); | ||
} | ||
|
||
typedef Env = (HttpClient, JsonCodec); | ||
|
||
Effect<Env, PokemonError, Pokemon> program( | ||
String pokemonId, | ||
) => | ||
Effect.gen(($) async { | ||
final (client, json) = $.sync(Effect.env()); | ||
|
||
final id = $.sync( | ||
Either.fromNullable( | ||
int.tryParse(pokemonId), | ||
PokemonIdNotInt.new, | ||
), | ||
); | ||
final id = $.sync(Either.fromNullable( | ||
int.tryParse(pokemonId), | ||
PokemonIdNotInt.new, | ||
).provide()); | ||
|
||
if (id < Constants.minimumPokemonId && id > Constants.maximumPokemonId) { | ||
return $.sync(Effect.fail(const InvalidPokemonIdRange())); | ||
} | ||
|
||
final uri = Uri.parse(Constants.requestAPIUrl(id)); | ||
final body = await $.async(Effect.tryCatch( | ||
execute: () => client.get(uri), | ||
onError: (_, __) => const GetPokemonRequestError(), | ||
)); | ||
final body = await $.async(client.get(uri).provideVoid()); | ||
|
||
final bodyJson = $.sync(Either.tryCatch( | ||
execute: () => json.decode(body), | ||
onError: (_, __) => const PokemonJsonDecodeError(), | ||
)); | ||
).provide()); | ||
|
||
final bodyJsonMap = $.sync<Map<String, dynamic>>( | ||
Either.safeCastStrict( | ||
Either.safeCastStrict<PokemonError, Map<String, dynamic>, dynamic>( | ||
bodyJson, | ||
(value) => const PokemonJsonInvalidMap(), | ||
), | ||
).provide(), | ||
); | ||
|
||
return $.sync(Effect.tryCatch( | ||
execute: () => Pokemon.fromJson(bodyJsonMap), | ||
onError: (_, __) => const PokemonInvalidJsonModel(), | ||
)); | ||
}); | ||
|
||
void main() async { | ||
await program("721") | ||
.map((pokemon) => print(pokemon)) | ||
.catchError( | ||
(error) => Effect.function( | ||
() => print("No pokemon: $error"), | ||
), | ||
) | ||
.runFuture((Http(), JsonCodec())); | ||
} |
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ environment: | |
sdk: ">=3.3.0 <4.0.0" | ||
|
||
dependencies: | ||
http: ^1.2.1 | ||
fpdart: | ||
path: ../../packages/fpdart | ||
|
||
|
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 |
---|---|---|
@@ -1,11 +1,8 @@ | ||
import 'dart:async'; | ||
|
||
extension FutureOrThenExtension<A> on FutureOr<A> { | ||
FutureOr<B> then<B>(FutureOr<B> Function(A a) f) { | ||
if (this is Future) { | ||
return (this as Future<A>).then(f); | ||
} | ||
|
||
return f(this as A); | ||
} | ||
FutureOr<B> then<B>(FutureOr<B> Function(A a) f) => switch (this) { | ||
final Future<A> self => self.then(f), | ||
final A self => f(self), | ||
}; | ||
} |