Signal-iOS/SignalServiceKit/src/Util/URLPathComponents.swift
Evan Hahn abd130a4c2
Clean up preauth challenge request
This change should have no user impact. It makes a few cleanups to
`OWSRequestFactory.requestPreauthChallengeRequest`:

- Adds tests
- Converts it to Swift
- Renames it to `requestPreauthChallenge`
- URL-encodes the parameters. This required some additional scaffolding;
  see `URLPathComponents`.
2023-01-25 14:22:22 -06:00

41 lines
984 B
Swift

//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
public struct URLPathComponents: ExpressibleByArrayLiteral, Equatable {
private static var allowedCharacters: CharacterSet {
var result = CharacterSet.urlPathAllowed
result.remove("/")
return result
}
private let pathComponents: [String]
public init(_ pathComponents: [String]) {
self.pathComponents = pathComponents
}
public init(arrayLiteral: String...) {
self.pathComponents = arrayLiteral
}
public var percentEncoded: String {
pathComponents
.compactMap {
$0.nilIfEmpty?.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters)
}
.joined(separator: "/")
}
}
// MARK: - URL extension
public extension URL {
init?(pathComponents: URLPathComponents) {
self.init(string: pathComponents.percentEncoded)
}
}