Skip to content

Commit

Permalink
[Set/#53] 네트워크 세팅 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe0929 committed Jan 12, 2024
1 parent 10f9fb8 commit 7b114d9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
1 change: 1 addition & 0 deletions HMH_iOS/HMH_iOS/Network/Base/BaseTargetType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ extension BaseTargetType {
var sampleData: Data {
return Data()
}

}
6 changes: 3 additions & 3 deletions HMH_iOS/HMH_iOS/Network/Foundation/APIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ struct APIConstants{
static let contentType = "Content-Type"
static let applicationJSON = "application/json"
static let auth = "Authorization"
static let accessToken = "" // TO-DO: AccessToken
static let appleAccessToken = ""
static let accessToken = "Bearer " // TO-DO: AccessToken
static let appleAccessToken = ""
static let OS = "OS"
static let iOS = "iOS"
}

extension APIConstants{
static let hasSocialTokenHeader = [contentType: applicationJSON,
auth : accessToken]
auth : appleAccessToken]
static let hasTokenHeader = [contentType: applicationJSON,
OS: iOS,
auth : accessToken]
Expand Down
1 change: 1 addition & 0 deletions HMH_iOS/HMH_iOS/Network/Foundation/NetworkHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ struct NetworkHelper {
default: return .networkFail
}
}

}
44 changes: 21 additions & 23 deletions HMH_iOS/HMH_iOS/Network/Foundation/NetworkRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@
import Foundation
import Moya

struct NetworkRequest {
let url: String
let httpMethod: Moya.Method
let body: Data? // optional
let headers: [String: String]? // optional

init(url: String,
httpMethod: Moya.Method,
requestBody: Data? = nil,
headers: [String: String]? = nil
) {
self.url = url
self.httpMethod = httpMethod
self.body = requestBody
self.headers = headers
struct ResponseData<Model: Codable> {
struct CommonResponse: Codable {
let result: Model
}

func createURLRequest(with url: URL) -> URLRequest {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = httpMethod.rawValue
urlRequest.httpBody = body
urlRequest.allHTTPHeaderFields = headers ?? [:]
return urlRequest

static func processResponse(_ result: Result<Response, MoyaError>) -> Result<Model?, Error> {
switch result {
case .success(let response):
do {
// status code가 200...299인 경우만 success로 체크 (아니면 예외발생)
_ = try response.filterSuccessfulStatusCodes()

let commonResponse = try JSONDecoder().decode(CommonResponse.self, from: response.data)
return .success(commonResponse.result)
} catch {
return .failure(error)
}

case .failure(let error):
return .failure(error)
}
}

}

13 changes: 10 additions & 3 deletions HMH_iOS/HMH_iOS/Network/Router/ChallengeRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import Foundation
import Moya

enum ChallengeRouter {
case createChallenge
case createChallenge(data: CreateChallengeRequestDTO)
}

extension ChallengeRouter: BaseTargetType {
private var headers: Parameters {
switch self {
case .createChallenge:
return APIConstants.hasTokenHeader
}
}

var path: String {
switch self {
case .createChallenge:
Expand All @@ -30,8 +37,8 @@ extension ChallengeRouter: BaseTargetType {

var task: Moya.Task {
switch self {
case .createChallenge:
return .requestPlain // 추후 변경 완료
case .createChallenge(let data):
return .requestJSONEncodable(data)
}
}
}

0 comments on commit 7b114d9

Please sign in to comment.