forked from jellyfin/Swiftfin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[iOS] Admin Dashboard - API Keys (jellyfin#1284)
* API Keys * Switch Deletion Alert for a Confirmation Dialog * Migrate from a list to a Collection VGrid. * Convert back to List. Also, now using my events! So, there is a confirmation and a failure message for both delete & create API. * want vs wish * Merge Issue Fixes * Review Changes * Reset newAPIName after creating a new API * cleanup --------- Co-authored-by: Ethan Pippin <[email protected]>
- Loading branch information
Showing
8 changed files
with
458 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// Swiftfin is subject to the terms of the Mozilla Public | ||
// License, v2.0. If a copy of the MPL was not distributed with this | ||
// file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) 2024 Jellyfin & Jellyfin Contributors | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import JellyfinAPI | ||
import OrderedCollections | ||
|
||
// TODO: for APIKey updating, could temp set new APIKeys | ||
|
||
final class APIKeysViewModel: ViewModel, Stateful { | ||
|
||
// MARK: Action | ||
|
||
enum Action: Equatable { | ||
case getAPIKeys | ||
case createAPIKey(name: String) | ||
case deleteAPIKey(key: String) | ||
} | ||
|
||
// MARK: State | ||
|
||
enum State: Hashable { | ||
case initial | ||
case error(JellyfinAPIError) | ||
case content | ||
} | ||
|
||
// MARK: Published Variables | ||
|
||
@Published | ||
final var apiKeys: [AuthenticationInfo] = [] | ||
@Published | ||
final var state: State = .initial | ||
|
||
// MARK: Action Responses | ||
|
||
func respond(to action: Action) -> State { | ||
switch action { | ||
case .getAPIKeys: | ||
Task { | ||
do { | ||
try await getAPIKeys() | ||
|
||
await MainActor.run { | ||
self.state = .content | ||
} | ||
} catch { | ||
await MainActor.run { | ||
self.state = .error(.init(error.localizedDescription)) | ||
} | ||
} | ||
} | ||
.store(in: &cancellables) | ||
case let .createAPIKey(name): | ||
Task { | ||
do { | ||
try await createAPIKey(name: name) | ||
|
||
await MainActor.run { | ||
self.state = .content | ||
} | ||
} catch { | ||
await MainActor.run { | ||
self.state = .error(.init(error.localizedDescription)) | ||
} | ||
} | ||
} | ||
.store(in: &cancellables) | ||
case let .deleteAPIKey(key): | ||
Task { | ||
do { | ||
try await deleteAPIKey(key: key) | ||
|
||
await MainActor.run { | ||
self.state = .content | ||
} | ||
} catch { | ||
await MainActor.run { | ||
self.state = .error(.init(error.localizedDescription)) | ||
} | ||
} | ||
} | ||
.store(in: &cancellables) | ||
} | ||
|
||
return state | ||
} | ||
|
||
private func getAPIKeys() async throws { | ||
let request = Paths.getKeys | ||
let response = try await userSession.client.send(request) | ||
|
||
guard let items = response.value.items else { return } | ||
|
||
await MainActor.run { | ||
self.apiKeys = items | ||
} | ||
} | ||
|
||
private func createAPIKey(name: String) async throws { | ||
let request = Paths.createKey(app: name) | ||
try await userSession.client.send(request).value | ||
|
||
try await getAPIKeys() | ||
} | ||
|
||
private func deleteAPIKey(key: String) async throws { | ||
let request = Paths.revokeKey(key: key) | ||
try await userSession.client.send(request) | ||
|
||
try await getAPIKeys() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.