Skip to content

Commit

Permalink
[Feat] #319 - header 및 utilPath 추가
Browse files Browse the repository at this point in the history
naverMap utilPath일 때는 baseURL도 다르게 처리함
  • Loading branch information
EunsuSeo01 committed Dec 30, 2024
1 parent f99224a commit 76feb7a
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions Hankkijogbo/Hankkijogbo/Network/Base/BaseTargetType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import UIKit

import Moya

Expand All @@ -25,6 +24,7 @@ enum HeaderType {
case loginHeader(accessToken: String)
case withdrawHeader(authorizationCode: String)
case formdataHeader(multipartData: [MultipartFormData])
case naverMapHeader(clientId: String, clientSecret: String)
}

/// 각 API에 따라 공통된 Path 값 (존재하지 않는 경우 빈 String 값)
Expand All @@ -38,6 +38,7 @@ enum UtilPath: String {
case university = "/v1/universities"
case location = "/v1/locations"
case universityStores = "/v1/university-stores"
case naverMap = "/v2/gc"
}

protocol BaseTargetType: TargetType {
Expand All @@ -51,26 +52,33 @@ protocol BaseTargetType: TargetType {

extension BaseTargetType {
var baseURL: URL {
guard let baseURL = URL(string: URLConstant.baseURL) else {
fatalError("ERROR - BASEURL")
switch utilPath {
case .naverMap:
guard let reverseGeocodingBaseURL = URL(string: URLConstant.reverseGeocodingBaseURL) else {
fatalError("ERROR - NAVER MAP Reverse Geocoding BASEURL")
}
return reverseGeocodingBaseURL
default:
guard let baseURL = URL(string: URLConstant.baseURL) else {
fatalError("ERROR - BASEURL")
}
return baseURL
}
return baseURL
}

var headers: [String: String]? {
var header: [String: String] = [:]

switch headerType {

case .loginHeader(let accessToken):
header["Content-Type"] = "application/json"
header["Authorization"] = "\(accessToken)"
return header

case .refreshTokenHeader:
header["Content-Type"] = "application/json"
let refreshToken = UserDefaults.standard.getRefreshToken()
header["Authorization"] = URLConstant.bearer + "\(refreshToken)"
return header

// 이후부터는 access token이 헤더에 필요합니다.
case .withdrawHeader(let authorizationCode):
Expand All @@ -80,6 +88,10 @@ extension BaseTargetType {
case .formdataHeader:
header["Content-Type"] = "multipart/form-data"

case .naverMapHeader(let clientId, let clientSecret):
header["x-ncp-apigw-api-key-id"] = clientId
header["x-ncp-apigw-api-key"] = clientSecret

default:
header["Content-Type"] = "application/json"
}
Expand Down Expand Up @@ -110,4 +122,3 @@ extension BaseTargetType {
}
}
}

0 comments on commit 76feb7a

Please sign in to comment.