Skip to content

Commit

Permalink
feat: #16/로그인 API 추가
Browse files Browse the repository at this point in the history
1. AccountApi.login() 추가
2. MockApi를 ApiImpl 상속을 통해 중복 코드 제거
  • Loading branch information
Younggun-Kim committed Oct 30, 2024
1 parent f14e8ce commit 52d37a2
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 15 deletions.
12 changes: 9 additions & 3 deletions lib/core/network/api_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ class ApiResponse<T> with _$ApiResponse<T> {
abstract class FailResponse with _$FailResponse {
const factory FailResponse({
required int status,
required String error,
required String message,
required String devMessage,
@Default('') String error,
@Default('') String message,
@Default('') String devMessage,
}) = _FailResponse;

factory FailResponse.fromJson(Map<String, dynamic> json) =>
_$FailResponseFromJson(json);

/// 서버 에러 시.
factory FailResponse.error() => const FailResponse(
status: 500,
message: '서버 에러',
);
}

extension ApiResponseExt on ApiResponse {
Expand Down
15 changes: 9 additions & 6 deletions lib/core/network/api_response.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -626,20 +626,23 @@ class __$$FailResponseImplCopyWithImpl<$Res>
class _$FailResponseImpl implements _FailResponse {
const _$FailResponseImpl(
{required this.status,
required this.error,
required this.message,
required this.devMessage});
this.error = '',
this.message = '',
this.devMessage = ''});

factory _$FailResponseImpl.fromJson(Map<String, dynamic> json) =>
_$$FailResponseImplFromJson(json);

@override
final int status;
@override
@JsonKey()
final String error;
@override
@JsonKey()
final String message;
@override
@JsonKey()
final String devMessage;

@override
Expand Down Expand Up @@ -683,9 +686,9 @@ class _$FailResponseImpl implements _FailResponse {
abstract class _FailResponse implements FailResponse {
const factory _FailResponse(
{required final int status,
required final String error,
required final String message,
required final String devMessage}) = _$FailResponseImpl;
final String error,
final String message,
final String devMessage}) = _$FailResponseImpl;

factory _FailResponse.fromJson(Map<String, dynamic> json) =
_$FailResponseImpl.fromJson;
Expand Down
6 changes: 3 additions & 3 deletions lib/core/network/api_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/feature/account/data/data_sources/dto/dto.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export 'login/response/login_response_dto.dart';
export 'login/login.dart';
24 changes: 23 additions & 1 deletion lib/feature/account/data/data_sources/mock/mock_api.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import 'dart:async';

import 'package:withu_app/core/core.dart';
import 'package:withu_app/feature/account/account.dart';

class AccountMockApi extends AccountApi {}
class AccountMockApi extends AccountApiImpl with MockAPI {
/// 로그인 API
@override
FutureOr<ApiResponse<LoginResponseDto>> login({
required LoginRequestDto requestData,
}) async {
/// Mock 응답 등록
dioAdapter.onPost(
loginPath,
(server) => server.reply(
200,
LoginResponseDtoMock.success().toJson(),
delay: const Duration(seconds: 1),
),
data: requestData.toJson(),
);

return await super.login(requestData: requestData);
}
}
12 changes: 12 additions & 0 deletions lib/feature/account/data/data_sources/remote/api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import 'dart:async';

import 'package:withu_app/core/core.dart';
import 'package:withu_app/feature/account/data/data_sources/dto/dto.dart';

abstract class AccountApi extends API {
/// API 주소
final path = "/api/account";

/// 로그인 주소
late final loginPath = '$path/login';

/// 로그인 API
FutureOr<ApiResponse<LoginResponseDto>> login({
required LoginRequestDto requestData,
});
}
21 changes: 20 additions & 1 deletion lib/feature/account/data/data_sources/remote/api_impl.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import 'dart:async';
import 'package:withu_app/core/network/api_response.dart';
import 'package:withu_app/feature/account/data/data_sources/dto/dto.dart';
import 'api.dart';

class AccountApiImpl extends AccountApi {}
class AccountApiImpl extends AccountApi {
/// 로그인 API
@override
FutureOr<ApiResponse<LoginResponseDto>> login({
required LoginRequestDto requestData,
}) async {
return dio
.post(
loginPath,
data: requestData.toJson(),
)
.then((response) => ApiResponse.success(
LoginResponseDto.fromJson(response.data),
))
.catchError((_) => const ApiResponse<LoginResponseDto>.error());
}
}

0 comments on commit 52d37a2

Please sign in to comment.