From a4e6f152cb0e9979ef9b267d4c77241542e22715 Mon Sep 17 00:00:00 2001 From: Deven Joshi Date: Mon, 1 Nov 2021 18:40:07 +0530 Subject: [PATCH 01/11] fix tests --- .../test/src/client/channel_test.dart | 12 +++++------ .../test/src/client/client_test.dart | 20 +++++++++---------- .../api/attachment_file_uploader_test.dart | 2 +- ...ontal_scrollable_positioned_list_test.dart | 1 + ...ersed_scrollable_positioned_list_test.dart | 1 + .../scrollable_positioned_list_test.dart | 1 + ...rated_scrollable_positioned_list_test.dart | 1 + ...ontal_scrollable_positioned_list_test.dart | 1 + .../test/channels_bloc_test.dart | 2 +- 9 files changed, 23 insertions(+), 18 deletions(-) diff --git a/packages/stream_chat/test/src/client/channel_test.dart b/packages/stream_chat/test/src/client/channel_test.dart index c2ea57b5..c217431a 100644 --- a/packages/stream_chat/test/src/client/channel_test.dart +++ b/packages/stream_chat/test/src/client/channel_test.dart @@ -118,9 +118,9 @@ void main() { setUpAll(() { // Fallback values - registerFallbackValue(FakeMessage()); - registerFallbackValue>([]); - registerFallbackValue(FakeAttachmentFile()); + registerFallbackValue(FakeMessage()); + registerFallbackValue([]); + registerFallbackValue(FakeAttachmentFile()); // detached loggers when(() => client.detachedLogger(any())).thenAnswer((invocation) { @@ -176,9 +176,9 @@ void main() { setUpAll(() { // Fallback values - registerFallbackValue(FakeMessage()); - registerFallbackValue(FakeAttachmentFile()); - registerFallbackValue(FakeEvent()); + registerFallbackValue(FakeMessage()); + registerFallbackValue(FakeAttachmentFile()); + registerFallbackValue(FakeEvent()); // detached loggers when(() => client.detachedLogger(any())).thenAnswer((invocation) { diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index b59faa38..36c5919e 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -31,7 +31,7 @@ void main() { setUpAll(() { // fallback values - registerFallbackValue(FakeUser()); + registerFallbackValue(FakeUser()); }); setUp(() { @@ -230,7 +230,7 @@ void main() { setUpAll(() { // fallback values - registerFallbackValue(FakeUser()); + registerFallbackValue(FakeUser()); }); setUp(() { @@ -311,7 +311,7 @@ void main() { setUpAll(() { // fallback values - registerFallbackValue(FakeUser()); + registerFallbackValue(FakeUser()); }); setUp(() { @@ -399,7 +399,7 @@ void main() { setUpAll(() { // fallback values - registerFallbackValue(FakeUser()); + registerFallbackValue(FakeUser()); }); setUp(() { @@ -523,9 +523,9 @@ void main() { setUpAll(() { // fallback values - registerFallbackValue(FakeEvent()); - registerFallbackValue(const PaginationParams()); - registerFallbackValue(FakeChannelState()); + registerFallbackValue(FakeEvent()); + registerFallbackValue(const PaginationParams()); + registerFallbackValue(FakeChannelState()); }); setUp(() async { @@ -827,9 +827,9 @@ void main() { setUpAll(() { // fallback values - registerFallbackValue(FakeEvent()); - registerFallbackValue(FakeMessage()); - registerFallbackValue(const PaginationParams()); + registerFallbackValue(FakeEvent()); + registerFallbackValue(FakeMessage()); + registerFallbackValue(const PaginationParams()); }); setUp(() async { diff --git a/packages/stream_chat/test/src/core/api/attachment_file_uploader_test.dart b/packages/stream_chat/test/src/core/api/attachment_file_uploader_test.dart index 27f4ce6a..58ffdefb 100644 --- a/packages/stream_chat/test/src/core/api/attachment_file_uploader_test.dart +++ b/packages/stream_chat/test/src/core/api/attachment_file_uploader_test.dart @@ -15,7 +15,7 @@ void main() { setUp(() { fileUploader = StreamAttachmentFileUploader(client); - registerFallbackValue(FakeMultiPartFile()); + registerFallbackValue(FakeMultiPartFile()); }); Response successResponse(String path, {Object? data}) => Response( diff --git a/packages/stream_chat_flutter/test/scrollable_positioned_list/horizontal_scrollable_positioned_list_test.dart b/packages/stream_chat_flutter/test/scrollable_positioned_list/horizontal_scrollable_positioned_list_test.dart index b88cef14..d32f3969 100644 --- a/packages/stream_chat_flutter/test/scrollable_positioned_list/horizontal_scrollable_positioned_list_test.dart +++ b/packages/stream_chat_flutter/test/scrollable_positioned_list/horizontal_scrollable_positioned_list_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:ui'; import 'package:flutter/material.dart'; diff --git a/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart b/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart index 3bc335ce..0b825c78 100644 --- a/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart +++ b/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:ui'; +import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; diff --git a/packages/stream_chat_flutter/test/scrollable_positioned_list/scrollable_positioned_list_test.dart b/packages/stream_chat_flutter/test/scrollable_positioned_list/scrollable_positioned_list_test.dart index 7ec70ab0..5c4f22f1 100644 --- a/packages/stream_chat_flutter/test/scrollable_positioned_list/scrollable_positioned_list_test.dart +++ b/packages/stream_chat_flutter/test/scrollable_positioned_list/scrollable_positioned_list_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:math'; import 'dart:ui'; diff --git a/packages/stream_chat_flutter/test/scrollable_positioned_list/separated_scrollable_positioned_list_test.dart b/packages/stream_chat_flutter/test/scrollable_positioned_list/separated_scrollable_positioned_list_test.dart index 640eafa5..0cc091c9 100644 --- a/packages/stream_chat_flutter/test/scrollable_positioned_list/separated_scrollable_positioned_list_test.dart +++ b/packages/stream_chat_flutter/test/scrollable_positioned_list/separated_scrollable_positioned_list_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:ui'; import 'package:flutter/material.dart'; diff --git a/packages/stream_chat_flutter/test/scrollable_positioned_list/seperated_horizontal_scrollable_positioned_list_test.dart b/packages/stream_chat_flutter/test/scrollable_positioned_list/seperated_horizontal_scrollable_positioned_list_test.dart index 20858e75..55574003 100644 --- a/packages/stream_chat_flutter/test/scrollable_positioned_list/seperated_horizontal_scrollable_positioned_list_test.dart +++ b/packages/stream_chat_flutter/test/scrollable_positioned_list/seperated_horizontal_scrollable_positioned_list_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:ui'; import 'package:flutter/material.dart'; diff --git a/packages/stream_chat_flutter_core/test/channels_bloc_test.dart b/packages/stream_chat_flutter_core/test/channels_bloc_test.dart index a237a23c..e6365868 100644 --- a/packages/stream_chat_flutter_core/test/channels_bloc_test.dart +++ b/packages/stream_chat_flutter_core/test/channels_bloc_test.dart @@ -10,7 +10,7 @@ import 'mocks.dart'; void main() { setUpAll(() { - registerFallbackValue(const PaginationParams()); + registerFallbackValue(const PaginationParams()); }); List _generateChannels( From f1a46a33373fe241ea3501a396cb6354c2ba1220 Mon Sep 17 00:00:00 2001 From: Deven Joshi Date: Mon, 1 Nov 2021 18:46:41 +0530 Subject: [PATCH 02/11] analysis fixes --- .../reversed_scrollable_positioned_list_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart b/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart index 0b825c78..b0c487ea 100644 --- a/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart +++ b/packages/stream_chat_flutter/test/scrollable_positioned_list/reversed_scrollable_positioned_list_test.dart @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui'; import 'dart:async'; +import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; From 685de93e3124f1eac39181d570168eeeab6e94bf Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 2 Nov 2021 18:09:49 +0100 Subject: [PATCH 03/11] feat(llc,core,ui): send used package in the headers --- .../stream_chat/lib/src/client/client.dart | 9 ++--- .../interceptor/user_agent_interceptor.dart | 20 +++++++++++ .../lib/src/core/http/stream_http_client.dart | 2 ++ packages/stream_chat/lib/version.dart | 34 +++++++++++++++++++ .../lib/src/stream_chat.dart | 8 ++++- .../example/lib/main.dart | 4 +-- .../lib/src/stream_chat_core.dart | 6 ++++ 7 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index 6b6ae933..b9f2136d 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -25,7 +25,6 @@ import 'package:stream_chat/src/core/models/member.dart'; import 'package:stream_chat/src/core/models/message.dart'; import 'package:stream_chat/src/core/models/own_user.dart'; import 'package:stream_chat/src/core/models/user.dart'; -import 'package:stream_chat/src/core/platform_detector/platform_detector.dart'; import 'package:stream_chat/src/core/util/utils.dart'; import 'package:stream_chat/src/db/chat_persistence_client.dart'; import 'package:stream_chat/src/event_type.dart'; @@ -44,10 +43,6 @@ final _levelEmojiMapper = { Level.SEVERE: '🚨', }; -final _userAgent = 'stream-chat-dart-client-' - '${CurrentPlatform.name}-' - '${PACKAGE_VERSION.split('+')[0]}'; - /// The official Dart client for Stream Chat, /// a service for building chat applications. /// This library can be used on any Dart project and on both mobile and web apps @@ -86,7 +81,7 @@ class StreamChatClient { location: location, connectTimeout: connectTimeout, receiveTimeout: receiveTimeout, - headers: {'X-Stream-Client': _userAgent}, + headers: {'X-Stream-Client': defaultUserAgent}, ); _chatApi = chatApi ?? @@ -106,7 +101,7 @@ class StreamChatClient { tokenManager: _tokenManager, handler: handleEvent, logger: detachedLogger('🔌'), - queryParameters: {'X-Stream-Client': _userAgent}, + queryParameters: {'X-Stream-Client': defaultUserAgent}, ); _retryPolicy = retryPolicy ?? diff --git a/packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart b/packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart new file mode 100644 index 00000000..3f18dae2 --- /dev/null +++ b/packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart @@ -0,0 +1,20 @@ +import 'package:dio/dio.dart'; +import 'package:stream_chat/src/core/platform_detector/platform_detector.dart'; +import 'package:stream_chat/version.dart'; + +/// User agent interceptor that sets the user agent header +class UserAgentInterceptor extends Interceptor { + final _defaultUserAgent = 'stream-chat-dart-client-' + '${CurrentPlatform.name}-' + '${PACKAGE_VERSION.split('+')[0]}'; + + @override + Future onRequest( + RequestOptions options, + RequestInterceptorHandler handler, + ) async { + options.headers['X-Stream-Client'] = + '$_defaultUserAgent-${usedPackage.name}'; + return handler.next(options); + } +} diff --git a/packages/stream_chat/lib/src/core/http/stream_http_client.dart b/packages/stream_chat/lib/src/core/http/stream_http_client.dart index c19cdb8a..3008d40a 100644 --- a/packages/stream_chat/lib/src/core/http/stream_http_client.dart +++ b/packages/stream_chat/lib/src/core/http/stream_http_client.dart @@ -8,6 +8,7 @@ import 'package:stream_chat/src/core/http/connection_id_manager.dart'; import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/connection_id_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/logging_interceptor.dart'; +import 'package:stream_chat/src/core/http/interceptor/user_agent_interceptor.dart'; import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; import 'package:stream_chat/src/core/http/token_manager.dart'; import 'package:stream_chat/src/location.dart'; @@ -41,6 +42,7 @@ class StreamHttpClient { ..._options.headers, } ..interceptors.addAll([ + UserAgentInterceptor(), if (tokenManager != null) AuthInterceptor(this, tokenManager), if (connectionIdManager != null) ConnectionIdInterceptor(connectionIdManager), diff --git a/packages/stream_chat/lib/version.dart b/packages/stream_chat/lib/version.dart index c839a929..6eafab42 100644 --- a/packages/stream_chat/lib/version.dart +++ b/packages/stream_chat/lib/version.dart @@ -1,6 +1,40 @@ import 'package:stream_chat/src/client/client.dart'; +import 'package:stream_chat/src/core/platform_detector/platform_detector.dart'; /// Current package version /// Used in [StreamChatClient] to build the `x-stream-client` header // ignore: constant_identifier_names const PACKAGE_VERSION = '3.2.0'; + +/// Default user agent used for the 'x-stream-client' header +final defaultUserAgent = 'stream-chat-dart-client-' + '${CurrentPlatform.name}-' + '${PACKAGE_VERSION.split('+')[0]}'; + +/// Current used package +/// This is used to set the `x-stream-client` header using info about +/// the package in use +/// For example: llc/core/ui +Package usedPackage = Package.llc; + +/// Possible packages +enum Package { + /// The Low Level Client + llc, + + /// The Core package + core, + + /// The UI package + ui, +} + +/// +extension XPackage on Package { + /// + String get name => { + Package.llc: 'llc', + Package.core: 'core', + Package.ui: 'ui', + }[this]!; +} diff --git a/packages/stream_chat_flutter/lib/src/stream_chat.dart b/packages/stream_chat_flutter/lib/src/stream_chat.dart index ba8bcf64..a2a40d0d 100644 --- a/packages/stream_chat_flutter/lib/src/stream_chat.dart +++ b/packages/stream_chat_flutter/lib/src/stream_chat.dart @@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_portal/flutter_portal.dart'; import 'package:jiffy/jiffy.dart'; +import 'package:stream_chat/version.dart'; import 'package:stream_chat_flutter/src/stream_chat_theme.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart'; @@ -110,7 +111,12 @@ class StreamChatState extends State { onBackgroundEventReceived: widget.onBackgroundEventReceived, backgroundKeepAlive: widget.backgroundKeepAlive, connectivityStream: widget.connectivityStream, - child: widget.child ?? const Offstage(), + child: Builder( + builder: (context) { + usedPackage = Package.ui; + return widget.child ?? const Offstage(); + }, + ), ), ); }, diff --git a/packages/stream_chat_flutter_core/example/lib/main.dart b/packages/stream_chat_flutter_core/example/lib/main.dart index 31410e63..e4739161 100644 --- a/packages/stream_chat_flutter_core/example/lib/main.dart +++ b/packages/stream_chat_flutter_core/example/lib/main.dart @@ -4,7 +4,7 @@ import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart'; Future main() async { /// Create a new instance of [StreamChatClient] passing the apikey obtained /// from your project dashboard. - final client = StreamChatClient('b67pax5b2wdq'); + final client = StreamChatClient('b67pax5b2wdq', logLevel: Level.INFO); /// Set the current user. In a production scenario, this should be done using /// a backend to generate a user token using our server SDK. @@ -120,7 +120,7 @@ class HomeScreen extends StatelessWidget { itemBuilder: (BuildContext context, int index) { final _item = channels[index]; return ListTile( - title: Text(_item.name!), + title: Text(_item.name ?? ''), subtitle: StreamBuilder( stream: _item.state!.lastMessageStream, initialData: _item.state!.lastMessage, diff --git a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart index 3d07b3c7..ce781c71 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart @@ -4,6 +4,7 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:stream_chat/stream_chat.dart'; +import 'package:stream_chat/version.dart'; import 'package:stream_chat_flutter_core/src/typedef.dart'; /// Widget used to provide information about the chat to the widget tree. @@ -89,6 +90,11 @@ class StreamChatCore extends StatefulWidget { /// State class associated with [StreamChatCore]. class StreamChatCoreState extends State with WidgetsBindingObserver { + /// Constructor used for creating a new instance of [StreamChatCoreState]. + StreamChatCoreState() { + usedPackage = Package.core; + } + /// Initialized client used throughout the application. StreamChatClient get client => widget.client; From 1729cbcbb689b0e01edd23c5d151a345023352da Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 2 Nov 2021 18:17:33 +0100 Subject: [PATCH 04/11] chore(core): fix example --- packages/stream_chat_flutter_core/example/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/stream_chat_flutter_core/example/lib/main.dart b/packages/stream_chat_flutter_core/example/lib/main.dart index e4739161..407da315 100644 --- a/packages/stream_chat_flutter_core/example/lib/main.dart +++ b/packages/stream_chat_flutter_core/example/lib/main.dart @@ -4,7 +4,7 @@ import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart'; Future main() async { /// Create a new instance of [StreamChatClient] passing the apikey obtained /// from your project dashboard. - final client = StreamChatClient('b67pax5b2wdq', logLevel: Level.INFO); + final client = StreamChatClient('b67pax5b2wdq'); /// Set the current user. In a production scenario, this should be done using /// a backend to generate a user token using our server SDK. From 931f01a5541f5de6c8df1c137f3b23fdab87ed56 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 3 Nov 2021 12:09:07 +0100 Subject: [PATCH 05/11] fix(llc): fix tests --- .../core/http/stream_http_client_test.dart | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/stream_chat/test/src/core/http/stream_http_client_test.dart b/packages/stream_chat/test/src/core/http/stream_http_client_test.dart index 7860a0d6..b45f9e60 100644 --- a/packages/stream_chat/test/src/core/http/stream_http_client_test.dart +++ b/packages/stream_chat/test/src/core/http/stream_http_client_test.dart @@ -7,6 +7,7 @@ import 'package:stream_chat/src/core/http/connection_id_manager.dart'; import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/connection_id_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/logging_interceptor.dart'; +import 'package:stream_chat/src/core/http/interceptor/user_agent_interceptor.dart'; import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/token_manager.dart'; @@ -48,12 +49,21 @@ void main() { return dioError; } + test('UserAgentInterceptor should be added', () { + const apiKey = 'api-key'; + final client = StreamHttpClient(apiKey); + + expect( + client.httpClient.interceptors.whereType().length, + 1); + }); + test('AuthInterceptor should be added if tokenManager is provided', () { const apiKey = 'api-key'; final client = StreamHttpClient(apiKey, tokenManager: TokenManager()); - expect(client.httpClient.interceptors.length, 1); - expect(client.httpClient.interceptors.first, isA()); + expect( + client.httpClient.interceptors.whereType().length, 1); }); test( @@ -65,10 +75,11 @@ void main() { connectionIdManager: ConnectionIdManager(), ); - expect(client.httpClient.interceptors.length, 1); expect( - client.httpClient.interceptors.first, - isA(), + client.httpClient.interceptors + .whereType() + .length, + 1, ); }, ); @@ -80,10 +91,9 @@ void main() { logger: Logger('test-logger'), ); - expect(client.httpClient.interceptors.length, 1); expect( - client.httpClient.interceptors.first, - isA(), + client.httpClient.interceptors.whereType().length, + 1, ); }); From dbe9e38ab0d922e36d302852c4318022c45e3733 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 3 Nov 2021 12:17:41 +0100 Subject: [PATCH 06/11] use melos 1.0.0-dev.6 --- .github/workflows/dart_code_metrics.yaml | 2 +- .github/workflows/stream_flutter_workflow.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dart_code_metrics.yaml b/.github/workflows/dart_code_metrics.yaml index 1272babc..0ae85b88 100644 --- a/.github/workflows/dart_code_metrics.yaml +++ b/.github/workflows/dart_code_metrics.yaml @@ -33,7 +33,7 @@ jobs: flutter-version: ${{ env.flutter_version }} - name: "Install Tools" - run: flutter pub global activate melos 1.0.0-dev.3 + run: flutter pub global activate melos 1.0.0-dev.6 - name: "Bootstrap Workspace" run: melos bootstrap diff --git a/.github/workflows/stream_flutter_workflow.yml b/.github/workflows/stream_flutter_workflow.yml index 1eb5082a..65913a18 100644 --- a/.github/workflows/stream_flutter_workflow.yml +++ b/.github/workflows/stream_flutter_workflow.yml @@ -31,7 +31,7 @@ jobs: flutter-version: ${{ env.flutter_version }} - name: "Install Tools" run: | - flutter pub global activate melos 1.0.0-dev.3 + flutter pub global activate melos 1.0.0-dev.6 - name: "Bootstrap Workspace" run: melos bootstrap - name: "Dart Analyze" From 55e4c1ebd574122b70cd0bcb8d645a5a9f6bbb17 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 3 Nov 2021 12:53:36 +0100 Subject: [PATCH 07/11] use a static map for additional headers --- .../stream_chat/lib/src/client/client.dart | 7 ++++ .../additional_headers_interceptor.dart | 17 +++++++++ .../interceptor/user_agent_interceptor.dart | 20 ---------- .../lib/src/core/http/stream_http_client.dart | 4 +- packages/stream_chat/lib/version.dart | 12 ------ .../additional_headers_interceptor_test.dart | 37 +++++++++++++++++++ .../core/http/stream_http_client_test.dart | 6 ++- .../lib/src/stream_chat.dart | 5 ++- .../lib/src/stream_chat_core.dart | 4 +- 9 files changed, 74 insertions(+), 38 deletions(-) create mode 100644 packages/stream_chat/lib/src/core/http/interceptor/additional_headers_interceptor.dart delete mode 100644 packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart create mode 100644 packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index b9f2136d..b88c8d35 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -25,6 +25,7 @@ import 'package:stream_chat/src/core/models/member.dart'; import 'package:stream_chat/src/core/models/message.dart'; import 'package:stream_chat/src/core/models/own_user.dart'; import 'package:stream_chat/src/core/models/user.dart'; +import 'package:stream_chat/src/core/platform_detector/platform_detector.dart'; import 'package:stream_chat/src/core/util/utils.dart'; import 'package:stream_chat/src/db/chat_persistence_client.dart'; import 'package:stream_chat/src/event_type.dart'; @@ -126,6 +127,12 @@ class StreamChatClient { _originalChatPersistenceClient = value; } + static String defaultUserAgent = 'stream-chat-dart-client-' + '${CurrentPlatform.name}-' + '${PACKAGE_VERSION.split('+')[0]}'; + + static Map additionalHeaders = {}; + ChatPersistenceClient? _originalChatPersistenceClient; /// Chat persistence client diff --git a/packages/stream_chat/lib/src/core/http/interceptor/additional_headers_interceptor.dart b/packages/stream_chat/lib/src/core/http/interceptor/additional_headers_interceptor.dart new file mode 100644 index 00000000..466db58f --- /dev/null +++ b/packages/stream_chat/lib/src/core/http/interceptor/additional_headers_interceptor.dart @@ -0,0 +1,17 @@ +import 'package:dio/dio.dart'; +import 'package:stream_chat/stream_chat.dart'; + +/// Interceptor that sets additional headers for all requests. +class AdditionalHeadersInterceptor extends Interceptor { + @override + Future onRequest( + RequestOptions options, + RequestInterceptorHandler handler, + ) async { + options.headers = { + ...options.headers, + ...StreamChatClient.additionalHeaders, + }; + return handler.next(options); + } +} diff --git a/packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart b/packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart deleted file mode 100644 index 3f18dae2..00000000 --- a/packages/stream_chat/lib/src/core/http/interceptor/user_agent_interceptor.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'package:dio/dio.dart'; -import 'package:stream_chat/src/core/platform_detector/platform_detector.dart'; -import 'package:stream_chat/version.dart'; - -/// User agent interceptor that sets the user agent header -class UserAgentInterceptor extends Interceptor { - final _defaultUserAgent = 'stream-chat-dart-client-' - '${CurrentPlatform.name}-' - '${PACKAGE_VERSION.split('+')[0]}'; - - @override - Future onRequest( - RequestOptions options, - RequestInterceptorHandler handler, - ) async { - options.headers['X-Stream-Client'] = - '$_defaultUserAgent-${usedPackage.name}'; - return handler.next(options); - } -} diff --git a/packages/stream_chat/lib/src/core/http/stream_http_client.dart b/packages/stream_chat/lib/src/core/http/stream_http_client.dart index 3008d40a..ddd8b52d 100644 --- a/packages/stream_chat/lib/src/core/http/stream_http_client.dart +++ b/packages/stream_chat/lib/src/core/http/stream_http_client.dart @@ -8,7 +8,7 @@ import 'package:stream_chat/src/core/http/connection_id_manager.dart'; import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/connection_id_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/logging_interceptor.dart'; -import 'package:stream_chat/src/core/http/interceptor/user_agent_interceptor.dart'; +import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; import 'package:stream_chat/src/core/http/token_manager.dart'; import 'package:stream_chat/src/location.dart'; @@ -42,7 +42,7 @@ class StreamHttpClient { ..._options.headers, } ..interceptors.addAll([ - UserAgentInterceptor(), + AdditionalHeadersInterceptor(), if (tokenManager != null) AuthInterceptor(this, tokenManager), if (connectionIdManager != null) ConnectionIdInterceptor(connectionIdManager), diff --git a/packages/stream_chat/lib/version.dart b/packages/stream_chat/lib/version.dart index 6eafab42..f0ea7c92 100644 --- a/packages/stream_chat/lib/version.dart +++ b/packages/stream_chat/lib/version.dart @@ -1,22 +1,10 @@ import 'package:stream_chat/src/client/client.dart'; -import 'package:stream_chat/src/core/platform_detector/platform_detector.dart'; /// Current package version /// Used in [StreamChatClient] to build the `x-stream-client` header // ignore: constant_identifier_names const PACKAGE_VERSION = '3.2.0'; -/// Default user agent used for the 'x-stream-client' header -final defaultUserAgent = 'stream-chat-dart-client-' - '${CurrentPlatform.name}-' - '${PACKAGE_VERSION.split('+')[0]}'; - -/// Current used package -/// This is used to set the `x-stream-client` header using info about -/// the package in use -/// For example: llc/core/ui -Package usedPackage = Package.llc; - /// Possible packages enum Package { /// The Low Level Client diff --git a/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart b/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart new file mode 100644 index 00000000..b0f009e1 --- /dev/null +++ b/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart @@ -0,0 +1,37 @@ +import 'package:dio/dio.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; +import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; +import 'package:stream_chat/src/core/http/stream_http_client.dart'; +import 'package:stream_chat/src/core/http/token.dart'; +import 'package:stream_chat/stream_chat.dart'; +import 'package:test/test.dart'; + +import '../../../mocks.dart'; + +void main() { + late StreamHttpClient client; + late AdditionalHeadersInterceptor additionalHeadersInterceptor; + + setUp(() { + client = MockHttpClient(); + additionalHeadersInterceptor = AdditionalHeadersInterceptor(); + }); + + test( + '`onRequest` should add additional headers in the request', + () async { + final options = RequestOptions(path: 'test-path'); + final handler = RequestInterceptorHandler(); + + StreamChatClient.additionalHeaders = {'test-header': 'test-value'}; + additionalHeadersInterceptor.onRequest(options, handler); + + final updatedOptions = (await handler.future).data as RequestOptions; + final updateHeaders = updatedOptions.headers; + + expect(updateHeaders.containsKey('test-header'), isTrue); + expect(updateHeaders['test-header'], 'test-value'); + }, + ); +} diff --git a/packages/stream_chat/test/src/core/http/stream_http_client_test.dart b/packages/stream_chat/test/src/core/http/stream_http_client_test.dart index b45f9e60..01b54b63 100644 --- a/packages/stream_chat/test/src/core/http/stream_http_client_test.dart +++ b/packages/stream_chat/test/src/core/http/stream_http_client_test.dart @@ -7,7 +7,7 @@ import 'package:stream_chat/src/core/http/connection_id_manager.dart'; import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/connection_id_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/logging_interceptor.dart'; -import 'package:stream_chat/src/core/http/interceptor/user_agent_interceptor.dart'; +import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/token_manager.dart'; @@ -54,7 +54,9 @@ void main() { final client = StreamHttpClient(apiKey); expect( - client.httpClient.interceptors.whereType().length, + client.httpClient.interceptors + .whereType() + .length, 1); }); diff --git a/packages/stream_chat_flutter/lib/src/stream_chat.dart b/packages/stream_chat_flutter/lib/src/stream_chat.dart index a2a40d0d..db645943 100644 --- a/packages/stream_chat_flutter/lib/src/stream_chat.dart +++ b/packages/stream_chat_flutter/lib/src/stream_chat.dart @@ -113,7 +113,10 @@ class StreamChatState extends State { connectivityStream: widget.connectivityStream, child: Builder( builder: (context) { - usedPackage = Package.ui; + StreamChatClient.additionalHeaders = { + 'X-Stream-Client': + '${StreamChatClient.defaultUserAgent}-${Package.ui}', + }; return widget.child ?? const Offstage(); }, ), diff --git a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart index ce781c71..d43f3f51 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart @@ -92,7 +92,9 @@ class StreamChatCoreState extends State with WidgetsBindingObserver { /// Constructor used for creating a new instance of [StreamChatCoreState]. StreamChatCoreState() { - usedPackage = Package.core; + StreamChatClient.additionalHeaders = { + 'X-Stream-Client': '${StreamChatClient.defaultUserAgent}-${Package.ui}', + }; } /// Initialized client used throughout the application. From 63e5eb0a24f0494f182f152fde76f6be1311b3a5 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 3 Nov 2021 13:12:39 +0100 Subject: [PATCH 08/11] clean up --- packages/stream_chat/lib/version.dart | 22 ------------------- .../lib/src/stream_chat.dart | 2 +- .../lib/src/stream_chat_core.dart | 14 +++++------- 3 files changed, 7 insertions(+), 31 deletions(-) diff --git a/packages/stream_chat/lib/version.dart b/packages/stream_chat/lib/version.dart index f0ea7c92..c839a929 100644 --- a/packages/stream_chat/lib/version.dart +++ b/packages/stream_chat/lib/version.dart @@ -4,25 +4,3 @@ import 'package:stream_chat/src/client/client.dart'; /// Used in [StreamChatClient] to build the `x-stream-client` header // ignore: constant_identifier_names const PACKAGE_VERSION = '3.2.0'; - -/// Possible packages -enum Package { - /// The Low Level Client - llc, - - /// The Core package - core, - - /// The UI package - ui, -} - -/// -extension XPackage on Package { - /// - String get name => { - Package.llc: 'llc', - Package.core: 'core', - Package.ui: 'ui', - }[this]!; -} diff --git a/packages/stream_chat_flutter/lib/src/stream_chat.dart b/packages/stream_chat_flutter/lib/src/stream_chat.dart index db645943..76b0f27a 100644 --- a/packages/stream_chat_flutter/lib/src/stream_chat.dart +++ b/packages/stream_chat_flutter/lib/src/stream_chat.dart @@ -115,7 +115,7 @@ class StreamChatState extends State { builder: (context) { StreamChatClient.additionalHeaders = { 'X-Stream-Client': - '${StreamChatClient.defaultUserAgent}-${Package.ui}', + '${StreamChatClient.defaultUserAgent}-ui', }; return widget.child ?? const Offstage(); }, diff --git a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart index d43f3f51..260aa1cd 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart @@ -90,20 +90,18 @@ class StreamChatCore extends StatefulWidget { /// State class associated with [StreamChatCore]. class StreamChatCoreState extends State with WidgetsBindingObserver { - /// Constructor used for creating a new instance of [StreamChatCoreState]. - StreamChatCoreState() { - StreamChatClient.additionalHeaders = { - 'X-Stream-Client': '${StreamChatClient.defaultUserAgent}-${Package.ui}', - }; - } - /// Initialized client used throughout the application. StreamChatClient get client => widget.client; Timer? _disconnectTimer; @override - Widget build(BuildContext context) => widget.child; + Widget build(BuildContext context) { + StreamChatClient.additionalHeaders = { + 'X-Stream-Client': '${StreamChatClient.defaultUserAgent}-ui', + }; + return widget.child; + } // coverage:ignore-start From f6f9e017cfcd297aee838afd1c3ee810c3d3edb9 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 3 Nov 2021 13:18:54 +0100 Subject: [PATCH 09/11] fix core header --- packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart index 260aa1cd..43f341f0 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart @@ -98,7 +98,7 @@ class StreamChatCoreState extends State @override Widget build(BuildContext context) { StreamChatClient.additionalHeaders = { - 'X-Stream-Client': '${StreamChatClient.defaultUserAgent}-ui', + 'X-Stream-Client': '${StreamChatClient.defaultUserAgent}-core', }; return widget.child; } From b8f0ae14f485951e7bad935baa181d2c7574ecb7 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 3 Nov 2021 15:14:36 +0100 Subject: [PATCH 10/11] fix analysis --- packages/stream_chat/lib/src/client/client.dart | 2 ++ .../stream_chat/lib/src/core/http/stream_http_client.dart | 2 +- .../interceptor/additional_headers_interceptor_test.dart | 6 ------ .../stream_chat_flutter_core/lib/src/stream_chat_core.dart | 1 - 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index b88c8d35..c9bdebc1 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -127,10 +127,12 @@ class StreamChatClient { _originalChatPersistenceClient = value; } + /// Default user agent for all requests static String defaultUserAgent = 'stream-chat-dart-client-' '${CurrentPlatform.name}-' '${PACKAGE_VERSION.split('+')[0]}'; + /// Additionals headers for all requests static Map additionalHeaders = {}; ChatPersistenceClient? _originalChatPersistenceClient; diff --git a/packages/stream_chat/lib/src/core/http/stream_http_client.dart b/packages/stream_chat/lib/src/core/http/stream_http_client.dart index ddd8b52d..429eb52f 100644 --- a/packages/stream_chat/lib/src/core/http/stream_http_client.dart +++ b/packages/stream_chat/lib/src/core/http/stream_http_client.dart @@ -5,10 +5,10 @@ import 'package:logging/logging.dart'; import 'package:meta/meta.dart'; import 'package:stream_chat/src/core/error/error.dart'; import 'package:stream_chat/src/core/http/connection_id_manager.dart'; +import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/connection_id_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/logging_interceptor.dart'; -import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; import 'package:stream_chat/src/core/http/token_manager.dart'; import 'package:stream_chat/src/location.dart'; diff --git a/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart b/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart index b0f009e1..d273158a 100644 --- a/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart +++ b/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart @@ -1,20 +1,14 @@ import 'package:dio/dio.dart'; -import 'package:mocktail/mocktail.dart'; import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; -import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; -import 'package:stream_chat/src/core/http/stream_http_client.dart'; -import 'package:stream_chat/src/core/http/token.dart'; import 'package:stream_chat/stream_chat.dart'; import 'package:test/test.dart'; import '../../../mocks.dart'; void main() { - late StreamHttpClient client; late AdditionalHeadersInterceptor additionalHeadersInterceptor; setUp(() { - client = MockHttpClient(); additionalHeadersInterceptor = AdditionalHeadersInterceptor(); }); diff --git a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart index 43f341f0..3d00b739 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart @@ -4,7 +4,6 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:stream_chat/stream_chat.dart'; -import 'package:stream_chat/version.dart'; import 'package:stream_chat_flutter_core/src/typedef.dart'; /// Widget used to provide information about the chat to the widget tree. From bc948d666a399ada95a7b8b960f2c07b91db2bf6 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 3 Nov 2021 15:21:26 +0100 Subject: [PATCH 11/11] fix analysis --- .../http/interceptor/additional_headers_interceptor_test.dart | 2 -- .../stream_chat/test/src/core/http/stream_http_client_test.dart | 2 +- packages/stream_chat_flutter/lib/src/stream_chat.dart | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart b/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart index d273158a..88cf42ae 100644 --- a/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart +++ b/packages/stream_chat/test/src/core/http/interceptor/additional_headers_interceptor_test.dart @@ -3,8 +3,6 @@ import 'package:stream_chat/src/core/http/interceptor/additional_headers_interce import 'package:stream_chat/stream_chat.dart'; import 'package:test/test.dart'; -import '../../../mocks.dart'; - void main() { late AdditionalHeadersInterceptor additionalHeadersInterceptor; diff --git a/packages/stream_chat/test/src/core/http/stream_http_client_test.dart b/packages/stream_chat/test/src/core/http/stream_http_client_test.dart index 01b54b63..a42a5324 100644 --- a/packages/stream_chat/test/src/core/http/stream_http_client_test.dart +++ b/packages/stream_chat/test/src/core/http/stream_http_client_test.dart @@ -4,10 +4,10 @@ import 'package:mocktail/mocktail.dart'; import 'package:stream_chat/src/core/api/responses.dart'; import 'package:stream_chat/src/core/error/error.dart'; import 'package:stream_chat/src/core/http/connection_id_manager.dart'; +import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/connection_id_interceptor.dart'; import 'package:stream_chat/src/core/http/interceptor/logging_interceptor.dart'; -import 'package:stream_chat/src/core/http/interceptor/additional_headers_interceptor.dart'; import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/token_manager.dart'; diff --git a/packages/stream_chat_flutter/lib/src/stream_chat.dart b/packages/stream_chat_flutter/lib/src/stream_chat.dart index 76b0f27a..327a0927 100644 --- a/packages/stream_chat_flutter/lib/src/stream_chat.dart +++ b/packages/stream_chat_flutter/lib/src/stream_chat.dart @@ -4,7 +4,6 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_portal/flutter_portal.dart'; import 'package:jiffy/jiffy.dart'; -import 'package:stream_chat/version.dart'; import 'package:stream_chat_flutter/src/stream_chat_theme.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';