From bda39348e0feb61a94d88d8582d6e33bce24f4be Mon Sep 17 00:00:00 2001 From: SandroMaglione Date: Mon, 18 Mar 2024 17:41:37 +0900 Subject: [PATCH] http request example --- examples/fpdart_http/lib/api.dart | 30 ++++++++++++++++++++++++ examples/fpdart_http/lib/http_error.dart | 11 +++++++++ examples/fpdart_http/lib/main.dart | 18 ++++++++++++++ examples/fpdart_http/pubspec.yaml | 19 +++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 examples/fpdart_http/lib/api.dart create mode 100644 examples/fpdart_http/lib/http_error.dart create mode 100644 examples/fpdart_http/lib/main.dart create mode 100644 examples/fpdart_http/pubspec.yaml diff --git a/examples/fpdart_http/lib/api.dart b/examples/fpdart_http/lib/api.dart new file mode 100644 index 0000000..73ed164 --- /dev/null +++ b/examples/fpdart_http/lib/api.dart @@ -0,0 +1,30 @@ +import 'package:fpdart/fpdart.dart'; +import 'package:http/http.dart' as http; + +import 'http_error.dart'; + +/// 1️⃣ Define dependencies, errors, response +Effect get( + Uri url, { + Map? headers, +}) => + + /// 2️⃣ Use the Do notation with the `gen` constructor + Effect.gen((_) async { + /// 3️⃣ Extract the dependency using `env` (environment) + final client = await _(Effect.env()); + + /// 4️⃣ Perform a request, catch errors, extract the response + final response = await _(Effect.tryCatch( + execute: () => client.get(url, headers: headers), + onError: (_, __) => const RequestError(), + )); + + /// 5️⃣ Use plain dart code to check for valid status + if (response.statusCode != 200) { + return await _(Effect.fail(const ResponseError())); + } + + /// 6️⃣ Return extracted/valid response + return response; + }); diff --git a/examples/fpdart_http/lib/http_error.dart b/examples/fpdart_http/lib/http_error.dart new file mode 100644 index 0000000..c7f9391 --- /dev/null +++ b/examples/fpdart_http/lib/http_error.dart @@ -0,0 +1,11 @@ +sealed class HttpError { + const HttpError(); +} + +final class RequestError extends HttpError { + const RequestError(); +} + +final class ResponseError extends HttpError { + const ResponseError(); +} diff --git a/examples/fpdart_http/lib/main.dart b/examples/fpdart_http/lib/main.dart new file mode 100644 index 0000000..ce8caf5 --- /dev/null +++ b/examples/fpdart_http/lib/main.dart @@ -0,0 +1,18 @@ +import 'package:fpdart/fpdart.dart'; +import 'package:http/http.dart' as http; + +import 'api.dart'; + +void main() async { + final main = await get( + Uri.https("pokeapi.co", "/api/v2/pokemon/10"), + ) + .tap( + (response) => Effect.function( + () => print(response.body), + ), + ) + .runFuture( + http.Client(), + ); +} diff --git a/examples/fpdart_http/pubspec.yaml b/examples/fpdart_http/pubspec.yaml new file mode 100644 index 0000000..3862015 --- /dev/null +++ b/examples/fpdart_http/pubspec.yaml @@ -0,0 +1,19 @@ +name: fpdart_http +description: > + Example of using fpdart with http. +version: 2.0.0 +homepage: https://www.sandromaglione.com/ +repository: https://github.com/SandroMaglione/fpdart +publish_to: "none" + +environment: + sdk: ">=3.3.0 <4.0.0" + +dependencies: + http: ^1.2.1 + fpdart: + path: ../../packages/fpdart + +dev_dependencies: + lints: ^2.0.1 + test: ^1.23.1