-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add AuthOptions instead of method parameters.
- add interceptors for authentication to handle different authentication uses. - bump to BloC 6.0.0 - add helper classes for pagination and error responses.
- Loading branch information
1 parent
e67b635
commit 0d47c65
Showing
10 changed files
with
279 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,31 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:miswag_auth/misc/client/interceptors/authentication_interceptor.dart'; | ||
import 'package:miswag_auth/misc/client/interceptors/debug_interceptor.dart'; | ||
import 'package:miswag_auth/models/auth_options.dart'; | ||
|
||
class ApiClient { | ||
final String baseUrl; | ||
final String tokenKey; | ||
|
||
final AuthOptions authOptions; | ||
final bool debug; | ||
Map<String, Object> authentication; | ||
|
||
Dio client; | ||
Dio authClient; | ||
|
||
ApiClient({ | ||
@required this.baseUrl, | ||
this.tokenKey = "token", | ||
}) : assert(baseUrl != null) { | ||
ApiClient(this.authOptions, {this.debug = false}) | ||
: assert(authOptions != null) { | ||
final BaseOptions options = BaseOptions( | ||
baseUrl: baseUrl, | ||
connectTimeout: 5000, | ||
receiveTimeout: 3000, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json" | ||
} | ||
); | ||
baseUrl: authOptions.baseUrl, | ||
connectTimeout: 5000, | ||
receiveTimeout: 3000, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json" | ||
}); | ||
authClient = Dio(options); | ||
client = Dio(options); | ||
client.interceptors.add(InterceptorsWrapper( | ||
onRequest: (RequestOptions options) async { | ||
if (authentication != null) { | ||
options.queryParameters.addAll({ | ||
tokenKey: authentication[tokenKey] | ||
}); | ||
} | ||
return options; | ||
})); | ||
client.interceptors.add(AuthenticationInterceptors(client: this)); | ||
if (debug) { | ||
client.interceptors.add(DebugInterceptor()); | ||
} | ||
} | ||
|
||
|
||
Future<Response> responseInterceptor(Response response) async { | ||
if (authentication != null) {} | ||
return response; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
lib/misc/client/interceptors/authentication_interceptor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:miswag_auth/misc/client/api_client.dart'; | ||
import 'package:miswag_auth/models/auth_options.dart'; | ||
|
||
class AuthenticationInterceptors extends InterceptorsWrapper { | ||
final ApiClient client; | ||
|
||
AuthenticationInterceptors({this.client}) : assert(client != null); | ||
|
||
@override | ||
Future onRequest(RequestOptions options) { | ||
if (this.client.authentication != null) { | ||
injectToken(options); | ||
} | ||
return super.onRequest(options); | ||
} | ||
|
||
@override | ||
Future onResponse(Response response) { | ||
return super.onResponse(response); | ||
} | ||
|
||
@override | ||
Future onError(DioError err) { | ||
return super.onError(err); | ||
} | ||
|
||
void injectToken(RequestOptions options) { | ||
if (this.client.authOptions.tokenType == TokenType.GET) { | ||
options.queryParameters.addAll({ | ||
this.client.authOptions.tokenKey: | ||
this.client.authentication[this.client.authOptions.tokenKey] | ||
}); | ||
} | ||
|
||
if (this.client.authOptions.tokenType == TokenType.Bearer) { | ||
options.headers.addAll({ | ||
this.client.authOptions.customAuthHeader: | ||
"${this.client.authOptions.tokenType} ${this.client.authentication[this.client.authOptions.tokenKey]}" | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.