|
| 1 | +// |
| 2 | +// network-layer |
| 3 | +// Copyright © 2025 Space Code. All rights reserved. |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import NetworkLayerInterfaces |
| 8 | + |
| 9 | +// MARK: - IQueryParametersFormatter |
| 10 | + |
| 11 | +protocol IQueryParametersFormatter { |
| 12 | + func format(rawParameters: [AnyHashable: Any]) -> [String: String] |
| 13 | +} |
| 14 | + |
| 15 | +// MARK: - QueryParametersFormatter |
| 16 | + |
| 17 | +final class QueryParametersFormatter: IQueryParametersFormatter { |
| 18 | + // MARK: Properties |
| 19 | + |
| 20 | + // Properties |
| 21 | + private let allowedCharacters: CharacterSet = { |
| 22 | + var restricted = CharacterSet(charactersIn: ":/?#[]@!$ &''()*+,;=\"<>%{}|\\^~`") |
| 23 | + restricted.formUnion(.newlines) |
| 24 | + return restricted.inverted |
| 25 | + }() |
| 26 | + |
| 27 | + // MARK: Initialization |
| 28 | + |
| 29 | + init() {} |
| 30 | + |
| 31 | + // MARK: Internal |
| 32 | + |
| 33 | + func format(rawParameters: [AnyHashable: Any]) -> [String: String] { |
| 34 | + var result: [String: String] = [:] |
| 35 | + rawParameters.forEach { key, value in |
| 36 | + guard |
| 37 | + let encodedKey = convertKeyToEncodedString(key), |
| 38 | + let encodedValue = convertValueToEncodedString(value) |
| 39 | + else { |
| 40 | + return |
| 41 | + } |
| 42 | + result[encodedKey] = encodedValue |
| 43 | + } |
| 44 | + return result |
| 45 | + } |
| 46 | + |
| 47 | + // MARK: - Private |
| 48 | + |
| 49 | + private func convertKeyToEncodedString(_ key: AnyHashable) -> String? { |
| 50 | + switch key { |
| 51 | + case let string as String: |
| 52 | + return encodeQueryComponent(string) |
| 53 | + case let encodedComponent as SpecificEncodedComponent: |
| 54 | + return encodedComponent.encodedValue |
| 55 | + case let convertible as CustomStringConvertible: |
| 56 | + return encodeQueryComponent(convertible.description) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private func convertValueToEncodedString(_ value: Any) -> String? { |
| 61 | + switch value { |
| 62 | + case let string as String: |
| 63 | + return encodeQueryComponent(string) |
| 64 | + case is [Any], is [String: Any]: |
| 65 | + guard |
| 66 | + let data = try? JSONSerialization.data(withJSONObject: value, options: [.sortedKeys, .prettyPrinted]), |
| 67 | + let jsonString = String(data: data, encoding: .utf8) |
| 68 | + else { |
| 69 | + return nil |
| 70 | + } |
| 71 | + return encodeQueryComponent(jsonString) |
| 72 | + case let encodedComponent as SpecificEncodedComponent: |
| 73 | + return encodedComponent.encodedValue |
| 74 | + case let convertible as CustomStringConvertible: |
| 75 | + return encodeQueryComponent(convertible.description) |
| 76 | + default: |
| 77 | + return encodeQueryComponent("\(value)") |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private func encodeQueryComponent(_ component: String) -> String? { |
| 82 | + component.addingPercentEncoding(withAllowedCharacters: allowedCharacters) |
| 83 | + } |
| 84 | +} |
0 commit comments