Skip to content

Commit

Permalink
[Refactor/#84] 네트워크 기본 세팅 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HELLOHIDI committed Aug 13, 2024
1 parent f962068 commit ff3d498
Show file tree
Hide file tree
Showing 12 changed files with 552 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

import Foundation

struct EmptyResponseDTO: Codable { }
public struct EmptyResponseDTO: Codable { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// AuthAPI.swift
// MyPageFeatureInterface
//
// Created by 류희재 on 8/13/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation

import Moya
import Domain

enum AuthAPI {
case revoke
case logout
}

extension AuthAPI: BaseAPI {
public static var apiType: APIType = .auth

var path: String {
switch self {
case .revoke:
return "user"
case .logout:
return "user/logout"
}
}

var method: Moya.Method {
switch self {
case .revoke:
return .delete
case .logout:
return .post
}
}

var task: Moya.Task {
switch self {
case .revoke:
return .requestPlain
case .logout:
return .requestPlain
}
}

var validationType: ValidationType {
switch self {
case .revoke:
return .successCodes
case .logout:
return .successCodes
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// APIError.swift
// MyPageFeatureInterface
//
// Created by 류희재 on 8/13/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation

public enum APIError: Error, Equatable {
case network(statusCode: Int, response: ErrorResponse)
case unknown
case tokenReissuanceFailed

init(error: Error, statusCode: Int? = 0, response: ErrorResponse) {
guard let statusCode else { self = .unknown ; return }

self = .network(statusCode: statusCode, response: response)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// BaseAPI.swift
// MyPageFeatureInterface
//
// Created by 류희재 on 8/13/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Alamofire
import Moya
import Foundation
import Core

public enum APIType {
case auth
case user
}

public protocol BaseAPI: TargetType {
static var apiType: APIType { get set }
}

extension BaseAPI {
public var baseURL: URL {
var base = "Config.Network.baseURL"
let operationBaseURL = "Config.Network.operationBaseURL"

switch Self.apiType {
case .auth:
base += "/auth"
case .user:
base += "/user"
}

guard let url = URL(string: base) else {
fatalError("baseURL could not be configured")
}

return url
}

public var headers: [String: String]? {
return HeaderType.jsonWithToken.value
}

public var validationType: ValidationType {
return .customCodes(Array(200..<600).filter { $0 != 401 })
}
}

public enum HeaderType {
case json
case jsonWithToken
case multipartWithToken

public var value: [String: String] {
switch self {
case .json:
return ["Content-Type": "application/json"]
case .jsonWithToken:
return ["Content-Type": "application/json",
"Authorization": UserManager.shared.accessToken]
case .multipartWithToken:
return ["Content-Type": "multipart/form-data",
"Authorization": UserManager.shared.accessToken]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// BaseEntity.swift
// MyPageFeatureInterface
//
// Created by 류희재 on 8/13/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation

public struct BaseEntity<T: Decodable>: Decodable {
public let success: Bool
public let message: String
public let data: T?

enum CodingKeys: String, CodingKey {
case success, message, data
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
success = try values.decode(Bool.self, forKey: .success)
message = try values.decode(String.self, forKey: .message)
data = try? values.decodeIfPresent(T.self, forKey: .data)
}
}
Loading

0 comments on commit ff3d498

Please sign in to comment.