Skip to content

Commit 28f1fc0

Browse files
committed
refactor: update Storage module to use new execute method with proper serialization
- Replace HTTPRequest struct usage with direct execute method calls - Use serializingDecodable() for JSON responses instead of manual decoding - Use serializingData() for raw data responses - Update method signatures to use new execute parameters - Fix type conversions for headers and query parameters - Remove unused encoder variables and fix async warnings - Maintain backward compatibility while modernizing request handling
1 parent e710377 commit 28f1fc0

File tree

3 files changed

+140
-192
lines changed

3 files changed

+140
-192
lines changed

Sources/Storage/StorageApi.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import HTTPTypes
66
import FoundationNetworking
77
#endif
88

9+
struct NoopParameter: Encodable, Sendable {}
10+
911
public class StorageApi: @unchecked Sendable {
1012
public let configuration: StorageClientConfiguration
1113

@@ -43,14 +45,28 @@ public class StorageApi: @unchecked Sendable {
4345
self.session = configuration.session
4446
}
4547

48+
private let urlQueryEncoder: any ParameterEncoding = URLEncoding.queryString
49+
private var defaultEncoder: any ParameterEncoder {
50+
JSONParameterEncoder(encoder: configuration.encoder)
51+
}
52+
4653
@discardableResult
47-
func execute(_ request: Helpers.HTTPRequest) async throws -> Data {
48-
var request = request
49-
request.headers = HTTPFields(configuration.headers).merging(with: request.headers)
54+
func execute<RequestBody: Encodable & Sendable>(
55+
_ url: URL,
56+
method: HTTPMethod = .get,
57+
headers: HTTPHeaders = [:],
58+
query: Parameters? = nil,
59+
body: RequestBody? = NoopParameter(),
60+
encoder: (any ParameterEncoder)? = nil
61+
) throws -> DataRequest {
62+
var request = try URLRequest(url: url, method: method, headers: headers)
5063

51-
let urlRequest = request.urlRequest
64+
request = try urlQueryEncoder.encode(request, with: query)
65+
if RequestBody.self != NoopParameter.self {
66+
request = try (encoder ?? defaultEncoder).encode(body, into: request)
67+
}
5268

53-
return try await session.request(urlRequest)
69+
return session.request(request)
5470
.validate { request, response, data in
5571
guard 200..<300 ~= response.statusCode else {
5672
guard let data else {
@@ -65,8 +81,6 @@ public class StorageApi: @unchecked Sendable {
6581
}
6682
return .success(())
6783
}
68-
.serializingData()
69-
.value
7084
}
7185
}
7286

Sources/Storage/StorageBucketApi.swift

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,21 @@ import Foundation
88
public class StorageBucketApi: StorageApi, @unchecked Sendable {
99
/// Retrieves the details of all Storage buckets within an existing project.
1010
public func listBuckets() async throws -> [Bucket] {
11-
let data = try await execute(
12-
HTTPRequest(
13-
url: configuration.url.appendingPathComponent("bucket"),
14-
method: .get
15-
)
16-
)
17-
18-
return try configuration.decoder.decode([Bucket].self, from: data)
11+
try await execute(
12+
configuration.url.appendingPathComponent("bucket"),
13+
method: .get
14+
).serializingDecodable([Bucket].self, decoder: configuration.decoder).value
1915
}
2016

2117
/// Retrieves the details of an existing Storage bucket.
2218
/// - Parameters:
2319
/// - id: The unique identifier of the bucket you would like to retrieve.
2420
public func getBucket(_ id: String) async throws -> Bucket {
25-
let data = try await execute(
26-
HTTPRequest(
27-
url: configuration.url.appendingPathComponent("bucket/\(id)"),
28-
method: .get
29-
)
30-
)
31-
32-
return try configuration.decoder.decode(Bucket.self, from: data)
21+
try await execute(
22+
configuration.url.appendingPathComponent("bucket/\(id)"),
23+
method: .get
24+
).serializingDecodable(Bucket.self, decoder: configuration.decoder).value
25+
3326
}
3427

3528
struct BucketParameters: Encodable {
@@ -45,67 +38,55 @@ public class StorageBucketApi: StorageApi, @unchecked Sendable {
4538
/// - id: A unique identifier for the bucket you are creating.
4639
/// - options: Options for creating the bucket.
4740
public func createBucket(_ id: String, options: BucketOptions = .init()) async throws {
48-
try await execute(
49-
HTTPRequest(
50-
url: configuration.url.appendingPathComponent("bucket"),
51-
method: .post,
52-
body: configuration.encoder.encode(
53-
BucketParameters(
54-
id: id,
55-
name: id,
56-
public: options.public,
57-
fileSizeLimit: options.fileSizeLimit,
58-
allowedMimeTypes: options.allowedMimeTypes
59-
)
60-
)
41+
_ = try await execute(
42+
configuration.url.appendingPathComponent("bucket"),
43+
method: .post,
44+
body: BucketParameters(
45+
id: id,
46+
name: id,
47+
public: options.public,
48+
fileSizeLimit: options.fileSizeLimit,
49+
allowedMimeTypes: options.allowedMimeTypes
6150
)
62-
)
51+
).serializingData().value
6352
}
6453

6554
/// Updates a Storage bucket.
6655
/// - Parameters:
6756
/// - id: A unique identifier for the bucket you are updating.
6857
/// - options: Options for updating the bucket.
6958
public func updateBucket(_ id: String, options: BucketOptions) async throws {
70-
try await execute(
71-
HTTPRequest(
72-
url: configuration.url.appendingPathComponent("bucket/\(id)"),
73-
method: .put,
74-
body: configuration.encoder.encode(
75-
BucketParameters(
76-
id: id,
77-
name: id,
78-
public: options.public,
79-
fileSizeLimit: options.fileSizeLimit,
80-
allowedMimeTypes: options.allowedMimeTypes
81-
)
82-
)
59+
_ = try await execute(
60+
configuration.url.appendingPathComponent("bucket/\(id)"),
61+
method: .put,
62+
body: BucketParameters(
63+
id: id,
64+
name: id,
65+
public: options.public,
66+
fileSizeLimit: options.fileSizeLimit,
67+
allowedMimeTypes: options.allowedMimeTypes
8368
)
84-
)
69+
).serializingData().value
8570
}
8671

8772
/// Removes all objects inside a single bucket.
8873
/// - Parameters:
8974
/// - id: The unique identifier of the bucket you would like to empty.
9075
public func emptyBucket(_ id: String) async throws {
91-
try await execute(
92-
HTTPRequest(
93-
url: configuration.url.appendingPathComponent("bucket/\(id)/empty"),
94-
method: .post
95-
)
96-
)
76+
_ = try await execute(
77+
configuration.url.appendingPathComponent("bucket/\(id)/empty"),
78+
method: .post
79+
).serializingData().value
9780
}
9881

9982
/// Deletes an existing bucket. A bucket can't be deleted with existing objects inside it.
10083
/// You must first `empty()` the bucket.
10184
/// - Parameters:
10285
/// - id: The unique identifier of the bucket you would like to delete.
10386
public func deleteBucket(_ id: String) async throws {
104-
try await execute(
105-
HTTPRequest(
106-
url: configuration.url.appendingPathComponent("bucket/\(id)"),
107-
method: .delete
108-
)
109-
)
87+
_ = try await execute(
88+
configuration.url.appendingPathComponent("bucket/\(id)"),
89+
method: .delete
90+
).serializingData().value
11091
}
11192
}

0 commit comments

Comments
 (0)