Skip to content

Commit

Permalink
feat(app): add AppHeaderInterceptor
Browse files Browse the repository at this point in the history
to provide these via AppCubit
- Accept-Language: The current locale of the app
- User-Agent: The package name and version of the app
  • Loading branch information
adar2378 committed Aug 23, 2023
1 parent 7a9d0f1 commit 0351c25
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/djangoflow_app/lib/djangoflow_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export 'src/presentation/consumers/app_cubit_consumer.dart';
export 'src/presentation/listeners/app_cubit_listener.dart';
export 'src/utils/djangoflow_app_runner.dart';
export 'src/utils/djangoflow_app_snackbar.dart';
export 'src/data/interceptors/app_header_interceptor.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:dio/dio.dart';
import 'package:djangoflow_app/src/bloc/app_cubit.dart';
import 'package:flutter/foundation.dart';

/// AppHeaderInterceptor is a dio [Interceptor] which adds the following headers to every request via [AppCubit] :
/// - Accept-Language: The current locale of the app
/// - User-Agent: The package name and version of the app
class AppHeaderInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
if (AppCubit.instance.state.locale.isNotEmpty) {
options.headers['Accept-Language'] = AppCubit.instance.state.locale;
}

if (!kIsWeb) {
options.headers['User-Agent'] =
'${AppCubit.packageInfo?.packageName}/${AppCubit.packageInfo?.version}';
}
super.onRequest(options, handler);
}
}
2 changes: 1 addition & 1 deletion packages/djangoflow_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ environment:
flutter: ">=1.17.0"

dependencies:
dio: ^5.3.2
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter

flutter_bloc: ^8.1.3
freezed_annotation: ^2.2.0
json_annotation: ^4.8.1
Expand Down

0 comments on commit 0351c25

Please sign in to comment.