-
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.
Merge pull request #38 from CoderJava/feature/tambahkan-pengecekan-ap…
…akah-hostname-nya-valid-atau-tidak Feature - Tambahkan pengecekan apakah hostname-nya valid atau tidak ke endpoint ping
- Loading branch information
Showing
18 changed files
with
891 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
extensions: |
36 changes: 36 additions & 0 deletions
36
lib/feature/data/datasource/general/general_remote_data_source.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,36 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:dipantau_desktop_client/config/flavor_config.dart'; | ||
import 'package:dipantau_desktop_client/feature/data/model/general/general_response.dart'; | ||
|
||
abstract class GeneralRemoteDataSource { | ||
/// Panggil endpoint [host]/api/ping | ||
/// | ||
/// Throws [DioException] untuk semua error kode | ||
late String pathPing; | ||
|
||
Future<GeneralResponse> ping(String baseUrl); | ||
} | ||
|
||
class GeneralRemoteDataSourceImpl implements GeneralRemoteDataSource { | ||
final Dio dio; | ||
|
||
GeneralRemoteDataSourceImpl({ | ||
required this.dio, | ||
}); | ||
|
||
final baseUrl = FlavorConfig.instance.values.baseUrl; | ||
|
||
@override | ||
String pathPing = ''; | ||
|
||
@override | ||
Future<GeneralResponse> ping(String baseUrl) async { | ||
pathPing = '$baseUrl/api/ping'; | ||
final response = await dio.get(pathPing); | ||
if (response.statusCode.toString().startsWith('2')) { | ||
return GeneralResponse.fromJson(response.data); | ||
} else { | ||
throw DioException(requestOptions: RequestOptions(path: pathPing)); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
lib/feature/data/repository/general/general_repository_impl.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,56 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:dipantau_desktop_client/core/error/failure.dart'; | ||
import 'package:dipantau_desktop_client/core/network/network_info.dart'; | ||
import 'package:dipantau_desktop_client/feature/data/datasource/general/general_remote_data_source.dart'; | ||
import 'package:dipantau_desktop_client/feature/data/model/general/general_response.dart'; | ||
import 'package:dipantau_desktop_client/feature/domain/repository/general/general_repository.dart'; | ||
|
||
class GeneralRepositoryImpl implements GeneralRepository { | ||
final GeneralRemoteDataSource remoteDataSource; | ||
final NetworkInfo networkInfo; | ||
|
||
GeneralRepositoryImpl({ | ||
required this.remoteDataSource, | ||
required this.networkInfo, | ||
}); | ||
|
||
String getErrorMessageFromEndpoint(dynamic dynamicErrorMessage, String httpErrorMessage, int? statusCode) { | ||
if (dynamicErrorMessage is Map && dynamicErrorMessage.containsKey('message')) { | ||
return '$statusCode ${dynamicErrorMessage['message']}'; | ||
} else if (dynamicErrorMessage is String) { | ||
return httpErrorMessage; | ||
} else { | ||
return httpErrorMessage; | ||
} | ||
} | ||
|
||
@override | ||
Future<({Failure? failure, GeneralResponse? response})> ping(String baseUrl) async { | ||
Failure? failure; | ||
GeneralResponse? response; | ||
final isConnected = await networkInfo.isConnected; | ||
if (isConnected) { | ||
try { | ||
response = await remoteDataSource.ping(baseUrl); | ||
} on DioException catch (error) { | ||
final message = error.message ?? error.toString(); | ||
if (error.response == null) { | ||
failure = ServerFailure(message); | ||
} else { | ||
final errorMessage = getErrorMessageFromEndpoint( | ||
error.response?.data, | ||
message, | ||
error.response?.statusCode, | ||
); | ||
failure = ServerFailure(errorMessage); | ||
} | ||
} on TypeError catch (error) { | ||
final errorMessage = error.toString(); | ||
failure = ParsingFailure(errorMessage); | ||
} | ||
} else { | ||
failure = ConnectionFailure(); | ||
} | ||
return (failure: failure, response: response); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
lib/feature/domain/repository/general/general_repository.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,6 @@ | ||
import 'package:dipantau_desktop_client/core/error/failure.dart'; | ||
import 'package:dipantau_desktop_client/feature/data/model/general/general_response.dart'; | ||
|
||
abstract class GeneralRepository { | ||
Future<({Failure? failure, GeneralResponse? response})> ping(String baseUrl); | ||
} |
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,34 @@ | ||
import 'package:dipantau_desktop_client/core/error/failure.dart'; | ||
import 'package:dipantau_desktop_client/core/usecase/usecase.dart'; | ||
import 'package:dipantau_desktop_client/feature/data/model/general/general_response.dart'; | ||
import 'package:dipantau_desktop_client/feature/domain/repository/general/general_repository.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
|
||
class Ping implements UseCaseRecords<GeneralResponse, ParamsPing> { | ||
final GeneralRepository repository; | ||
|
||
Ping({required this.repository}); | ||
|
||
@override | ||
Future<({Failure? failure, GeneralResponse? response})> call(ParamsPing params) { | ||
return repository.ping(params.baseUrl); | ||
} | ||
} | ||
|
||
class ParamsPing extends Equatable { | ||
final String baseUrl; | ||
|
||
ParamsPing({ | ||
required this.baseUrl, | ||
}); | ||
|
||
@override | ||
List<Object?> get props => [ | ||
baseUrl, | ||
]; | ||
|
||
@override | ||
String toString() { | ||
return 'ParamsPing{baseUrl: $baseUrl}'; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
lib/feature/presentation/bloc/setup_credential/setup_credential_bloc.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 'dart:async'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:dipantau_desktop_client/core/util/helper.dart'; | ||
import 'package:dipantau_desktop_client/feature/domain/usecase/ping/ping.dart'; | ||
|
||
part 'setup_credential_event.dart'; | ||
|
||
part 'setup_credential_state.dart'; | ||
|
||
class SetupCredentialBloc extends Bloc<SetupCredentialEvent, SetupCredentialState> { | ||
final Helper helper; | ||
final Ping ping; | ||
|
||
SetupCredentialBloc({ | ||
required this.helper, | ||
required this.ping, | ||
}) : super(InitialSetupCredentialState()) { | ||
on<PingSetupCredentialEvent>(_onPingSetupCredentialEvent); | ||
} | ||
|
||
FutureOr<void> _onPingSetupCredentialEvent( | ||
PingSetupCredentialEvent event, | ||
Emitter<SetupCredentialState> emit, | ||
) async { | ||
final baseUrl = event.baseUrl; | ||
emit(LoadingSetupCredentialState()); | ||
final result = await ping( | ||
ParamsPing( | ||
baseUrl: baseUrl, | ||
), | ||
); | ||
final response = result.response; | ||
final failure = result.failure; | ||
if (response != null) { | ||
emit(SuccessPingSetupCredentialState(baseUrl: baseUrl)); | ||
return; | ||
} | ||
|
||
final errorMessage = helper.getErrorMessageFromFailure(failure); | ||
emit(FailureSetupCredentialState(errorMessage: errorMessage)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
lib/feature/presentation/bloc/setup_credential/setup_credential_event.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,16 @@ | ||
part of 'setup_credential_bloc.dart'; | ||
|
||
abstract class SetupCredentialEvent {} | ||
|
||
class PingSetupCredentialEvent extends SetupCredentialEvent { | ||
final String baseUrl; | ||
|
||
PingSetupCredentialEvent({ | ||
required this.baseUrl, | ||
}); | ||
|
||
@override | ||
String toString() { | ||
return 'PingSetupCredentialEvent{baseUrl: $baseUrl}'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
lib/feature/presentation/bloc/setup_credential/setup_credential_state.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,33 @@ | ||
part of 'setup_credential_bloc.dart'; | ||
|
||
abstract class SetupCredentialState {} | ||
|
||
class InitialSetupCredentialState extends SetupCredentialState {} | ||
|
||
class LoadingSetupCredentialState extends SetupCredentialState {} | ||
|
||
class FailureSetupCredentialState extends SetupCredentialState { | ||
final String errorMessage; | ||
|
||
FailureSetupCredentialState({ | ||
required this.errorMessage, | ||
}); | ||
|
||
@override | ||
String toString() { | ||
return 'FailureSetupCredentialState{errorMessage: $errorMessage}'; | ||
} | ||
} | ||
|
||
class SuccessPingSetupCredentialState extends SetupCredentialState { | ||
final String baseUrl; | ||
|
||
SuccessPingSetupCredentialState({ | ||
required this.baseUrl, | ||
}); | ||
|
||
@override | ||
String toString() { | ||
return 'SuccessPingSetupCredentialState{baseUrl: $baseUrl}'; | ||
} | ||
} |
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
Oops, something went wrong.