Skip to content

Commit

Permalink
Puts back async NetworkingService shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Nov 14, 2023
1 parent e702595 commit f92cf27
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 158 deletions.
31 changes: 28 additions & 3 deletions Sources/Networking/Async Api/Delete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public extension NetworkingClient {
}

func delete<T: Decodable>(_ route: String,
keypath: String? = nil) async throws -> T {
keypath: String? = nil) async throws -> T {
let json: Any = try await delete(route)
return try self.toModel(json, keypath: keypath)
}

func delete<T: Decodable>(_ route: String,
keypath: String? = nil) async throws -> T where T: Collection {
keypath: String? = nil) async throws -> T where T: Collection {
let keypath = keypath ?? defaultCollectionParsingKeyPath
let json: Any = try await delete(route)
return try self.toModel(json, keypath: keypath)
}

func delete(_ route: String) async throws -> Any {
let data: Data = try await delete(route)
return try JSONSerialization.jsonObject(with: data, options: [])
Expand All @@ -35,3 +35,28 @@ public extension NetworkingClient {
try await request(.delete, route).execute()
}
}

public extension NetworkingService {

func delete(_ route: String) async throws {
return try await network.delete(route)
}

func delete<T: Decodable>(_ route: String,
keypath: String? = nil) async throws -> T {
try await network.delete(route, keypath: keypath)
}

func delete<T: Decodable>(_ route: String,
keypath: String? = nil) async throws -> T where T: Collection {
try await network.delete(route, keypath: keypath)
}

func delete(_ route: String) async throws -> Any {
try await network.delete(route)
}

func delete(_ route: String) async throws -> Data {
try await network.delete(route)
}
}
28 changes: 28 additions & 0 deletions Sources/Networking/Async Api/Get.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,31 @@ public extension NetworkingClient {
try await request(.get, route, urlParams: urlParams).execute()
}
}

public extension NetworkingService {

func get(_ route: String, urlParams: Params? = nil) async throws {
return try await network.get(route, urlParams: urlParams)
}

func get<T: Decodable>(_ route: String,
urlParams: Params? = nil,
keypath: String? = nil) async throws -> T {
try await network.get(route, urlParams: urlParams, keypath: keypath)
}

func get<T: Decodable>(_ route: String,
urlParams: Params? = nil,
keypath: String? = nil) async throws -> T where T: Collection {
try await network.get(route, urlParams: urlParams, keypath: keypath)
}


func get(_ route: String, urlParams: Params? = nil) async throws -> Any {
try await network.get(route, urlParams: urlParams)
}

func get(_ route: String, urlParams: Params? = nil) async throws -> Data {
try await network.get(route, urlParams: urlParams)
}
}
27 changes: 27 additions & 0 deletions Sources/Networking/Async Api/Patch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,30 @@ public extension NetworkingClient {
}

}

public extension NetworkingService {

func patch(_ route: String, body: HTTPBody? = nil) async throws {
return try await network.patch(route, body: body)
}

func patch<T: Decodable>(_ route: String,
body: HTTPBody? = nil,
keypath: String? = nil) async throws -> T {
try await network.patch(route, body: body, keypath: keypath)
}

func patch<T: Decodable>(_ route: String,
body: HTTPBody? = nil,
keypath: String? = nil) async throws -> T where T: Collection {
try await network.patch(route, body: body, keypath: keypath)
}

func patch(_ route: String, body: HTTPBody? = nil) async throws -> Any {
try await network.patch(route, body: body)
}

func patch(_ route: String, body: HTTPBody? = nil) async throws -> Data {
try await network.patch(route, body: body)
}
}
28 changes: 27 additions & 1 deletion Sources/Networking/Async Api/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public extension NetworkingClient {
let _: Data = try await post(route, body: body)
}


func post<T: Decodable>(_ route: String,
body: HTTPBody? = nil,
keypath: String? = nil) async throws -> T {
Expand All @@ -38,3 +37,30 @@ public extension NetworkingClient {
try await request(.post, route, body: body).execute()
}
}

public extension NetworkingService {

func post(_ route: String, body: HTTPBody? = nil) async throws {
return try await network.post(route, body: body)
}

func post<T: Decodable>(_ route: String,
body: HTTPBody? = nil,
keypath: String? = nil) async throws -> T {
try await network.post(route, body: body, keypath: keypath)
}

func post<T: Decodable>(_ route: String,
body: HTTPBody? = nil,
keypath: String? = nil) async throws -> T where T: Collection {
try await network.post(route, body: body, keypath: keypath)
}

func post(_ route: String, body: HTTPBody? = nil) async throws -> Any {
try await network.post(route, body: body)
}

func post(_ route: String, body: HTTPBody? = nil) async throws -> Data {
try await network.post(route, body: body)
}
}
27 changes: 27 additions & 0 deletions Sources/Networking/Async Api/Put.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,30 @@ public extension NetworkingClient {
try await request(.put, route, body: body).execute()
}
}

public extension NetworkingService {

func put(_ route: String, body: HTTPBody? = nil) async throws {
return try await network.put(route, body: body)
}

func put<T: Decodable>(_ route: String,
body: HTTPBody? = nil,
keypath: String? = nil) async throws -> T {
try await network.put(route, body: body, keypath: keypath)
}

func put<T: Decodable>(_ route: String,
body: HTTPBody? = nil,
keypath: String? = nil) async throws -> T where T: Collection {
try await network.put(route, body: body, keypath: keypath)
}

func put(_ route: String, body: HTTPBody? = nil) async throws -> Any {
try await network.put(route, body: body)
}

func put(_ route: String, body: HTTPBody? = nil) async throws -> Data {
try await network.put(route, body: body)
}
}
155 changes: 1 addition & 154 deletions Sources/Networking/NetworkingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public protocol NetworkingService {

// Sugar, just forward calls to underlying network client

// TODO Put back
/*
public extension NetworkingService {
Expand Down Expand Up @@ -229,158 +230,4 @@ public extension NetworkingService {
network.delete(route, params: params, keypath: keypath)
}
}
// Async
public extension NetworkingService {
// Data
func get(_ route: String, params: Params = Params()) async throws -> Data {
try await network.get(route, params: params)
}
func post(_ route: String, params: Params = Params()) async throws -> Data {
try await network.post(route, params: params)
}
func post(_ route: String, body: Encodable) async throws -> Data {
try await network.post(route, body: body)
}
func put(_ route: String, params: Params = Params()) async throws -> Data {
try await network.put(route, params: params)
}
func patch(_ route: String, params: Params = Params()) async throws -> Data {
try await network.patch(route, params: params)
}
func delete(_ route: String, params: Params = Params()) async throws -> Data {
try await network.delete(route, params: params)
}
// Void
func get(_ route: String, params: Params = Params()) async throws {
return try await network.get(route, params: params)
}
func post(_ route: String, params: Params = Params()) async throws {
return try await network.post(route, params: params)
}
func post(_ route: String, body: Encodable) async throws {
return try await network.post(route, body: body)
}
func put(_ route: String, params: Params = Params()) async throws {
return try await network.put(route, params: params)
}
func patch(_ route: String, params: Params = Params()) async throws {
return try await network.patch(route, params: params)
}
func delete(_ route: String, params: Params = Params()) async throws {
return try await network.delete(route, params: params)
}
// JSON
func get(_ route: String, params: Params = Params()) async throws -> Any {
try await network.get(route, params: params)
}
func post(_ route: String, params: Params = Params()) async throws -> Any {
try await network.post(route, params: params)
}
func post(_ route: String, body: Encodable) async throws -> Any {
try await network.post(route, body: body)
}
func put(_ route: String, params: Params = Params()) async throws -> Any {
try await network.put(route, params: params)
}
func patch(_ route: String, params: Params = Params()) async throws -> Any {
try await network.patch(route, params: params)
}
func delete(_ route: String, params: Params = Params()) async throws -> Any {
try await network.delete(route, params: params)
}
// Decodable
func get<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T {
try await network.get(route, params: params, keypath: keypath)
}
func post<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T {
try await network.post(route, params: params, keypath: keypath)
}
func post<T: Decodable>(_ route: String, body: Encodable) async throws -> T {
try await network.post(route, body: body)
}
func put<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T {
try await network.put(route, params: params, keypath: keypath)
}
func patch<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T {
try await network.patch(route, params: params, keypath: keypath)
}
func patch<T: Decodable>(_ route: String, body: Encodable) async throws -> T {
try await network.patch(route, body: body)
}
func delete<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T {
try await network.delete(route, params: params, keypath: keypath)
}
// Array Decodable
func get<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T where T: Collection {
try await network.get(route, params: params, keypath: keypath)
}
func post<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T where T: Collection {
try await network.post(route, params: params, keypath: keypath)
}
func put<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T where T: Collection {
try await network.put(route, params: params, keypath: keypath)
}
func patch<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T where T: Collection {
try await network.patch(route, params: params, keypath: keypath)
}
func delete<T: Decodable>(_ route: String,
params: Params = Params(),
keypath: String? = nil) async throws -> T where T: Collection {
try await network.delete(route, params: params, keypath: keypath)
}
}
*/

0 comments on commit f92cf27

Please sign in to comment.