Skip to content

Commit 9d498ad

Browse files
committed
Adds NetworkingService async-await shortcuts
1 parent 949a1af commit 9d498ad

File tree

1 file changed

+137
-8
lines changed

1 file changed

+137
-8
lines changed

Sources/Networking/NetworkingService.swift

Lines changed: 137 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ public extension NetworkingService {
8787
func get<T: Decodable>(_ route: String,
8888
params: Params = Params(),
8989
keypath: String? = nil) -> AnyPublisher<T, Error> {
90-
return get(route, params: params)
91-
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) }
92-
.receive(on: DispatchQueue.main)
93-
.eraseToAnyPublisher()
90+
network.get(route, params: params, keypath: keypath)
9491
}
9592

9693
func post<T: Decodable>(_ route: String,
@@ -154,10 +151,7 @@ public extension NetworkingService {
154151
func get<T: NetworkingJSONDecodable>(_ route: String,
155152
params: Params = Params(),
156153
keypath: String? = nil) -> AnyPublisher<T, Error> {
157-
return get(route, params: params)
158-
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) }
159-
.receive(on: DispatchQueue.main)
160-
.eraseToAnyPublisher()
154+
network.get(route, params: params, keypath: keypath)
161155
}
162156

163157
func post<T: NetworkingJSONDecodable>(_ route: String,
@@ -218,3 +212,138 @@ public extension NetworkingService {
218212
network.delete(route, params: params, keypath: keypath)
219213
}
220214
}
215+
216+
// Async
217+
public extension NetworkingService {
218+
219+
// Data
220+
221+
func get(_ route: String, params: Params = Params()) async throws -> Data {
222+
try await network.get(route, params: params)
223+
}
224+
225+
func post(_ route: String, params: Params = Params()) async throws -> Data {
226+
try await network.post(route, params: params)
227+
}
228+
229+
func put(_ route: String, params: Params = Params()) async throws -> Data {
230+
try await network.put(route, params: params)
231+
}
232+
233+
func patch(_ route: String, params: Params = Params()) async throws -> Data {
234+
try await network.patch(route, params: params)
235+
}
236+
237+
func delete(_ route: String, params: Params = Params()) async throws -> Data {
238+
try await network.delete(route, params: params)
239+
}
240+
241+
// Void
242+
243+
func get(_ route: String, params: Params = Params()) async throws {
244+
return try await network.get(route, params: params)
245+
}
246+
247+
func post(_ route: String, params: Params = Params()) async throws {
248+
return try await network.post(route, params: params)
249+
}
250+
251+
func put(_ route: String, params: Params = Params()) async throws {
252+
return try await network.put(route, params: params)
253+
}
254+
255+
func patch(_ route: String, params: Params = Params()) async throws {
256+
return try await network.patch(route, params: params)
257+
}
258+
259+
func delete(_ route: String, params: Params = Params()) async throws {
260+
return try await network.delete(route, params: params)
261+
}
262+
263+
// JSON
264+
265+
func get(_ route: String, params: Params = Params()) async throws -> Any {
266+
try await network.get(route, params: params)
267+
}
268+
269+
func post(_ route: String, params: Params = Params()) async throws -> Any {
270+
try await network.post(route, params: params)
271+
}
272+
273+
func put(_ route: String, params: Params = Params()) async throws -> Any {
274+
try await network.put(route, params: params)
275+
}
276+
277+
func patch(_ route: String, params: Params = Params()) async throws -> Any {
278+
try await network.patch(route, params: params)
279+
}
280+
281+
func delete(_ route: String, params: Params = Params()) async throws -> Any {
282+
try await network.delete(route, params: params)
283+
}
284+
285+
// Decodable
286+
287+
func get<T: Decodable>(_ route: String,
288+
params: Params = Params(),
289+
keypath: String? = nil) async throws -> T {
290+
try await network.get(route, params: params, keypath: keypath)
291+
}
292+
293+
func post<T: Decodable>(_ route: String,
294+
params: Params = Params(),
295+
keypath: String? = nil) async throws -> T {
296+
try await network.post(route, params: params, keypath: keypath)
297+
}
298+
299+
func put<T: Decodable>(_ route: String,
300+
params: Params = Params(),
301+
keypath: String? = nil) async throws -> T {
302+
try await network.put(route, params: params, keypath: keypath)
303+
}
304+
305+
func patch<T: Decodable>(_ route: String,
306+
params: Params = Params(),
307+
keypath: String? = nil) async throws -> T {
308+
try await network.patch(route, params: params, keypath: keypath)
309+
}
310+
311+
func delete<T: Decodable>(_ route: String,
312+
params: Params = Params(),
313+
keypath: String? = nil) async throws -> T {
314+
try await network.delete(route, params: params, keypath: keypath)
315+
}
316+
317+
// Array Decodable
318+
319+
func get<T: Decodable>(_ route: String,
320+
params: Params = Params(),
321+
keypath: String? = nil) async throws -> T where T: Collection {
322+
try await network.get(route, params: params, keypath: keypath)
323+
}
324+
325+
func post<T: Decodable>(_ route: String,
326+
params: Params = Params(),
327+
keypath: String? = nil) async throws -> T where T: Collection {
328+
try await network.post(route, params: params, keypath: keypath)
329+
}
330+
331+
func put<T: Decodable>(_ route: String,
332+
params: Params = Params(),
333+
keypath: String? = nil) async throws -> T where T: Collection {
334+
try await network.put(route, params: params, keypath: keypath)
335+
}
336+
337+
func patch<T: Decodable>(_ route: String,
338+
params: Params = Params(),
339+
keypath: String? = nil) async throws -> T where T: Collection {
340+
try await network.patch(route, params: params, keypath: keypath)
341+
}
342+
343+
func delete<T: Decodable>(_ route: String,
344+
params: Params = Params(),
345+
keypath: String? = nil) async throws -> T where T: Collection {
346+
try await network.delete(route, params: params, keypath: keypath)
347+
}
348+
}
349+

0 commit comments

Comments
 (0)