-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// BaseTargetType.swift | ||
// HMH_iOS | ||
// | ||
// Created by 지희의 MAC on 1/11/24. | ||
// | ||
|
||
import Foundation | ||
import Moya | ||
|
||
protocol BaseTargetType: TargetType {} | ||
|
||
extension BaseTargetType { | ||
var baseURL: URL { | ||
return URL(string: Config.baseURL)! | ||
} | ||
|
||
var headers: [String: String]? { | ||
return [ | ||
"Content-Type": "application/json", | ||
"Authorization": "Bearer " | ||
] | ||
} | ||
|
||
var sampleData: Data { | ||
return Data() | ||
} | ||
|
||
var validationType: ValidationType { | ||
return .successCodes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// MoyaLoggerPlugin.swift | ||
// HMH_iOS | ||
// | ||
// Created by 지희의 MAC on 1/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import Foundation | ||
import Moya | ||
|
||
final class MoyaLoggingPlugin: PluginType { | ||
// Request를 보낼 때 호출 | ||
func willSend(_ request: RequestType, target: TargetType) { | ||
guard let httpRequest = request.request else { | ||
print("--> 유효하지 않은 요청") | ||
return | ||
} | ||
let url = httpRequest.description | ||
let method = httpRequest.httpMethod ?? "unknown method" | ||
var log = "----------------------------------------------------\n\n[\(method)] \(url)\n\n----------------------------------------------------\n" | ||
log.append("API: \(target)\n") | ||
if let headers = httpRequest.allHTTPHeaderFields, !headers.isEmpty { | ||
log.append("header: \(headers)\n") | ||
} | ||
if let body = httpRequest.httpBody, let bodyString = String(bytes: body, encoding: String.Encoding.utf8) { | ||
log.append("\(bodyString)\n") | ||
} | ||
log.append("------------------- END \(method) --------------------------") | ||
print(log) | ||
} | ||
// Response가 왔을 때 | ||
func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) { | ||
switch result { | ||
case let .success(response): | ||
onSuceed(response, target: target, isFromError: false) | ||
case let .failure(error): | ||
onFail(error, target: target) | ||
} | ||
} | ||
|
||
func onSuceed(_ response: Response, target: TargetType, isFromError: Bool) { | ||
let request = response.request | ||
let url = request?.url?.absoluteString ?? "nil" | ||
let statusCode = response.statusCode | ||
var log = "------------------- 네트워크 통신 성공 -------------------" | ||
log.append("\n[\(statusCode)] \(url)\n----------------------------------------------------\n") | ||
log.append("API: \(target)\n") | ||
response.response?.allHeaderFields.forEach { | ||
log.append("\($0): \($1)\n") | ||
} | ||
if let reString = String(bytes: response.data, encoding: String.Encoding.utf8) { | ||
log.append("\(reString)\n") | ||
} | ||
log.append("------------------- END HTTP (\(response.data.count)-byte body) -------------------") | ||
print(log) | ||
} | ||
|
||
func onFail(_ error: MoyaError, target: TargetType) { | ||
if let response = error.response { | ||
onSuceed(response, target: target, isFromError: true) | ||
return | ||
} | ||
var log = "네트워크 오류" | ||
log.append("<-- \(error.errorCode) \(target)\n") | ||
log.append("\(error.failureReason ?? error.errorDescription ?? "unknown error")\n") | ||
log.append("<-- END HTTP") | ||
print(log) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// NetworkError.swift | ||
// HMH_iOS | ||
// | ||
// Created by 지희의 MAC on 1/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
enum NetworkError: Error { | ||
case httpError(ErrorResponse) | ||
case decodedError | ||
case networkFail | ||
case error(Error?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NetworkResult.swift | ||
// HMH_iOS | ||
// | ||
// Created by 지희의 MAC on 1/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
enum NetworkResult<T> { | ||
case success(T) // 서버 통신 성공했을 때, | ||
case requestErr(T) // 요청 에러 발생했을 때, | ||
case pathErr // 경로 에러 발생했을 때, | ||
case serverErr // 서버의 내부적 에러가 발생했을 때, | ||
case networkFail // 네트워크 연결 실패했을 때 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// StatusCode.swift | ||
// HMH_iOS | ||
// | ||
// Created by 지희의 MAC on 1/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
enum StatusCase: String, Decodable { | ||
case okay = "OK" | ||
case created = "CREATED" | ||
case accepted = "ACCEPTED" | ||
case noContent = "NO_CONTENT" | ||
case badRequest = "BAD_REQUEST" | ||
case unAuthorized = "UNAUTHORIZED" | ||
case forbidden = "FORBIDDEN" | ||
case notFound = "NOT_FOUND" | ||
case methodNotAllowed = "METHOD_NOT_ALLOWED" | ||
case notAcceptable = "NOT_ACCEPTABLE" | ||
case conflict = "CONFLICT" | ||
case unsupportedMediaType = "UNSUPPORTED_MEDIA_TYPE" | ||
case internalSever = "INTERNAL_SERVER" | ||
case badGateway = "BAD_GATEWAY" | ||
case serviceUnavailable = "SERVICE_UNAVAILABLE" | ||
case notDefined = "NOT_DEFINED_CLIENT_ERROR" | ||
|
||
// swiftlint: disable cyclomatic_complexity | ||
init(_ statusCode: Int) { | ||
switch statusCode { | ||
case 200: self = .okay | ||
case 201: self = .created | ||
case 202: self = .noContent | ||
case 204: self = .noContent | ||
case 400: self = .badRequest | ||
case 401: self = .unAuthorized | ||
case 404: self = .notFound | ||
case 405: self = .methodNotAllowed | ||
case 406: self = .notAcceptable | ||
case 409: self = .conflict | ||
case 415: self = .unsupportedMediaType | ||
case 500: self = .internalSever | ||
case 502: self = .badRequest | ||
case 503: self = .serviceUnavailable | ||
default: self = .notDefined | ||
} | ||
} | ||
} |