Skip to content

Commit

Permalink
Use new for the new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Jul 16, 2024
1 parent b716a38 commit 962ca20
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 13 additions & 3 deletions Sources/Networking/Networking+HTTPRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ public extension Networking {
await cancelRequest(.data, requestType: .get, url: url)
}

func get<T: Decodable>(_ path: String, responseType: T.Type) async -> Result<T, NetworkingError> {
func newGet<T: Decodable>(_ path: String) async -> Result<T, NetworkingError> {
return await handle(.get, path: path, parameters: nil)
}

func post<T: Decodable>(_ path: String, parameters: [String: Any]) async -> Result<T, NetworkingError> {
func newPost<T: Decodable>(_ path: String, parameters: [String: Any]) async -> Result<T, NetworkingError> {
return await handle(.post, path: path, parameters: parameters)
}

func put<T: Decodable>(_ path: String, parameters: [String: Any]) async -> Result<T, NetworkingError> {
func newPost(_ path: String, parameters: [String: Any]) async -> Result<Void, NetworkingError> {
let result: Result<Data, NetworkingError> = await handle(.post, path: path, parameters: parameters)
switch result {
case .success:
return .success(())
case .failure(let error):
return .failure(error)
}
}

func newPut<T: Decodable>(_ path: String, parameters: [String: Any]) async -> Result<T, NetworkingError> {
return await handle(.put, path: path, parameters: parameters)
}

Expand Down
18 changes: 17 additions & 1 deletion Tests/NetworkingTests/GETTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,28 @@ class GETTests: XCTestCase {

func testNewGET() async throws {
let networking = Networking(baseURL: baseURL)
let result = await networking.get("/get", responseType: Friend.self)

let result: Result<Friend, NetworkingError> = await networking.newGet("/get")

switch result {
case .success(let success):
print("worked")
case .failure(let failure):
print(failure.localizedDescription)
}
}

func testNewPOST() async throws {
let networking = Networking(baseURL: baseURL)

let result: Result<Void, NetworkingError> = await networking.newPost("/get", parameters: ["String": "String"])

switch result {
case .success(let success):
print("worked")
case .failure(let failure):
print(failure.localizedDescription)
}
}

}

0 comments on commit 962ca20

Please sign in to comment.