Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Now supporting get parameters #281

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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