Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
Added bearerToken and added sessionToken param to RestrrBuilder#refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlessenich committed Mar 13, 2024
1 parent d0dc9dc commit 65bc3d7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion lib/src/requests/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ class CompiledRoute {
}

Future<Response> 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<String, dynamic> headers = {'Content-Type': contentType};
if (bearerToken != null) {
headers['Authorization'] = 'Bearer $bearerToken';
}
return dio.fetch(RequestOptions(
path: compiledRoute,
headers: headers,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/restrr_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class RestrrBuilder {
return _handleAuthProcess(authFunction: (apiImpl) => apiImpl._sessionService.create(username, password));
}

Future<RestResponse<Restrr>> refresh() async {
return _handleAuthProcess(authFunction: (apiImpl) => apiImpl._sessionService.refresh());
Future<RestResponse<Restrr>> refresh({required String sessionToken}) async {
return _handleAuthProcess(authFunction: (apiImpl) => apiImpl._sessionService.refresh(sessionToken));
}

Future<RestResponse<RestrrImpl>> _handleAuthProcess(
Expand Down
24 changes: 18 additions & 6 deletions lib/src/service/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class RequestHandler {
required T Function(dynamic) mapper,
required RouteOptions routeOptions,
bool isWeb = false,
String? bearerToken,
Map<int, RestrrError> errorMap = const {},
dynamic body,
String contentType = 'application/json'}) async {
try {
final Response<dynamic> response =
await route.submit(routeOptions: routeOptions, body: body, isWeb: isWeb, contentType: contentType);
final Response<dynamic> 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);
Expand All @@ -38,12 +39,13 @@ class RequestHandler {
{required CompiledRoute route,
required RouteOptions routeOptions,
bool isWeb = false,
String? bearerToken,
dynamic body,
Map<int, RestrrError> errorMap = const {},
String contentType = 'application/json'}) async {
try {
final Response<dynamic> response =
await route.submit(routeOptions: routeOptions, body: body, isWeb: isWeb, contentType: contentType);
final Response<dynamic> 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);
Expand All @@ -58,14 +60,15 @@ class RequestHandler {
{required CompiledRoute route,
required RouteOptions routeOptions,
bool isWeb = false,
String? bearerToken,
required T Function(dynamic) mapper,
Map<int, RestrrError> errorMap = const {},
Function(String)? fullRequest,
dynamic body,
String contentType = 'application/json'}) async {
try {
final Response<dynamic> response =
await route.submit(routeOptions: routeOptions, body: body, isWeb: isWeb, contentType: contentType);
final Response<dynamic> response = await route.submit(
routeOptions: routeOptions, body: body, isWeb: isWeb, bearerToken: bearerToken, contentType: contentType);
if (response.data is! List<dynamic>) {
throw StateError('Received response is not a list!');
}
Expand Down Expand Up @@ -118,13 +121,16 @@ abstract class ApiService {
Future<RestResponse<T>> request<T>(
{required CompiledRoute route,
required T Function(dynamic) mapper,
String? customBearerToken,
bool noAuth = false,
Map<int, RestrrError> errorMap = const {},
dynamic body,
String contentType = 'application/json'}) async {
return RequestHandler.request(
route: route,
routeOptions: api.routeOptions,
isWeb: api.options.isWeb,
bearerToken: customBearerToken ?? (noAuth ? null : api.session.token),
mapper: mapper,
errorMap: errorMap,
body: body,
Expand All @@ -134,13 +140,16 @@ abstract class ApiService {

Future<RestResponse<bool>> noResponseRequest<T>(
{required CompiledRoute route,
String? customBearerToken,
bool noAuth = false,
dynamic body,
Map<int, RestrrError> errorMap = const {},
String contentType = 'application/json'}) async {
return RequestHandler.noResponseRequest(
route: route,
routeOptions: api.routeOptions,
isWeb: api.options.isWeb,
bearerToken: customBearerToken ?? (noAuth ? null : api.session.token),
body: body,
errorMap: errorMap,
contentType: contentType)
Expand All @@ -150,6 +159,8 @@ abstract class ApiService {
Future<RestResponse<List<T>>> multiRequest<T>(
{required CompiledRoute route,
required T Function(dynamic) mapper,
String? customBearerToken,
bool noAuth = false,
Map<int, RestrrError> errorMap = const {},
Function(String)? fullRequest,
dynamic body,
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/service/session_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class SessionService extends ApiService {
});
}

Future<RestResponse<Session>> refresh() async {
Future<RestResponse<Session>> refresh(String sessionToken) async {
return request(
route: SessionRoutes.create.compile(),
customBearerToken: sessionToken,
mapper: (json) => api.entityBuilder.buildSession(json),
errorMap: {
404: RestrrError.invalidCredentials,
Expand Down

0 comments on commit 65bc3d7

Please sign in to comment.