diff --git a/packages/wyatt_http_client/example/example.dart b/packages/wyatt_http_client/example/example.dart new file mode 100644 index 00000000..a5c529ea --- /dev/null +++ b/packages/wyatt_http_client/example/example.dart @@ -0,0 +1,78 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_http_client/wyatt_http_client.dart'; + +Future testSimpleGet() async { + print('testSimpleGet'); + final pipeline = Pipeline() + ..addMiddleware(const BodyToJsonMiddleware()) + ..addMiddleware(const SimpleLoggerMiddleware()); + + final client = MiddlewareClient(pipeline: pipeline); + + final response = await client + .get(Uri.parse('https://jsonplaceholder.typicode.com/todos/1')); + print(response.body); +} + +Future testUriPrefix() async { + print('testUriPrefix'); + final pipeline = Pipeline() + ..addMiddleware( + const UriPrefixMiddleware( + protocol: Protocols.https, + authority: 'jsonplaceholder.typicode.com', + ), + ) + ..addMiddleware(const BodyToJsonMiddleware()) + ..addMiddleware(const SimpleLoggerMiddleware()); + + final client = MiddlewareClient(pipeline: pipeline); + + final response = await client.get(Uri.parse('/todos/1')); + print(response.body); +} + +Future testBasicAuth() async { + print('testBasicAuth'); + final pipeline = Pipeline() + ..addMiddleware( + const BasicAuthMiddleware( + username: 'guest', + password: 'guest', + ), + ) + ..addMiddleware(const BodyToJsonMiddleware()) + ..addMiddleware(const SimpleLoggerMiddleware()); + + final client = MiddlewareClient(pipeline: pipeline); + + final response = + await client.get(Uri.parse('https://jigsaw.w3.org/HTTP/Basic/')); + + if (HttpStatus.from(response.statusCode).isSuccess()) { + print("🎉 You're in!"); + } else { + print("⭕️ Nope, you're not in."); + } +} + +void main(List args) { + testSimpleGet(); + testUriPrefix(); + testBasicAuth(); +}