diff --git a/iOS/Layover/Layover/Network/Provider/Provider.swift b/iOS/Layover/Layover/Network/Provider/Provider.swift index d1585f2..0301e13 100644 --- a/iOS/Layover/Layover/Network/Provider/Provider.swift +++ b/iOS/Layover/Layover/Network/Provider/Provider.swift @@ -19,8 +19,8 @@ extension ProviderType { authenticationIfNeeded: Bool = true, retryCount: Int = 2) async throws -> R where E.Response == R { return try await request(with: endPoint, - authenticationIfNeeded: authenticationIfNeeded, - retryCount: retryCount) + authenticationIfNeeded: authenticationIfNeeded, + retryCount: retryCount) } } @@ -100,6 +100,34 @@ class Provider: ProviderType { return data } + // 이미지 업로드용 + func upload(data: Data, to url: String, method: HTTPMethod = .PUT) async throws -> Data { + guard let url = URL(string: url) else { throw NetworkError.components } + var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData) + request.httpMethod = method.rawValue + + let (data, response) = try await session.upload(for: request, from: data) + try self.checkStatusCode(of: response) + return data + } + + // 동영상 업로드용 + func backgroundUpload(fromFile: URL, + to url: String, + method: HTTPMethod = .PUT, + sessionTaskDelegate: URLSessionTaskDelegate? = nil, + delegateQueue: OperationQueue? = nil) async throws -> Data { + guard let url = URL(string: url) else { throw NetworkError.components } + var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData) + request.httpMethod = method.rawValue + let backgroundSession = URLSession(configuration: .background(withIdentifier: UUID().uuidString), + delegate: sessionTaskDelegate, + delegateQueue: delegateQueue) + let (data, response) = try await backgroundSession.upload(for: request, fromFile: fromFile) + try self.checkStatusCode(of: response) + return data + } + private func checkStatusCode(of response: URLResponse) throws { guard let response = response as? HTTPURLResponse else { throw NetworkError.unknown