From 65bc3d724fc7efa408b620458ee1e19cac92aec5 Mon Sep 17 00:00:00 2001 From: Jason Dean Lessenich Date: Wed, 13 Mar 2024 01:15:04 +0100 Subject: [PATCH] Added bearerToken and added sessionToken param to RestrrBuilder#refresh --- lib/src/requests/route.dart | 5 ++++- lib/src/restrr_base.dart | 4 ++-- lib/src/service/api_service.dart | 24 ++++++++++++++++++------ lib/src/service/session_service.dart | 3 ++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/src/requests/route.dart b/lib/src/requests/route.dart index 9578066..742cc7c 100644 --- a/lib/src/requests/route.dart +++ b/lib/src/requests/route.dart @@ -71,12 +71,15 @@ class CompiledRoute { } Future submit( - {required RouteOptions routeOptions, dynamic body, bool isWeb = false, String contentType = 'application/json'}) { + {required RouteOptions routeOptions, dynamic body, bool isWeb = false, String? bearerToken, String contentType = 'application/json'}) { if (baseRoute.isVersioned && routeOptions.apiVersion == -1) { throw StateError('Cannot submit a versioned route without specifying the API version!'); } final Dio dio = Dio(); Map headers = {'Content-Type': contentType}; + if (bearerToken != null) { + headers['Authorization'] = 'Bearer $bearerToken'; + } return dio.fetch(RequestOptions( path: compiledRoute, headers: headers, diff --git a/lib/src/restrr_base.dart b/lib/src/restrr_base.dart index 8c26ed4..da64596 100644 --- a/lib/src/restrr_base.dart +++ b/lib/src/restrr_base.dart @@ -36,8 +36,8 @@ class RestrrBuilder { return _handleAuthProcess(authFunction: (apiImpl) => apiImpl._sessionService.create(username, password)); } - Future> refresh() async { - return _handleAuthProcess(authFunction: (apiImpl) => apiImpl._sessionService.refresh()); + Future> refresh({required String sessionToken}) async { + return _handleAuthProcess(authFunction: (apiImpl) => apiImpl._sessionService.refresh(sessionToken)); } Future> _handleAuthProcess( diff --git a/lib/src/service/api_service.dart b/lib/src/service/api_service.dart index d5fca15..c4e3534 100644 --- a/lib/src/service/api_service.dart +++ b/lib/src/service/api_service.dart @@ -19,12 +19,13 @@ class RequestHandler { required T Function(dynamic) mapper, required RouteOptions routeOptions, bool isWeb = false, + String? bearerToken, Map errorMap = const {}, dynamic body, String contentType = 'application/json'}) async { try { - final Response response = - await route.submit(routeOptions: routeOptions, body: body, isWeb: isWeb, contentType: contentType); + final Response response = await route.submit( + routeOptions: routeOptions, body: body, isWeb: isWeb, bearerToken: bearerToken, contentType: contentType); return RestResponse(data: mapper.call(response.data), statusCode: response.statusCode); } on DioException catch (e) { return _handleDioException(e, isWeb, errorMap); @@ -38,12 +39,13 @@ class RequestHandler { {required CompiledRoute route, required RouteOptions routeOptions, bool isWeb = false, + String? bearerToken, dynamic body, Map errorMap = const {}, String contentType = 'application/json'}) async { try { - final Response response = - await route.submit(routeOptions: routeOptions, body: body, isWeb: isWeb, contentType: contentType); + final Response response = await route.submit( + routeOptions: routeOptions, body: body, isWeb: isWeb, bearerToken: bearerToken, contentType: contentType); return RestResponse(data: true, statusCode: response.statusCode); } on DioException catch (e) { return _handleDioException(e, isWeb, errorMap); @@ -58,14 +60,15 @@ class RequestHandler { {required CompiledRoute route, required RouteOptions routeOptions, bool isWeb = false, + String? bearerToken, required T Function(dynamic) mapper, Map errorMap = const {}, Function(String)? fullRequest, dynamic body, String contentType = 'application/json'}) async { try { - final Response response = - await route.submit(routeOptions: routeOptions, body: body, isWeb: isWeb, contentType: contentType); + final Response response = await route.submit( + routeOptions: routeOptions, body: body, isWeb: isWeb, bearerToken: bearerToken, contentType: contentType); if (response.data is! List) { throw StateError('Received response is not a list!'); } @@ -118,6 +121,8 @@ abstract class ApiService { Future> request( {required CompiledRoute route, required T Function(dynamic) mapper, + String? customBearerToken, + bool noAuth = false, Map errorMap = const {}, dynamic body, String contentType = 'application/json'}) async { @@ -125,6 +130,7 @@ abstract class ApiService { route: route, routeOptions: api.routeOptions, isWeb: api.options.isWeb, + bearerToken: customBearerToken ?? (noAuth ? null : api.session.token), mapper: mapper, errorMap: errorMap, body: body, @@ -134,6 +140,8 @@ abstract class ApiService { Future> noResponseRequest( {required CompiledRoute route, + String? customBearerToken, + bool noAuth = false, dynamic body, Map errorMap = const {}, String contentType = 'application/json'}) async { @@ -141,6 +149,7 @@ abstract class ApiService { route: route, routeOptions: api.routeOptions, isWeb: api.options.isWeb, + bearerToken: customBearerToken ?? (noAuth ? null : api.session.token), body: body, errorMap: errorMap, contentType: contentType) @@ -150,6 +159,8 @@ abstract class ApiService { Future>> multiRequest( {required CompiledRoute route, required T Function(dynamic) mapper, + String? customBearerToken, + bool noAuth = false, Map errorMap = const {}, Function(String)? fullRequest, dynamic body, @@ -158,6 +169,7 @@ abstract class ApiService { route: route, routeOptions: api.routeOptions, isWeb: api.options.isWeb, + bearerToken: customBearerToken ?? (noAuth ? null : api.session.token), mapper: mapper, errorMap: errorMap, fullRequest: fullRequest, diff --git a/lib/src/service/session_service.dart b/lib/src/service/session_service.dart index 7a6f1ff..b68e9b5 100644 --- a/lib/src/service/session_service.dart +++ b/lib/src/service/session_service.dart @@ -20,9 +20,10 @@ class SessionService extends ApiService { }); } - Future> refresh() async { + Future> refresh(String sessionToken) async { return request( route: SessionRoutes.create.compile(), + customBearerToken: sessionToken, mapper: (json) => api.entityBuilder.buildSession(json), errorMap: { 404: RestrrError.invalidCredentials,