Skip to content
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
19 changes: 10 additions & 9 deletions Sources/BSWFoundation/APIClient/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,17 @@ extension APIClient {
}
}

private enum URLEncoding {
enum URLEncoding {
static func query(_ parameters: [String: Any]) -> String {
var components: [(String, String)] = []

for key in parameters.keys.sorted(by: <) {
let value = parameters[key]!
components += queryComponents(fromKey: key, value: value)
}
return components.map { "\($0)=\($1)" }.joined(separator: "&")
}

static func queryComponents(fromKey key: String, value: Any) -> [(String, String)] {
private static func queryComponents(fromKey key: String, value: Any) -> [(String, String)] {
var components: [(String, String)] = []
if let dictionary = value as? [String: Any] {
for (nestedKey, value) in dictionary {
Expand All @@ -93,16 +92,16 @@ private enum URLEncoding {
components += queryComponents(fromKey: "\(key)[]", value: value)
}
}
else if let value = value as? NSNumber {
if value.isBool {
components.append((escape(key), escape((value.boolValue ? "1" : "0"))))
} else {
components.append((escape(key), escape("\(value)")))
}
else if let value = value as? Int {
components.append((escape(key), escape("\(value)")))
}
else if let bool = value as? Bool {
components.append((escape(key), escape((bool ? "1" : "0"))))
}
else if let date = value as? Date {
let dateString = isoFormatter.string(from: date)
components.append((escape(key), escape(dateString)))
}
else {
components.append((escape(key), escape("\(value)")))
}
Expand Down Expand Up @@ -137,3 +136,5 @@ private extension String {
return String(cleanedSuffix.unicodeScalars.filter { allowed.contains($0) })
}
}

private nonisolated(unsafe) let isoFormatter = ISO8601DateFormatter()
15 changes: 0 additions & 15 deletions Sources/BSWFoundation/Extensions/NSNumber+Extensions.swift

This file was deleted.

12 changes: 12 additions & 0 deletions Tests/BSWFoundationTests/APIClient/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import Foundation

actor RouterTests {

@Test
func queryParams() async throws {
let result = URLEncoding.query([
"hello": true,
"cruel": 1,
"world": "asda",
"what": "https://www.theleftbit.com/",
"are": Date(timeIntervalSince1970: 1751979655),
])
#expect(result == "are=2025-07-08T13%3A00%3A55Z&cruel=1&hello=1&what=https%3A//www.theleftbit.com/&world=asda")
}

@Test
func defaultUserAgentGeneration() async throws {
let sut = APIClient.Router(environment: Giphy.Hosts.production)
Expand Down
Loading