From 7363e25a5761ab718e87faef2f59811db76159fb Mon Sep 17 00:00:00 2001 From: GeonH0 Date: Thu, 12 Sep 2024 23:06:11 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20NetworkService=EC=9D=98=20=EA=B3=B5?= =?UTF-8?q?=ED=86=B5=EC=9C=BC=EB=A1=9C=20=EC=82=AC=EC=9A=A9=EB=90=98?= =?UTF-8?q?=EB=8A=94=20URLSession=20=EC=9E=91=EC=97=85=EA=B3=BC=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EC=B2=98=EB=A6=AC=EB=A5=BC=20=EB=B3=84?= =?UTF-8?q?=EB=8F=84=EC=9D=98=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/Network/NetworkService.swift | 135 ++++++------------ 1 file changed, 43 insertions(+), 92 deletions(-) diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift index 47a0f82..3c233e8 100644 --- a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift @@ -16,59 +16,9 @@ protocol NetworkService { func postJsonRequest(url: URL, parameters:[String: Any], responseType: T.Type) -> Single } -class BaseNetworkService: NetworkService { - - func getRequest(url: URL, responseType: T.Type) -> Single { - var request = URLRequest(url: url) - request.httpMethod = "GET" - return Single.create { single in - let task = URLSession.shared.dataTask(with: request) { data, response, error in - if let error = error { - single(.failure(error)) - } else if let data = data { - do { - let decoder = JSONDecoder() - decoder.dateDecodingStrategy = .iso8601 - let responseObject = try decoder.decode(T.self, from: data) - single(.success(responseObject)) - } catch let decodingError { - single(.failure(decodingError)) - } - } - } - task.resume() - - return Disposables.create { - task.cancel() - } - } - } - - func postRequest( - url: URL, parameters: [String: Any], - imageDatas: [Data], - responseType: T.Type - ) -> Single { +final class BaseNetworkService: NetworkService { + private func createRequest(with request: URLRequest, responseType: T.Type) -> Single { return Single.create { single in - var formDataRequest = MultipartFormDataRequest(url: url) - - for (key, value) in parameters { - formDataRequest.addTextField(named: key, value: String(describing: value)) - } - - for (index, imageData) in imageDatas.enumerated() { - let filename = "image\(index).jpg" - formDataRequest.addDataField( - named: "recipeImgUrls", - data: imageData, - filename: filename, - mimeType: "image/jpeg" - ) - } - - formDataRequest.finalize() - let request = formDataRequest.asURLRequest() - let task = URLSession.shared.dataTask(with: request) { data, response, error in if let error = error { single(.failure(error)) @@ -99,52 +49,53 @@ class BaseNetworkService: NetworkService { } } } + + func getRequest(url: URL, responseType: T.Type) -> Single { + var request = URLRequest(url: url) + request.httpMethod = "GET" + return createRequest(with: request, responseType: responseType) + } + + func postRequest( + url: URL, parameters: [String: Any], + imageDatas: [Data], + responseType: T.Type + ) -> Single { + var formDataRequest = MultipartFormDataRequest(url: url) + + for (key, value) in parameters { + formDataRequest.addTextField(named: key, value: String(describing: value)) + } + + for (index, imageData) in imageDatas.enumerated() { + let filename = "image\(index).jpg" + formDataRequest.addDataField( + named: "recipeImgUrls", + data: imageData, + filename: filename, + mimeType: "image/jpeg" + ) + } + + formDataRequest.finalize() + let request = formDataRequest.asURLRequest() + return createRequest(with: request, responseType: responseType) + } func postJsonRequest( url: URL, parameters: [String: Any], responseType: T.Type ) -> Single { - return Single.create { single in - // JSON으로 변환 - guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { - single(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "JSON 인코딩 실패"]))) - return Disposables.create() - } - - var request = URLRequest(url: url) - request.httpMethod = "POST" - request.setValue("application/json", forHTTPHeaderField: "Content-Type") - request.httpBody = httpBody - - let task = URLSession.shared.dataTask(with: request) { data, response, error in - if let error = error { - single(.failure(error)) - } else if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 { - let statusCode = httpResponse.statusCode - let responseString = data.flatMap { String(data: $0, encoding: .utf8) } ?? "No response data" - let error = NSError( - domain: "", - code: statusCode, - userInfo: [NSLocalizedDescriptionKey: "HTTP \(statusCode): \(responseString)"] - ) - single(.failure(error)) - } else if let data = data { - do { - let decoder = JSONDecoder() - decoder.dateDecodingStrategy = .iso8601 - let responseObject = try decoder.decode(T.self, from: data) - single(.success(responseObject)) - } catch let decodingError { - single(.failure(decodingError)) - } - } - } - task.resume() - - return Disposables.create { - task.cancel() - } + guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { + return .error(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "JSON 인코딩 실패"])) } + + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.httpBody = httpBody + + return createRequest(with: request, responseType: responseType) } }