Skip to content

Commit

Permalink
Merge pull request #197 from boostcampwm2023/iOS/feat#187
Browse files Browse the repository at this point in the history
feat: 업로드용 메서드 구현
  • Loading branch information
loinsir authored Dec 5, 2023
2 parents 6a4721a + 5d157f7 commit 982060f
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions iOS/Layover/Layover/Network/Provider/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 982060f

Please sign in to comment.