-
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 #43 from markgravity/chore/15-add-api-service
[15] [Backend] Add API service
- Loading branch information
Showing
6 changed files
with
139 additions
and
0 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,31 @@ | ||
part of 'api_service.dart'; | ||
|
||
class ApiException implements Exception { | ||
const ApiException({ | ||
required this.source, | ||
required this.message, | ||
required this.code, | ||
}); | ||
|
||
static ApiException? fromHttpException(HttpException exception) { | ||
if (exception.type != HttpExceptionType.response && | ||
exception.response?.data is! Map<String, dynamic>) { | ||
return null; | ||
} | ||
|
||
final json = exception.response!.data as Map<String, dynamic>; | ||
final source = json["errors"][0]["source"] as String; | ||
final message = json["errors"][0]["detail"] as String; | ||
final code = json["errors"][0]["code"] as String; | ||
|
||
return ApiException( | ||
source: source, | ||
message: message, | ||
code: code, | ||
); | ||
} | ||
|
||
final String source; | ||
final String message; | ||
final String code; | ||
} |
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,5 @@ | ||
part of 'api_service.dart'; | ||
|
||
abstract class ApiParams with Mappable { | ||
// | ||
} |
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,92 @@ | ||
import 'package:survey/gen/configs.gen.dart'; | ||
import 'package:survey/services/http/http_service.dart'; | ||
import 'package:object_mapper/object_mapper.dart'; | ||
import 'package:survey/services/locator/locator_service.dart'; | ||
|
||
part 'api_exception.dart'; | ||
|
||
part 'api_params.dart'; | ||
|
||
part 'api_service_register.dart'; | ||
|
||
abstract class ApiService { | ||
Future<T> call<T extends Mappable>({ | ||
required HttpMethod method, | ||
String? baseUrl, | ||
required String endPoint, | ||
ApiParams? params, | ||
bool requiresAuthentication = true, | ||
String? token, | ||
String? tokenType, | ||
}); | ||
|
||
void configureGlobalBaseUrl(String? baseUrl); | ||
|
||
void configureGlobalToken(String? token, String? tokenType); | ||
} | ||
|
||
class ApiServiceImpl implements ApiService { | ||
static String? _baseUrl; | ||
static String? _token; | ||
static String _tokenType = "Bearer"; | ||
final HttpService _httpService = locator.get(); | ||
|
||
@override | ||
Future<T> call<T extends Mappable>({ | ||
required HttpMethod method, | ||
String? baseUrl, | ||
required String endPoint, | ||
ApiParams? params, | ||
bool requiresAuthentication = true, | ||
String? token, | ||
String? tokenType, | ||
}) async { | ||
final String? finalBaseUrl = baseUrl ?? _baseUrl; | ||
assert(finalBaseUrl != null); | ||
|
||
final Map<String, dynamic> headers = {}; | ||
|
||
// Append token | ||
final String? finalToken = token ?? _token; | ||
if (requiresAuthentication && finalToken != null) { | ||
final finalTokenType = tokenType ?? _tokenType; | ||
headers["authorization"] = "$finalTokenType $finalToken"; | ||
} | ||
|
||
final url = finalBaseUrl! + endPoint; | ||
try { | ||
final response = await _httpService.request( | ||
method: method, | ||
data: params?.toJson(), | ||
url: url, | ||
headers: headers, | ||
) as Map<String, dynamic>; | ||
return Mapper.fromJson( | ||
response["data"]["attributes"] as Map<String, dynamic>) | ||
.toObject<T>(); | ||
} on HttpException catch (e) { | ||
throw ApiException.fromHttpException(e) ?? e; | ||
} | ||
} | ||
|
||
/// Configures a default base url when [call] with `baseUrl` is null. | ||
/// | ||
/// This value is stored in a static variable [_baseUrl] so it will be available in any | ||
/// instance of [ApiServiceImpl], after set | ||
@override | ||
void configureGlobalBaseUrl(String? baseUrl) { | ||
_baseUrl = baseUrl; | ||
} | ||
|
||
/// Configures a default token & tokenType when [call] with token or tokenType is null. | ||
/// | ||
/// This value is stored in a static variable [_token], [_tokenType] so it will be available in any | ||
/// instance of [ApiServiceImpl], after set | ||
@override | ||
void configureGlobalToken(String? token, String? tokenType) { | ||
_token = token; | ||
if (tokenType != null) { | ||
_tokenType = tokenType; | ||
} | ||
} | ||
} |
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,9 @@ | ||
part of 'api_service.dart'; | ||
|
||
class ApiServiceRegister { | ||
final ApiService _apiService = locator.get(); | ||
ApiServiceRegister() { | ||
Mappable.factories = Configs.factories; | ||
_apiService.configureGlobalBaseUrl(Configs.app.api.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
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