From a51834141f52e51cbbbe975eddea803580ba8ae9 Mon Sep 17 00:00:00 2001 From: Graham Smith Date: Sat, 16 Jan 2021 16:28:12 +0000 Subject: [PATCH 1/2] Implement authenticator --- chopper/lib/src/authenticator.dart | 9 +++++++++ chopper/lib/src/base.dart | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 chopper/lib/src/authenticator.dart diff --git a/chopper/lib/src/authenticator.dart b/chopper/lib/src/authenticator.dart new file mode 100644 index 00000000..26063650 --- /dev/null +++ b/chopper/lib/src/authenticator.dart @@ -0,0 +1,9 @@ +import 'dart:async'; + +import 'package:chopper/chopper.dart'; + +/// Returns a request that includes a credential to satisfy an authentication challenge in +/// [response]. Returns null if the challenge cannot be satisfied. +abstract class Authenticator { + FutureOr authenticate(Request request, Response response); +} \ No newline at end of file diff --git a/chopper/lib/src/base.dart b/chopper/lib/src/base.dart index 6747f52b..32cf48c6 100644 --- a/chopper/lib/src/base.dart +++ b/chopper/lib/src/base.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'package:chopper/src/authenticator.dart'; import 'package:meta/meta.dart'; import 'package:http/http.dart' as http; import 'constants.dart'; @@ -37,6 +38,10 @@ class ChopperClient { /// the request and response interceptors are called respectively. final Converter converter; + /// The [Authenticator] that handles provides reactive authentication for a + /// request. + final Authenticator authenticator; + /// The [ErrorConverter] that handles response transformation before the /// response interceptors are called, but only on error responses /// (statusCode < 200 || statusCode >= 300\). @@ -109,6 +114,7 @@ class ChopperClient { this.baseUrl = '', http.Client client, Iterable interceptors = const [], + this.authenticator, this.converter, this.errorConverter, Iterable services = const [], @@ -316,6 +322,15 @@ class ChopperClient { final response = await http.Response.fromStream(streamRes); dynamic res = Response(response, response.body); + if(authenticator != null) { + + var updatedRequest = authenticator.authenticate(request, res); + + if(updatedRequest != null) { + res = await send(updatedRequest); + } + } + if (_responseIsSuccessful(response.statusCode)) { res = await _handleSuccessResponse( res, From b23d6649815d6c990ee66e28ba7cd1d0150939a4 Mon Sep 17 00:00:00 2001 From: Ivan Terekhin Date: Sat, 6 Feb 2021 12:13:54 +0300 Subject: [PATCH 2/2] Changes for PR --- chopper/lib/src/authenticator.dart | 6 +++--- chopper/lib/src/base.dart | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/chopper/lib/src/authenticator.dart b/chopper/lib/src/authenticator.dart index 26063650..a5c02e6d 100644 --- a/chopper/lib/src/authenticator.dart +++ b/chopper/lib/src/authenticator.dart @@ -2,8 +2,8 @@ import 'dart:async'; import 'package:chopper/chopper.dart'; -/// Returns a request that includes a credential to satisfy an authentication challenge in -/// [response]. Returns null if the challenge cannot be satisfied. +/// This method should return a [Request] that includes credentials to satisfy an authentication challenge received in +/// [response]. It should return `null` if the challenge cannot be satisfied. abstract class Authenticator { FutureOr authenticate(Request request, Response response); -} \ No newline at end of file +} diff --git a/chopper/lib/src/base.dart b/chopper/lib/src/base.dart index 32cf48c6..8f448623 100644 --- a/chopper/lib/src/base.dart +++ b/chopper/lib/src/base.dart @@ -38,7 +38,7 @@ class ChopperClient { /// the request and response interceptors are called respectively. final Converter converter; - /// The [Authenticator] that handles provides reactive authentication for a + /// The [Authenticator] that can provide reactive authentication for a /// request. final Authenticator authenticator; @@ -322,11 +322,10 @@ class ChopperClient { final response = await http.Response.fromStream(streamRes); dynamic res = Response(response, response.body); - if(authenticator != null) { - + if (authenticator != null) { var updatedRequest = authenticator.authenticate(request, res); - if(updatedRequest != null) { + if (updatedRequest != null) { res = await send(updatedRequest); } }