From c3bec79176b2fcb0d02b67b9c8b1c73b1788a404 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Tue, 5 Aug 2025 15:28:13 +0530 Subject: [PATCH] Update README.md --- .../better_networking_example/README.md | 183 +----------------- 1 file changed, 2 insertions(+), 181 deletions(-) diff --git a/packages/better_networking/better_networking_example/README.md b/packages/better_networking/better_networking_example/README.md index 61a0e8e4..8f8e04c7 100644 --- a/packages/better_networking/better_networking_example/README.md +++ b/packages/better_networking/better_networking_example/README.md @@ -1,182 +1,3 @@ -# πŸ“‘ `better_networking` Package +# `better_networking` example -`better_networking` is a lightweight and extensible Dart package designed to simplify HTTP requests and streaming operations. It provides enhanced request modeling, consistent response handling, and built-in utility functions to streamline interactions with both REST and GraphQL APIs. Whether you're handling HTTP requests or streaming data (e.g., Server-Sent Events), this package makes the process more testable and developer-friendly. - ---- - -## πŸ”§ Features - -* **Unified request modeling** via `HttpRequestModel` -* **Consistent response handling** with `HttpResponseModel` -* **Streamed response support** (e.g., SSE) -* **Client management** with cancellation and lifecycle control -* **Built-in utilities** for parsing headers and content types -* **Support for both REST and GraphQL APIs** - ---- - -## πŸ“¦ Installation - -To install the `better_networking` package, add it to your `pubspec.yaml`: - -```yaml -dependencies: - better_networking: ^ -``` - -Then run the following command in your terminal to fetch the package: - -```bash -flutter pub get -``` - ---- - -## πŸš€ Quick Start - -Here’s a basic example to get you started with the package: - -```dart -final model = HttpRequestModel( - url: 'https://api.example.com/data', - method: HTTPVerb.post, - headers: [ - NameValueModel(name: 'Authorization', value: 'Bearer '), - ], - body: '{"key": "value"}', -); - -final (resp, duration, err) = await sendHttpRequest( - 'unique-request-id', - APIType.rest, - model, -); - -// To cancel the request -cancelHttpRequest('unique-request-id'); -``` - ---- - -## 🧩 API Overview - -### πŸ“₯ `HttpRequestModel` - -The `HttpRequestModel` defines the structure for outgoing HTTP requests, including headers, body content, parameters, and more. - -#### Constructor: - -```dart -const factory HttpRequestModel({ - @Default(HTTPVerb.get) HTTPVerb method, - @Default("") String url, - List? headers, - List? params, - List? isHeaderEnabledList, - List? isParamEnabledList, - @Default(ContentType.json) ContentType bodyContentType, - String? body, - String? query, - List? formData, -}); -``` - -#### Fields: - -* **`method`**: The HTTP verb to use (e.g., GET, POST, PUT). -* **`url`**: The target URL for the request. -* **`headers`**: A list of header key-value pairs. -* **`params`**: URL parameters as key-value pairs. -* **`isHeaderEnabledList`**: Toggles for enabling/disabling individual headers. -* **`isParamEnabledList`**: Toggles for enabling/disabling individual parameters. -* **`bodyContentType`**: The MIME type for the request body (e.g., `json`, `form`). -* **`body`**: The raw body of the request (usually a JSON or string). -* **`query`**: A custom query string to be appended to the URL. -* **`formData`**: Multipart form data (for file uploads, etc.). - ---- - -### πŸ” Request Sending Examples - -#### ➀ Standard REST HTTP Request - -This example demonstrates a simple GET request to fetch data from a REST API: - -```dart -const model = HttpRequestModel( - url: 'https://jsonplaceholder.typicode.com/posts/1', - method: HTTPVerb.get, - headers: [ - NameValueModel(name: 'User-Agent', value: 'Dart/3.0 (dart:io)'), - NameValueModel(name: 'Accept', value: 'application/json'), - ], -); - -final (resp, dur, err) = await sendHttpRequest( - 'get_test', - APIType.rest, - model, -); - -final output = jsonDecode(resp?.body ?? '{}'); -print(output); -``` - -#### ➀ Standard GraphQL HTTP Request - -This example demonstrates a GraphQL query: - -```dart -const model = HttpRequestModel( - url: 'https://countries.trevorblades.com/', - query: kGQLquery, // Your GraphQL query string -); - -final (resp, dur, err) = await sendHttpRequest( - 'gql_test', - APIType.graphql, - model, -); - -final output = jsonDecode(resp?.body ?? '{}'); -print(output); -``` - -#### ➀ Streamed HTTP Request (e.g., SSE) - -If you're dealing with server-sent events (SSE) or other streaming data, use the `streamHttpRequest` method: - -```dart -const model = HttpRequestModel( - url: 'https://sse.dev/test', - method: HTTPVerb.get, -); - -final stream = await streamHttpRequest('sse_test', APIType.rest, model); - -stream.listen((data) { - if (data != null) { - final HttpResponse? resp = data.$2; - final Duration? dur = data.$3; - final String? err = data.$4; - - // Handle the response here - } -}); -``` - ---- - -## 🀝 Contributing - -We welcome contributions to the `better_networking` package! If you'd like to contribute, please fork the repository and submit a pull request. For major changes or new features, it's a good idea to open an issue first to discuss your ideas. - ---- - -## πŸͺͺ License - -`better_networking` is licensed under the MIT License. - -MIT Β© API Dash - ---- +`better_networking` package example.