Skip to content

Commit

Permalink
Now supporting get parameters (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis authored Oct 13, 2024
1 parent 28f479e commit 127ac61
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Sources/Networking/Networking+HTTPRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public extension Networking {
await cancelRequest(.data, requestType: .get, url: url)
}

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

func newPost<T: Decodable>(_ path: String, parameters: [String: Any]) async -> Result<T, NetworkingError> {
Expand Down
31 changes: 28 additions & 3 deletions Sources/Networking/Networking+New.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,35 @@ extension Networking {
}

private func createRequest(path: String, requestType: RequestType, parameters: Any?) throws -> URLRequest {
let parameterType: Networking.ParameterType? = parameters != nil ? .json : nil
var request = URLRequest(url: try composedURL(with: path), requestType: requestType, path: path, parameterType: parameterType, responseType: .json, boundary: boundary, authorizationHeaderValue: authorizationHeaderValue, token: token, authorizationHeaderKey: authorizationHeaderKey, headerFields: headerFields)
guard var urlComponents = URLComponents(string: try composedURL(with: path).absoluteString) else {
throw URLError(.badURL)
}

if requestType == .get, let queryParameters = parameters as? [String: Any] {
urlComponents.queryItems = queryParameters.map { key, value in
URLQueryItem(name: key, value: "\(value)")
}
}

guard let url = urlComponents.url else {
throw URLError(.badURL)
}

if let parameters = parameters {
let parameterType: Networking.ParameterType? = (requestType == .get || parameters == nil) ? nil : .json
var request = URLRequest(
url: url,
requestType: requestType,
path: path,
parameterType: parameterType,
responseType: .json,
boundary: boundary,
authorizationHeaderValue: authorizationHeaderValue,
token: token,
authorizationHeaderKey: authorizationHeaderKey,
headerFields: headerFields
)

if requestType != .get, let parameters = parameters {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
}

Expand Down
24 changes: 24 additions & 0 deletions Tests/NetworkingTests/NewNetworkingTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import XCTest
import CoreLocation
@testable import Networking

class NewNetworkingTests: XCTestCase {
Expand All @@ -18,6 +19,29 @@ class NewNetworkingTests: XCTestCase {
}
}

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

let pickupCoordinate = CLLocationCoordinate2D(latitude: 59.91700978556453, longitude: 10.760668740407757)
let deliveryCoordinate = CLLocationCoordinate2D(latitude: 59.937611066825674, longitude: 10.735343079276985)

let parameters = [
"pickup_latitude": pickupCoordinate.latitude,
"pickup_longitude": pickupCoordinate.longitude,
"delivery_latitude": deliveryCoordinate.latitude,
"delivery_longitude": deliveryCoordinate.longitude
]

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

switch result {
case .success(_):
print("Test passed")
case .failure(let error):
print("error \(error)")
}
}

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

Expand Down

0 comments on commit 127ac61

Please sign in to comment.