Merge pull request #300 from LePips/support-redirects

Support Redirects
This commit is contained in:
Ethan Pippin 2022-01-16 19:10:51 -07:00 committed by GitHub
commit 28f23a439a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View File

@ -354,6 +354,8 @@ internal enum L10n {
internal static var system: String { return L10n.tr("Localizable", "system") }
/// Tags
internal static var tags: String { return L10n.tr("Localizable", "tags") }
/// Too Many Redirects
internal static var tooManyRedirects: String { return L10n.tr("Localizable", "tooManyRedirects") }
/// Try again
internal static var tryAgain: String { return L10n.tr("Localizable", "tryAgain") }
/// TV Shows

View File

@ -44,7 +44,8 @@ final class ConnectToServerViewModel: ViewModel {
return message
}
func connectToServer(uri: String) {
func connectToServer(uri: String, redirectCount: Int = 0) {
#if targetEnvironment(simulator)
var uri = uri
if uri == "localhost" {
@ -63,6 +64,27 @@ final class ConnectToServerViewModel: ViewModel {
case .finished: ()
case let .failure(error):
switch error {
case is ErrorResponse:
let errorResponse = error as! ErrorResponse
switch errorResponse {
case let .error(_, _, response, _):
// a url in the response is the result if a redirect
if let newURL = response?.url {
if redirectCount > 2 {
self.handleAPIRequestError(displayMessage: L10n.tooManyRedirects,
logLevel: .critical,
tag: "connectToServer",
completion: completion)
} else {
self
.connectToServer(uri: newURL.absoluteString
.removeRegexMatches(pattern: "/web/index.html"),
redirectCount: redirectCount + 1)
}
} else {
self.handleAPIRequestError(completion: completion)
}
}
case is SwiftfinStore.Errors:
let swiftfinError = error as! SwiftfinStore.Errors
switch swiftfinError {