Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KeyActions enum and rename Task #463

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ disabled_rules:
- identifier_name
- function_body_length
- trailing_comma
- cyclomatic_complexity

analyzer_rules:
- unused_declaration
Expand Down
6 changes: 3 additions & 3 deletions Sources/MeiliSearch/Async/Client+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension MeiliSearch {
/**
See `waitForTask(taskUid:options:_:)`
*/
public func waitForTask(taskUid: Int, options: WaitOptions? = nil) async throws -> Task {
public func waitForTask(taskUid: Int, options: WaitOptions? = nil) async throws -> MTask {
try await withCheckedThrowingContinuation { continuation in
self.waitForTask(taskUid: taskUid, options: options) { result in
continuation.resume(with: result)
Expand All @@ -82,7 +82,7 @@ extension MeiliSearch {
/**
See `waitForTask(task:options:_:)`
*/
public func waitForTask(task: TaskInfo, options: WaitOptions? = nil) async throws -> Task {
public func waitForTask(task: TaskInfo, options: WaitOptions? = nil) async throws -> MTask {
try await withCheckedThrowingContinuation { continuation in
self.waitForTask(task: task, options: options) { result in
continuation.resume(with: result)
Expand All @@ -93,7 +93,7 @@ extension MeiliSearch {
/**
See `getTask(taskUid:_:)`
*/
public func getTask(taskUid: Int) async throws -> Task {
public func getTask(taskUid: Int) async throws -> MTask {
try await withCheckedThrowingContinuation { continuation in
self.getTask(taskUid: taskUid) { result in
continuation.resume(with: result)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Async/Indexes+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ extension Indexes {
/**
See `getTask(taskUid:_:)`
*/
public func getTask(taskUid: Int) async throws -> Task {
public func getTask(taskUid: Int) async throws -> MTask {
try await withCheckedThrowingContinuation { continuation in
self.getTask(taskUid: taskUid) { result in
continuation.resume(with: result)
Expand Down
6 changes: 3 additions & 3 deletions Sources/MeiliSearch/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public struct MeiliSearch {
public func waitForTask(
taskUid: Int,
options: WaitOptions? = nil,
_ completion: @escaping (Result<Task, Swift.Error>
_ completion: @escaping (Result<MTask, Swift.Error>
) -> Void) {
self.tasks.waitForTask(taskUid: taskUid, options: options, completion)
}
Expand All @@ -171,7 +171,7 @@ public struct MeiliSearch {
public func waitForTask(
task: TaskInfo,
options: WaitOptions? = nil,
_ completion: @escaping (Result<Task, Swift.Error>
_ completion: @escaping (Result<MTask, Swift.Error>
) -> Void) {
self.tasks.waitForTask(taskUid: task.taskUid, options: options, completion)
}
Expand All @@ -188,7 +188,7 @@ public struct MeiliSearch {
*/
public func getTask(
taskUid: Int,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<MTask, Swift.Error>) -> Void) {
self.tasks.get(taskUid: taskUid, completion)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public struct Indexes {
*/
public func getTask(
taskUid: Int,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<MTask, Swift.Error>) -> Void) {
self.tasks.get(taskUid: taskUid, completion)
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/MeiliSearch/Model/Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct Key: Codable, Equatable {
/// An alphanumeric key value generated by Meilisearch by hashing the uid and the master key on API key creation.
public let key: String
/// An array of API actions permitted for the key.
public let actions: [String]
public let actions: [KeyAction]
/// An array of strings (indexes names) the key is authorized to act on.
public let indexes: [String]
/// Date and time when the key will expire, represented in RFC 3339 format.
Expand All @@ -29,3 +29,4 @@ public struct Key: Codable, Equatable {
/// Date and time when the key was last updated, represented in RFC 3339 format.
public let updatedAt: String
}

202 changes: 202 additions & 0 deletions Sources/MeiliSearch/Model/KeyAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
///Documentation: https://www.meilisearch.com/docs/reference/api/keys#actions

Check warning on line 1 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Comment Spacing Violation: Prefer at least one space after slashes for comments (comment_spacing)
public enum KeyAction: Equatable, Codable {
case wildcard
case search
case documentsWildcard
case documentsAdd
case documentsGet
case documentsDelete
case indexesWildcard
case indexesCreate
case indexesGet
case indexesUpdate
case indexesDelete
case indexesSwap
case tasksWildcard
case tasksGet
case tasksCancel
case tasksDelete
case settingsWildcard
case settingsGet
case settingsUpdate
case statsGet
case dumpsCreate
case snapshotsCreate
case version
case keysGet
case keysCreate
case keysUpdate
case keysDelete
case unknown(String)

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(String.self)

self = KeyAction(rawValue)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

try container.encode(self.value)
}
}

public extension KeyAction {
init(_ value: String) {
self = switch value {
case "*":
.wildcard
case "search":
.search
case "documents.*":
.documentsWildcard
case "documents.add":
.documentsAdd
case "documents.get":
.documentsGet
case "documents.delete":
.documentsDelete
case "indexes.*":
.indexesWildcard
case "indexes.create":
.indexesCreate
case "indexes.get":
.indexesGet
case "indexes.update":
.indexesUpdate
case "indexes.delete":
.indexesDelete
case "indexes.swap":
.indexesSwap

Check warning on line 72 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "tasks.*":
.tasksWildcard

Check warning on line 74 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "tasks.get":
.tasksGet

Check warning on line 76 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "tasks.cancel":
.tasksCancel

Check warning on line 78 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "tasks.delete":
.tasksDelete

Check warning on line 80 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "settings.*":
.settingsWildcard

Check warning on line 82 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "settings.get":
.settingsGet

Check warning on line 84 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "settings.update":
.settingsUpdate

Check warning on line 86 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "stats.get":
.statsGet

Check warning on line 88 in Sources/MeiliSearch/Model/KeyAction.swift

View workflow job for this annotation

GitHub Actions / linter-check

Indentation Width Violation: Code should be indented using one tab or 2 spaces (indentation_width)
case "dumps.create":
.dumpsCreate
case "snapshots.create":
.snapshotsCreate
case "version":
.version
case "keys.get":
.keysGet
case "keys.create":
.keysCreate
case "keys.update":
.keysUpdate
case "keys.delete":
.keysDelete
default:
.unknown(value)
}
}

var value: String {
switch self {
case .wildcard:
"*"
case .search:
"search"
case .documentsWildcard:
"documents.*"
case .documentsAdd:
"documents.add"
case .documentsGet:
"documents.get"
case .documentsDelete:
"documents.delete"
case .indexesWildcard:
"indexes.*"
case .indexesCreate:
"indexes.create"
case .indexesGet:
"indexes.get"
case .indexesUpdate:
"indexes.update"
case .indexesDelete:
"indexes.delete"
case .indexesSwap:
"indexes.swap"
case .tasksWildcard:
"tasks.*"
case .tasksGet:
"tasks.get"
case .tasksCancel:
"tasks.cancel"
case .tasksDelete:
"tasks.delete"
case .settingsWildcard:
"settings.*"
case .settingsGet:
"settings.get"
case .settingsUpdate:
"settings.update"
case .statsGet:
"stats.get"
case .dumpsCreate:
"dumps.create"
case .snapshotsCreate:
"snapshots.create"
case .version:
"version"
case .keysGet:
"keys.get"
case .keysCreate:
"keys.create"
case .keysUpdate:
"keys.update"
case .keysDelete:
"keys.delete"
case .unknown(let string):
string
}
}
}

extension KeyAction: CaseIterable {
public static var allCases: [KeyAction] {
[
.wildcard,
.search,
.documentsWildcard,
.documentsAdd,
.documentsGet,
.documentsDelete,
.indexesWildcard,
.indexesCreate,
.indexesGet,
.indexesUpdate,
.indexesDelete,
.indexesSwap,
.tasksWildcard,
.tasksGet,
.tasksCancel,
.tasksDelete,
.settingsWildcard,
.settingsGet,
.settingsUpdate,
.statsGet,
.dumpsCreate,
.snapshotsCreate,
.version,
.keysGet,
.keysCreate,
.keysUpdate,
.keysDelete,
]
}
}
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Model/KeyParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public struct KeyParams: Codable, Equatable {
/// A uuid v4 to identify the API key. If not specified, it is automatically generated by Meilisearch.
public var uid: String?
/// An array of API actions permitted for the key.
public let actions: [String]
public let actions: [KeyAction]
/// An array of strings (indexes names) the key is authorized to act on.
public let indexes: [String]
/// Date and time when the key will expire, represented in RFC 3339 format.
Expand All @@ -24,7 +24,7 @@ public struct KeyParams: Codable, Equatable {
description: String? = nil,
name: String? = nil,
uid: String? = nil,
actions: [String],
actions: [KeyAction],
indexes: [String],
expiresAt: String?
) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Model/Task/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
`Task` instances represent the current transaction status, use the `uid` value to
verify the status of your transaction.
*/
public struct Task: Decodable, Equatable {
public struct MTask: Decodable, Equatable {
/// Unique ID for the current `Task`.
public let uid: Int

Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Model/Task/TaskDetails.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public extension Task {
public extension MTask {
enum Details: Equatable {
case indexCreation(TaskIndexCreationDetails)
case indexUpdate(TaskIndexUpdateDetails)
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Model/Task/TaskInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct TaskInfo: Codable, Equatable {
public let indexUid: String?

/// Returns if the task has been successful or not.
public let status: Task.Status
public let status: MTask.Status

/// Type of the task.
public let type: TaskType
Expand All @@ -26,7 +26,7 @@ public struct TaskInfo: Codable, Equatable {

@discardableResult
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func wait(on client: MeiliSearch, options: WaitOptions? = nil) async throws -> Task {
public func wait(on client: MeiliSearch, options: WaitOptions? = nil) async throws -> MTask {
try await client.waitForTask(task: self, options: options)
}
}
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Model/Task/TaskStatus.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public extension Task {
public extension MTask {
enum Status: String, Codable, Equatable {
/// When a task was successfully enqueued and is waiting to be processed.
case enqueued
Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Model/Task/TasksResults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
*/
public struct TasksResults: Decodable, Equatable {
/// Results list containing objects of `Task`.
public let results: [Task]
public let results: [MTask]
/// Integer value used to retrieve the next batch of tasks.
public let next: Int?
/// Integer value representing the first `uid` of the first task returned.
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/QueryParameters/CancelTasksQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class CancelTasksQuery: Queryable {
/// List of strings with all the types the response should contain.
public let types: [TaskType]
/// List of strings with all the statuses the response should contain.
public let statuses: [Task.Status]
public let statuses: [MTask.Status]
/// Filter tasks response by a particular list of index Uids strings
public let indexUids: [String]
/// Filter tasks based on a list of task's uids.
Expand All @@ -22,7 +22,7 @@ public class CancelTasksQuery: Queryable {
public let afterStartedAt: Date?

init(
types: [TaskType]? = nil, statuses: [Task.Status]? = nil,
types: [TaskType]? = nil, statuses: [MTask.Status]? = nil,
indexUids: [String]? = nil, uids: [Int]? = nil,
beforeEnqueuedAt: Date? = nil, afterEnqueuedAt: Date? = nil,
beforeStartedAt: Date? = nil, afterStartedAt: Date? = nil
Expand Down
Loading
Loading