sonic 0.0.3
sonic: ^0.0.3 copied to clipboard
A fluent interface for handling network requests.
Sonic
An HTTP Client with a Fluent interface and an improved type support.
Features #
- Fluent Interface
- Concurrency Control
- Response Type Caching
- Flutter friendly, with methods designed for ease of use with flutter.
Getting Started #
- Add the latest version of the package from pubspec.
- Import the package:
import 'package:sonic/sonic.dart';
- Initialize the client:
final sonic = Sonic( baseConfiguration: const BaseConfiguration( baseUrl: YOUR_BASE_URL, debugMode: true, ), ); sonic.initialize();
- Use the
create
method to create a new request builder.await sonic .create<YOUR_RESPONSE_TYPE>(url: YOUR_PATH_OR_FULL_URL) .withDecoder((dynamic json) => YOUR_RESPONSE_TYPE.fromMap(json)) .get() .execute();
- the response of this async operation will be
SonicResponse<T>
where T will beYOUR_RESPONSE_TYPE
Usage #
final sonic = Sonic(
baseConfiguration: const BaseConfiguration(
baseUrl: 'https://jsonplaceholder.typicode.com/',
debugMode: true,
),
);
sonic.initialize();
final response = await sonic
.create<TodoModel>(url: '/todos/1')
.get()
.withDecoder((dynamic json) => TodoModel.fromMap(json))
.onError((error) {
print(error.message);
print(error.stackTrace);
})
.onSuccess(
(data) {
print(data.data?.title);
},
)
.onLoading(() => log('Loading'))
.execute();
NOTES #
- You can also get raw response and ignore the type parsing by using
asRawRequest()
builder method and specifying the type parameter asdynamic
- You wont need to pass decoder using
withDecoder
after the first call to the same type as the decoder will be cached based on the type. - You can either pass relative url (path) or an absolute url to the
url
named parameter. - If
debugMode
is true,LogInterceptor
will be added to the internalDio
instance and it will be used to log everything related to a network call in the standard output. - You can have multiple instances of
Sonic
as individually, they do not share resources with each other. But it is recommended to have a global instance ofSonic
and use dependency injection to inject them into your business logic. - You can use
FormData
as body for uploading files etc. You can wrap the file withMultipartFile
instance. these are fromDio
library.
Contributions #
Contributions are always welcome!