Skip to content

Commit

Permalink
[Refactor] #239 - 네트워크 구조 변경
Browse files Browse the repository at this point in the history
[Refactor] #239 - 네트워크 구조 변경
  • Loading branch information
jeongdung-eo authored Mar 11, 2024
2 parents 4248387 + 92e06e4 commit 3c484f3
Show file tree
Hide file tree
Showing 27 changed files with 548 additions and 796 deletions.
106 changes: 27 additions & 79 deletions iOS-NOTTODO/iOS-NOTTODO.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions iOS-NOTTODO/iOS-NOTTODO/Global/Enum/DefaultKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@

import Foundation

enum LoginType {
case Kakao, Apple

var social: String {
switch self {
case .Kakao: return "KAKAO"
case .Apple: return "APPLE"
}
}
enum LoginType: String, Codable, CaseIterable {
case KAKAO, APPLE
}

struct DefaultKeys {
Expand Down
59 changes: 0 additions & 59 deletions iOS-NOTTODO/iOS-NOTTODO/Network/API/Achieve/AchieveAPI.swift

This file was deleted.

124 changes: 0 additions & 124 deletions iOS-NOTTODO/iOS-NOTTODO/Network/API/AddMission/AddMissionAPI.swift

This file was deleted.

32 changes: 21 additions & 11 deletions iOS-NOTTODO/iOS-NOTTODO/Network/API/Auth/AuthAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ import Foundation

import Moya

final class AuthAPI {
typealias AuthData = GeneralResponse<AuthResponseDTO>
typealias EmptyData = GeneralResponse<VoidType>

protocol AuthAPIType {
func postKakaoAuth(social: LoginType, request: AuthRequest, completion: @escaping (AuthData?) -> Void)
func postAppleAuth(social: LoginType, request: AuthRequest, completion: @escaping (AuthData?) -> Void)
func deleteAuth(completion: @escaping (EmptyData?) -> Void)
func withdrawalAuth(completion: @escaping (EmptyData?) -> Void)
}

final class AuthAPI: AuthAPIType {

static let shared: AuthAPI = AuthAPI()

Expand All @@ -19,13 +29,13 @@ final class AuthAPI {

// MARK: - POST

func postKakaoAuth(social: String, socialToken: String, fcmToken: String, completion: @escaping (GeneralResponse<AuthResponseDTO>?) -> Void) {
authProvider.request(.kakaoAuth(social: social, socialToken: socialToken, fcmToken: fcmToken)) { result in
func postKakaoAuth(social: LoginType, request: AuthRequest, completion: @escaping (AuthData?) -> Void) {
authProvider.request(.kakaoAuth(social: social, request: request)) { result in
switch result {
case .success(let response):
do {
guard let authData = try response.map(GeneralResponse<AuthResponseDTO>?.self) else { return }
completion(authData)
let response = try response.map(AuthData?.self)
completion(response)
} catch let err {
print(err.localizedDescription, 500)
}
Expand All @@ -36,13 +46,13 @@ final class AuthAPI {
}
}

func postAppleAuth(social: String, socialToken: String, fcmToken: String, name: String, completion: @escaping (GeneralResponse<AuthResponseDTO>?) -> Void) {
authProvider.request(.appleAuth(social: social, socialToken: socialToken, fcmToken: fcmToken, name: name)) { result in
func postAppleAuth(social: LoginType, request: AuthRequest, completion: @escaping (AuthData?) -> Void) {
authProvider.request(.appleAuth(social: social, request: request)) { result in
switch result {
case .success(let response):
do {
guard let authData = try response.map(GeneralResponse<AuthResponseDTO>?.self) else { return }
completion(authData)
let response = try response.map(AuthData?.self)
completion(response)
} catch let err {
print(err.localizedDescription, 500)
}
Expand All @@ -55,15 +65,15 @@ final class AuthAPI {

// MARK: - Delete

func deleteAuth(completion: @escaping (GeneralResponse<VoidType>?) -> Void) {
func deleteAuth(completion: @escaping (EmptyData?) -> Void) {
authProvider.request(.logout) { _ in
completion(nil)
}
}

// MARK: - Withdrawal

func withdrawalAuth(completion: @escaping (GeneralResponse<VoidType>?) -> Void) {
func withdrawalAuth(completion: @escaping (EmptyData?) -> Void) {
authProvider.request(.withdrawal) { _ in
completion(nil)
}
Expand Down
69 changes: 69 additions & 0 deletions iOS-NOTTODO/iOS-NOTTODO/Network/API/BaseAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// BaseAPI.swift
// iOS-NOTTODO
//
// Created by JEONGEUN KIM on 3/10/24.
//

import Foundation

import Moya

enum BaseDomain {
case auth
case mission
case recommend
}

extension BaseDomain {

var url: String {
switch self {
case .auth:
return "/auth"
case .mission:
return "/mission"
case .recommend:
return "/recommend"
}
}
}

protocol BaseService: TargetType {
var domain: BaseDomain { get }
var urlPath: String { get }
var headerType: HeaderType { get }
}

extension BaseService {
var baseURL: URL {
return URL(string: Bundle.main.baseURL)!
}

var path: String {
return domain.url + urlPath
}

var validationType: ValidationType {
return .successCodes
}

var headers: [String: String]? {
return headerType.value
}
}

public enum HeaderType {
case json
case jsonWithToken

public var value: [String: String] {
switch self {
case .json:
return ["Content-Type": "application/json"]
case .jsonWithToken:
return ["Content-Type": "application/json",
"Authorization": "\(KeychainUtil.getAccessToken())"]
}
}
}
Loading

0 comments on commit 3c484f3

Please sign in to comment.