Fast and minimalistic Dart HTTP client library for
- creating cancellable HTTP requests
- managed authentication
- supported multithreaded requests and parsing.
Unwired ⚡ is designed to be easy to use while also being super flexible and customizable. It is built on top of http package.
- Cancellable HTTP requests
- Authentication Manager
- Data Parsing
- Multithreaded Requests
- Interceptors
In Unwired, requests are made using RequestHandler
object. However, before you can make any HTTP request, you should initialise the object. Initialising calls the synchronize()
function of the AuthManager
if you are using any, and the init()
function of HttpWorker
which processes your HTTP requests.
final RequestHandler<int> requestHandler = RequestHandler<int>(); // Debug Http Worker will be used since
// no Http Worker is passed in the constructor
await requestHandler.initialise(); // Once the future completes, you can start making requests
// using this requestHandler object
final Request request = requestHandler.get(
url: "https://api.nasa.gov/planetary/apod",
params: {"api_key": "YOUR_API_KEY"}
);
final Response response = await request.response;
Or you can use the request
method to make the request.
final Request request = requestHandler.request(
method = RequestMethod.get,
url = "https://api.nava.gov/planetary/apod",
params = {"api_key", "YOUR_API_KEY"}
);
POST request will be similar to the GET request. You can pass body
to the method as well.
final Request request = requestHandler.post(
url: "...",
body: ... // Body can be of any type
);
final Response response = await request.response;
Similar to the case with GET request, you can use request
to make the POST request. request
method can be used to call other HTTP requests such as DELETE, PUT, PATCH etc.
Cancelling is as simple as calling cancel
method on Cancellable
object. This will cause the Response
object to return immediately with isCancelled
set to true.
request.controller.cancel();
final Response response = await request.response;
print(response.isCancelled); // TRUE
To parse a data in Unwired requests, you should create a Parser
object that tells the library how to parse the data.
class APODParser extends Parser<APOD> {
@override
APOD parse(Object data) {
// Parse your data into your desired object (APOD in this case) here.
// Using generators like freezed for your data class will give you a nice function to parse
// the data from JSON. You can call that function here or throw if parsing fails. The error
// should be caught by the HTTP Worker and packed into the Response object.
return apod;
}
}
final Request<APOD> request = requestHandler.get(
url: "https://api.nasa.gov/planetary/apod",
params: {"api_key": "DEMO_KEY"},
parser: APODParser(),
);
final Reponse<APOD> response = await request.response;
final APOD apod = response.data;
Unwired supports managed authentication. You can create and use your own implementation of AuthManager
class to manage the authentication in application and requests.
class TokenAuthManager extends AuthManager<String> {
TokenAuthManager({required this.secureStorage}) {
secureStorage.read(key: _key).then((value) {
_token = value;
_completer.complete();
});
}
final FlutterSecureStorage secureStorage;
final Completer<void> _completer = Completer<void>();
Future synchronize() {
return _completer.future;
}
String _key = 'random_key';
set key(String k) {
_key = k;
}
String? _token;
@override
String? get authObject => _token;
Future authenticate(String token) async {
await secureStorage.write(key: _key, value: token);
_token = token;
}
Future unauthenticate() async {
await secureStorage.delete(key: _key);
_token = null;
}
@override
bool get isAuthenticated => _token != null;
String Function(String? token) _tokenParser =
(token) => token != null ? 'Bearer $token' : '';
/// This function is used to parse the [authObject] to include any keyword
/// such as 'Bearer ' along with the [String] token in the `Authorization`
/// header of a request depending on the type of token.
set tokenParser(String Function(String? token) parser) {
_tokenParser = parser;
}
@override
String get parsedAuthObject => _tokenParser(_token);
}
Pass your implementation of AuthManager
to the RequestHandler
.
final RequestHandler<int> requestHandler = RequestHandler<int>(authManager: TokenAuthManager());
Now you can access the functions like authenticate
and unauthenticate
to manage the auth state of your app.
final token = ... // Some request to get your token
requestHandler.authenticate(token);
To make authenticated requests, simply set the auth
argument of the request
or get
or post
methods to true. This will automatically include the parsedAuthObject
to the Authentication header of the request.
final Request request = requestHandler.get(url: "...", auth: true);
Yes. Unwired is stable and actively maintained.
Open source projects like Unwired thrive on contributions from the community. Any contribution you make is greatly appreciated.
Here are the ways you can be a part of the development of Unwired
- Report bugs and scenarios that are difficult to implement
- Report parts of the documentation that are unclear
- Fix typos/grammar mistakes
- Update the documentation / add examples
- Implement new features by making a pull-request
- Add test cases for existing features
This is where your logo could be! Sponsor Unwired