summaryrefslogtreecommitdiff
path: root/Sora/Data/Booru
diff options
context:
space:
mode:
Diffstat (limited to 'Sora/Data/Booru')
-rw-r--r--Sora/Data/Booru/BooruManager.swift14
-rw-r--r--Sora/Data/Booru/Provider/BooruProviderCredentials.swift36
2 files changed, 46 insertions, 4 deletions
diff --git a/Sora/Data/Booru/BooruManager.swift b/Sora/Data/Booru/BooruManager.swift
index 562bb1e..2459f2d 100644
--- a/Sora/Data/Booru/BooruManager.swift
+++ b/Sora/Data/Booru/BooruManager.swift
@@ -295,10 +295,22 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng
components.scheme = "https"
components.host = domain
components.path = "/posts.json"
- components.queryItems = [
+
+ var queryItems = [
URLQueryItem(name: "page", value: String(page)),
URLQueryItem(name: "tags", value: tagString),
]
+
+ if let validCredentials = credentials {
+ let login = validCredentials.login.trimmingCharacters(in: .whitespacesAndNewlines)
+
+ if !validCredentials.apiKey.isEmpty, !login.isEmpty {
+ queryItems.append(URLQueryItem(name: "login", value: login))
+ queryItems.append(URLQueryItem(name: "api_key", value: validCredentials.apiKey))
+ }
+ }
+
+ components.queryItems = queryItems
url = components.url
case .moebooru:
diff --git a/Sora/Data/Booru/Provider/BooruProviderCredentials.swift b/Sora/Data/Booru/Provider/BooruProviderCredentials.swift
index 898201a..6733405 100644
--- a/Sora/Data/Booru/Provider/BooruProviderCredentials.swift
+++ b/Sora/Data/Booru/Provider/BooruProviderCredentials.swift
@@ -5,17 +5,45 @@ struct BooruProviderCredentials: Codable, Identifiable, Equatable {
let provider: BooruProvider
var apiKey: String
var userID: Int
+ var login: String
- init(provider: BooruProvider, apiKey: String, userID: Int, id: UUID = UUID()) {
+ init(
+ provider: BooruProvider,
+ apiKey: String,
+ userID: Int,
+ login: String = "",
+ id: UUID = UUID()
+ ) {
self.id = id
self.provider = provider
self.apiKey = apiKey
self.userID = userID
+ self.login = login
+ }
+
+ // swiftlint:disable explicit_enum_raw_value
+ private enum CodingKeys: String, CodingKey {
+ case id
+ case provider
+ case apiKey
+ case userID
+ case login
+ }
+ // swiftlint:enable explicit_enum_raw_value
+
+ init(from decoder: Decoder) throws {
+ let container = try decoder.container(keyedBy: CodingKeys.self)
+
+ id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID()
+ provider = try container.decode(BooruProvider.self, forKey: .provider)
+ apiKey = try container.decodeIfPresent(String.self, forKey: .apiKey) ?? ""
+ userID = try container.decodeIfPresent(Int.self, forKey: .userID) ?? 0
+ login = try container.decodeIfPresent(String.self, forKey: .login) ?? ""
}
static func from(
// swiftlint:disable:next large_tuple
- _ rawCredentials: [(provider: BooruProvider, apiKey: String, userID: Int)],
+ _ rawCredentials: [(provider: BooruProvider, apiKey: String, userID: Int, login: String)],
existingCredentials: [Self]
) -> [Self] {
rawCredentials.map { credentials in
@@ -26,6 +54,7 @@ struct BooruProviderCredentials: Codable, Identifiable, Equatable {
provider: credentials.provider,
apiKey: credentials.apiKey,
userID: credentials.userID,
+ login: credentials.login,
id: existingKey.id
)
}
@@ -33,7 +62,8 @@ struct BooruProviderCredentials: Codable, Identifiable, Equatable {
return Self(
provider: credentials.provider,
apiKey: credentials.apiKey,
- userID: credentials.userID
+ userID: credentials.userID,
+ login: credentials.login
)
}
}