From 4d7568d4cd7c449208083f5361d8285838e4336a Mon Sep 17 00:00:00 2001 From: bsorinnn Date: Sat, 30 Apr 2022 23:56:44 +0900 Subject: [PATCH] =?UTF-8?q?[Add]=20AuthAPI=20=EC=97=B0=EA=B2=B0=20(#63)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hyangyu/Hyangyu/Sources/APIModels/Auth.swift | 2 - .../APIServices/AuthAPI/PasswordAPI.swift | 44 ++++++++++++++++--- .../APIServices/AuthAPI/PasswordService.swift | 10 +++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Hyangyu/Hyangyu/Sources/APIModels/Auth.swift b/Hyangyu/Hyangyu/Sources/APIModels/Auth.swift index 4012972..b0583f8 100644 --- a/Hyangyu/Hyangyu/Sources/APIModels/Auth.swift +++ b/Hyangyu/Hyangyu/Sources/APIModels/Auth.swift @@ -32,5 +32,3 @@ struct CodeData: Codable { struct EmailCheckData: Codable { let message: String } - - diff --git a/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordAPI.swift b/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordAPI.swift index 1b1b2b2..24a8fc4 100644 --- a/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordAPI.swift +++ b/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordAPI.swift @@ -37,7 +37,22 @@ public class PasswordAPI { case .success(let response): let statusCode = response.statusCode let data = response.data - let networkResult = self.judgeGetEmailCodeStatus(by: statusCode, data) + let networkResult = self.judgeStatus(by: statusCode, data) + completion(networkResult) + + case .failure(let err): + print(err) + } + } + } + + func checkCode(completion: @escaping (NetworkResult) -> Void, email: String, authNum: String) { + courseProvider.request(.checkCode(email: email, authNum: authNum)) { (result) in + switch result { + case .success(let response): + let statusCode = response.statusCode + let data = response.data + let networkResult = self.judgeStatus(by: statusCode, data) completion(networkResult) case .failure(let err): @@ -67,16 +82,16 @@ public class PasswordAPI { } } - private func judgeGetEmailCodeStatus(by statusCode: Int, _ data: Data) -> NetworkResult { - + private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult { let decoder = JSONDecoder() - guard let decodedData = try? decoder.decode(GenericResponse.self, from: data) else { + guard let decodedData = try? decoder.decode(GenericResponse.self, from: data) + else { return .pathErr } switch statusCode { case 200: - return .success(decodedData.data) + return .success(decodedData.message) case 400..<500: return .requestErr(decodedData.message) case 500: @@ -85,5 +100,24 @@ public class PasswordAPI { return .networkFail } } + +// private func judgeGetEmailCodeStatus(by statusCode: Int, _ data: Data) -> NetworkResult { +// +// let decoder = JSONDecoder() +// guard let decodedData = try? decoder.decode(GenericResponse.self, from: data) else { +// return .pathErr +// } +// +// switch statusCode { +// case 200: +// return .success(decodedData.data) +// case 400..<500: +// return .requestErr(decodedData.message) +// case 500: +// return .serverErr +// default: +// return .networkFail +// } +// } } diff --git a/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordService.swift b/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordService.swift index 01b155c..779a41b 100644 --- a/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordService.swift +++ b/Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordService.swift @@ -11,6 +11,7 @@ import Moya enum PasswordService { case putChangedPassword(email: String, password: String) case postEmailCode(email: String) + case checkCode(email: String, authNum: String) } @@ -25,6 +26,8 @@ extension PasswordService: TargetType { return Const.URL.passwordURL case .postEmailCode(let email): return Const.URL.findPasswordURL + "/\(email)" + case .checkCode(let email, let authNum): + return Const.URL.checkCodeURL } } @@ -34,6 +37,8 @@ extension PasswordService: TargetType { return .put case .postEmailCode(_): return .post + case .checkCode(_, _): + return .delete } } @@ -51,6 +56,11 @@ extension PasswordService: TargetType { ], encoding: JSONEncoding.default) case .postEmailCode(_): return .requestPlain + case .checkCode(let email, let authNum): + return .requestParameters(parameters: [ + "email": email, + "authNum": authNum + ], encoding: JSONEncoding.default) } }