diff --git a/lib/app/Dio/dio.dart b/lib/app/Dio/dio.dart index 04645dbe..a8bc92cf 100644 --- a/lib/app/Dio/dio.dart +++ b/lib/app/Dio/dio.dart @@ -16,7 +16,7 @@ class GetDio { String baseURL = Global.apiBaseURL, bool applyBaseURL = true, bool loginRequired = true, - bool debugLog = false, + bool debugLog = true, bool buttonLock = true, bool showPopup = true, String? acceptHeader, @@ -57,17 +57,19 @@ class GetDio { // Check cache first and return cached data if supplied maxAge // has not elapsed. - final key = CacheOptions.defaultCacheKeyBuilder(options); - final cache = await Global.cacheStore.get(key); - if (cache != null && - cacheOptions != null && - !(cacheOptions.policy == CachePolicy.refresh) && - DateTime.now() - .isBefore(cache.responseDate.add(cacheOptions.maxAge))) { - if (buttonLock) ButtonController.setButtonValue(false); - // Resolve the request and pass cached data as response. - return handler - .resolve(cache.toResponse(options, fromNetwork: false)); + if (cacheEnabled) { + final key = CacheOptions.defaultCacheKeyBuilder(options); + final cache = await Global.cacheStore.get(key); + if (cache != null && + cacheOptions != null && + !(cacheOptions.policy == CachePolicy.refresh) && + DateTime.now() + .isBefore(cache.responseDate.add(cacheOptions.maxAge))) { + if (buttonLock) ButtonController.setButtonValue(false); + // Resolve the request and pass cached data as response. + return handler + .resolve(cache.toResponse(options, fromNetwork: false)); + } } // Proceed with the request. handler.next(options); @@ -92,7 +94,7 @@ class GetDio { // Todo: Add better exception handling based on response codes. if (error.response == null) { - throw Exception(error.message); + handler.next(error); } else if (error.response?.data.runtimeType is Map && error.response?.data.containsKey("message") && showPopup) { @@ -109,12 +111,12 @@ class GetDio { DioCacheInterceptor( options: cacheOptions ?? CacheOptions( - policy: CachePolicy.request, store: Global.cacheStore)), + policy: CachePolicy.refresh, store: Global.cacheStore)), ); // Log the request in the console for debugging if [debugLog] is true. if (debugLog) - dio.interceptors - .add(PrettyDioLogger(requestHeader: true, requestBody: true)); + dio.interceptors.add(PrettyDioLogger( + requestHeader: true, requestBody: true, responseHeader: true)); return dio; } } diff --git a/lib/blocs/authentication_bloc/authentication_bloc.dart b/lib/blocs/authentication_bloc/authentication_bloc.dart index 326a7a6f..dbaa24ca 100644 --- a/lib/blocs/authentication_bloc/authentication_bloc.dart +++ b/lib/blocs/authentication_bloc/authentication_bloc.dart @@ -27,13 +27,17 @@ class AuthenticationBloc yield AuthenticationUnauthenticated(); } else if (event is RequestDeviceCode) { // Get device code to initiate authentication. - Response response = await AuthService.getDeviceToken(); - // ['device_code'] should not be null. - if (response.data['device_code'] != null) { - yield AuthenticationInitialized( - DeviceCodeModel.fromJson(response.data)); - } else { - yield AuthenticationError('Something went wrong, please try again.'); + try { + Response response = await AuthService.getDeviceToken(); + // ['device_code'] should not be null. + if (response.data['device_code'] != null) { + yield AuthenticationInitialized( + DeviceCodeModel.fromJson(response.data)); + } else { + yield AuthenticationError('Something went wrong, please try again.'); + } + } catch (e) { + add(AuthError(e.toString())); } } else if (event is RequestAccessToken) { // Recurring function to request access token from Github on the supplied interval diff --git a/lib/services/authentication/auth_service.dart b/lib/services/authentication/auth_service.dart index ccac6322..5c82feea 100644 --- a/lib/services/authentication/auth_service.dart +++ b/lib/services/authentication/auth_service.dart @@ -42,6 +42,7 @@ class AuthService { var response = await GetDio.getDio( loggedIn: false, baseURL: 'https://github.com/', + debugLog: false, loginRequired: false) .post("${_url}device/code", data: formData); return response; @@ -67,19 +68,27 @@ class AuthService { 'device_code': deviceCode, 'grant_type': 'urn:ietf:params:oauth:grant-type:device_code', }); - Response response = await GetDio.getDio( - loggedIn: false, - loginRequired: false, - baseURL: 'https://github.com/', - buttonLock: false) - .post("${_url}oauth/access_token", data: formData); - if (response.data['access_token'] != null) { - storeAccessToken(AccessTokenModel.fromJson(response.data)); + try { + Response response = await GetDio.getDio( + loggedIn: false, + loginRequired: false, + cacheEnabled: false, + debugLog: false, + baseURL: 'https://github.com/', + buttonLock: false) + .post("${_url}oauth/access_token", data: formData); + if (response.data['access_token'] != null) { + storeAccessToken(AccessTokenModel.fromJson(response.data)); + return response; + } else if (response.data['error'] != null && + response.data['error'] != "authorization_pending" && + response.data['error'] != "slow_down") + throw Exception(response.data['error_description']); return response; - } else if (response.data['error'] == 'incorrect_device_code') { - throw Exception(response.data['error_description']); + } catch (e) { + print(e); + throw Exception(e); } - return response; } Future getDeviceCode() async { diff --git a/pubspec.yaml b/pubspec.yaml index 6bb1772d..f37effd2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: A Github mobile client publish_to: 'none' -version: 0.0.2-24032021.internal+2 +version: 0.0.3-25032021.internal+3 environment: sdk: '>=2.12.0 <3.0.0'