iOS/Sources/Shared/API/Authentication/AuthenticationRoutes.swift
Bruno Pantaleão Gonçalves 74c0567e90
Error when setting variables from Environment and PR comment when using swiftlint disables (#3576)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2025-04-29 12:40:11 +00:00

71 lines
2.0 KiB
Swift

import Alamofire
import Foundation
struct RouteInfo: Alamofire.URLRequestConvertible {
let route: AuthenticationRoute
let baseURL: URL
func asURLRequest() throws -> URLRequest {
try route.asURLRequestWith(baseURL: baseURL)
}
}
enum AuthenticationRoute {
case token(authorizationCode: String)
case refreshToken(token: String)
case revokeToken(token: String)
func asURLRequestWith(baseURL: URL) throws -> URLRequest {
let baseRequest = try URLRequest(url: baseURL.appendingPathComponent(path), method: method)
let request: URLRequest
if let parameters {
request = try URLEncoding.httpBody.encode(baseRequest, with: parameters)
} else {
request = baseRequest
}
return request
}
// MARK: - Private helpers
private var clientID: String {
var clientID = "https://home-assistant.io/iOS"
// swiftlint:disable prohibit_environment_assignment
if Current.appConfiguration == .debug {
clientID = "https://home-assistant.io/iOS/dev-auth"
} else if Current.appConfiguration == .beta {
clientID = "https://home-assistant.io/iOS/beta-auth"
}
// swiftlint:enable prohibit_environment_assignment
return clientID
}
private var method: HTTPMethod {
.post
}
private var parameters: Parameters? {
switch self {
case let .token(authorizationCode):
return ["client_id": clientID, "grant_type": "authorization_code", "code": authorizationCode]
case let .refreshToken(token):
return ["client_id": clientID, "grant_type": "refresh_token", "refresh_token": token]
case let .revokeToken(token):
return ["action": "revoke", "token": token]
}
}
private var path: String {
switch self {
case .token:
return "auth/token"
case .refreshToken:
return "auth/token"
case .revokeToken:
return "auth/token"
}
}
}