Skip to content

Commit d7faeaf

Browse files
authored
Merge pull request #1 from BinaryBirds/feature/dev
Simplified upload and download task
2 parents 512f5e3 + dbee182 commit d7faeaf

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

Sources/SwiftHttp/HttpError.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public enum HttpError: LocalizedError {
2525
/// The response has an invalid header value
2626
case invalidHeaderValue(HttpResponse)
2727

28+
/// Upload request does not have data to send
29+
case missingUploadData
30+
2831
/// Unknown error
2932
case unknown(Error)
3033

@@ -41,6 +44,8 @@ public enum HttpError: LocalizedError {
4144
return "Missing header"
4245
case .invalidHeaderValue(_):
4346
return "Invalid header value"
47+
case .missingUploadData:
48+
return "Missing upload data"
4449
case .unknown(let error):
4550
return error.localizedDescription
4651
}

Sources/SwiftHttp/UrlSessionHttpClient.swift

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,13 @@ public struct UrlSessionHttpClient: HttpClient {
5353
///
5454
public func uploadTask(_ req: HttpRequest) async throws -> HttpResponse {
5555
let urlRequest = req.urlRequest
56+
guard let data = urlRequest.httpBody else {
57+
throw HttpError.missingUploadData
58+
}
5659
if log {
5760
print(urlRequest.curlString)
5861
}
59-
60-
let res: (Data, URLResponse) = try await withUnsafeThrowingContinuation { c in
61-
session.uploadTask(with: urlRequest, from: urlRequest.httpBody) { data, response, error in
62-
if let error = error {
63-
return c.resume(throwing: error)
64-
}
65-
guard let data = data, let urlResponse = response else {
66-
return c.resume(throwing: HttpError.invalidResponse)
67-
}
68-
c.resume(returning: (data, urlResponse))
69-
}.resume()
70-
}
62+
let res: (Data, URLResponse) = try await session.upload(for: urlRequest, from: data, delegate: nil)
7163
return try HttpRawResponse(res)
7264
}
7365

@@ -85,22 +77,10 @@ public struct UrlSessionHttpClient: HttpClient {
8577
if log {
8678
print(urlRequest.curlString)
8779
}
88-
89-
let res: (Data, URLResponse) = try await withUnsafeThrowingContinuation { c in
90-
session.downloadTask(with: urlRequest) { url, response, error in
91-
if let error = error {
92-
return c.resume(throwing: error)
93-
}
94-
guard let urlResponse = response, let url = url, let pathData = url.path.data(using: .utf8) else {
95-
return c.resume(throwing: HttpError.invalidResponse)
96-
}
97-
c.resume(returning: (pathData, urlResponse))
98-
}
99-
.resume()
80+
let res: (URL, URLResponse) = try await session.download(for: urlRequest, delegate: nil)
81+
guard let pathData = res.0.path.data(using: .utf8) else {
82+
throw HttpError.invalidResponse
10083
}
101-
return try HttpRawResponse(res)
84+
return try HttpRawResponse((pathData, res.1))
10285
}
10386
}
104-
105-
106-
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Viasz-Kádi Ferenc on 2022. 03. 27..
6+
//
7+
8+
import Foundation
9+
10+
import XCTest
11+
@testable import SwiftHttp
12+
13+
final class DownloadTests: XCTestCase {
14+
15+
let api = ImageApi()
16+
17+
func testDownload() async throws {
18+
let data = try await api.download()
19+
let filePath = String(data: data, encoding: .utf8)
20+
XCTAssertFalse(filePath == nil)
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Viasz-Kádi Ferenc on 2022. 03. 27..
6+
//
7+
8+
import Foundation
9+
import SwiftHttp
10+
11+
struct ImageApi {
12+
13+
let client: HttpClient = UrlSessionHttpClient(log: true)
14+
let apiBaseUrl = HttpUrl(host: "via.placeholder.com")
15+
16+
func download() async throws -> Data {
17+
let pipeline = HttpRawPipeline(url: apiBaseUrl.path("150"), method: .get)
18+
let res = try await pipeline.execute(client.downloadTask)
19+
return res.data
20+
}
21+
}

0 commit comments

Comments
 (0)