Skip to content

Instantly share code, notes, and snippets.

@rtlsilva
Created January 15, 2020 18:49
Show Gist options
  • Save rtlsilva/1833f9caaa8f03a24876fdd0eb7fef27 to your computer and use it in GitHub Desktop.
Save rtlsilva/1833f9caaa8f03a24876fdd0eb7fef27 to your computer and use it in GitHub Desktop.
import 'package:dio/dio.dart';
import 'package:my_api/api.dart';
/// Creates instance of [Dio] to be used in the remote layer of the app.
Dio createDio(BaseOptions baseConfiguration) {
var dio = Dio(baseConfiguration);
dio.interceptors.addAll([
// interceptor to retry failed requests
// interceptor to add bearer token to requests
// interceptor to refresh access tokens
// interceptor to log requests/responses
// etc.
]);
return dio;
}
/// Creates Dio Options for initializing a Dio client.
///
/// [baseUrl] Base url for the configuration
/// [connectionTimeout] Timeout when sending data
/// [connectionReadTimeout] Timeout when receiving data
BaseOptions createDioOptions(
String baseUrl, int connectionTimeout, int connectionReadTimeout) {
return BaseOptions(
baseUrl: baseUrl,
connectTimeout: connectionTimeout,
receiveTimeout: connectionReadTimeout,
);
}
/// Creates an instance of the backend API with default options.
MyApi createMyApi() {
const baseUrl =
'http://petstore.swagger.io/v2';
final options = createDioOptions(baseUrl, 10000, 10000);
final dio = createDio(options);
return MyApi(dio: dio);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment