Use this package as a standard library for customizing a network request
Features
- Customizing a network request
- Supporting handling refresh tokens if needed
- The standard for all Flutter services
Getting started
Depend on it
Run this command:
With Flutter:
flutter pub add tek_core_network_flutter
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):
dependencies:
tek_core_network_flutter: ^0.1.0
Usage
void main() {
test('Call api `base_url/path` ',
() async {
Future<void> refreshHandler(String token,
Function(bool isSuccess, String newToken) callback) async {
try {
// Handling checking current `token` if needed
// Logics to handle refresh token
callback(true, 'your new token');
} catch (e) {
callback(false, '');
}
}
TokenCredentials tokenCredentials = TokenCredentials.hasToken(
token: 'your token',
refreshHandler: (token, callback) async {
await refreshHandler(token, callback);
},
);
final RequestConfiguration config = RequestConfiguration(
tokenCredentials: tokenCredentials,
);
final clientConfig = APIClientConfiguration(
baseUrl: "base_url",
requestConfiguration: config,
isLoggingEnabled: true,
);
final NetworkRequestBuilder builder = NetworkRequestBuilder()
..setPath('/path')
..setMethod(HttpMethod.GET)
..setHasToken(true)
..setHeaders({"Content-Type": "application/json"})
..setQueryParameters({
"param_1": param1 value,
});
final api = ApiClient(configuration: clientConfig);
final response = await api.apiCall(builder.build());
response?.maybeWhen(
success: (data) {
NetworkLogger.logging(data.toString());
},
error: (message) {
NetworkLogger.logging("Message: $message");
},
orElse: () {});
});
}
Additional information
N/A