
A lightweight and flexible Dart HTTP client designed for making API calls with dynamic response parsing. It provides an easy way to handle HTTP requests and parse JSON responses into specific data models.
✨ Features
- Simple API: Includes basic HTTP methods like
GET
,POST
, andDELETE
. - Dynamic Parsing: Supports generic types for flexible JSON-to-object conversion.
- Error Handling: Encapsulates success and error states using the
Result<T>
class. - No Inheritance: Avoids unnecessary inheritance from
http.Client
for a minimal and focused API.
🚀 Installation
Add the following to your pubspec.yaml
:
dependencies:
tarsier_http_parser: ^1.0.0
Run the following command:
flutter pub get
📒 Usage
-
Define a Data Model
Create a data model that includes afromJson
factory constructor:
class User {
final String id;
final String name;
User({required this.id, required this.name});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'] as String,
name: json['name'] as String,
);
}
}
-
Initialize the HTTP Client
Create an instance of TarsierHttpClient with the fromJson function:
final client = TarsierHttpClient<User>(
fromJson: (json) => User.fromJson(json as Map<String, dynamic>),
);
-
Make HTTP Requests
GET Request
final result = await client.get(Uri.parse('https://api.example.com/user'));
result
.onSuccess((user) => print('User: ${user.name}'))
.onError((error) => print('Error: $error'));
POST Request
final postResult = await client.post(
Uri.parse('https://api.example.com/user'),
body: {'name': 'New User'},
);
postResult
.onSuccess((user) => print('Created User: ${user.name}'))
.onError((error) => print('Error: $error'));
DELETE Request
final deleteResult = await client.delete(Uri.parse('https://api.example.com/user/123'));
deleteResult
.onSuccess((data) => print('User deleted'))
.onError((error) => print('Error: $error'));
🎖️ License
This project is licensed under the MIT License. See the LICENSE file for details.
🐞 Contributing
Contributions are welcome! Please submit a pull request or file an issue for any bugs or feature requests on GitHub.
Libraries
- tarsier_http_parser
- Tarsier HTTP Parser