Skip to content

Commit

Permalink
Merge #252
Browse files Browse the repository at this point in the history
252: Update keys api r=bidoubiwa a=bidoubiwa

Keys API changes! 

## Breaking
- `client.getkeys` now returns a `Results<Key>` object see [specification](https://github.com/meilisearch/specifications/blob/main/text/0085-api-keys.md#as-anna--i-want-to-list-the-api-keys)

## New
- `client.getKey` returns a `Key` object of a specific key
- `client.createKey` returns a `Key` object created with a `KeyParams` object
- `client.updateKey` returns a `Key` object with the updated key
- `client.deleteKey` returns void as the chosen key has been deleted



Co-authored-by: Charlotte Vermandel <[email protected]>
Co-authored-by: cvermand <[email protected]>
  • Loading branch information
3 people authored Feb 14, 2022
2 parents f5a5209 + 026ab54 commit 93ca47d
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 69 deletions.
129 changes: 121 additions & 8 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,128 @@ get_all_tasks_1: |-
print(error)
}
}
get_keys_1: |-
client.keys { (result: Result<Key, Swift.Error>) in
switch result {
case .success(let key):
print(key)
case .failure(let error):
print(error)
}
get_on_key_1: |-
self.client.getKey(key: "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4") { result in
switch result {
case .success(let key):
print(key)
case .failure(let error):
print(error)
}
}
get_all_keys_1: |-
self.client.getKeys() { result in
switch result {
case .success(let keys):
print(keys)
case .failure(let error):
print(error)
}
}
create_a_key_1: |-
let keyParams = KeyParams(
description: "Add documents: Products API key",
actions: ["documents.add"],
indexes: ["products"],
expiresAt: "2042-04-02T00:42:42Z"
)
self.client.createKey(keyParams) { result in
switch result {
case .success(let key):
print(keys)
case .failure(let error):
print(error)
}
}
update_a_key_1: |-
let keyParams = KeyParams(
description: "Add documents: Products API key",
actions: ["documents.add", "documents.delete"],
indexes: ["products", "reviews"],
expiresAt: "2042-04-02T00:42:42Z"
)
self.client.updateKey(
key: "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4"
keyparams: keyParams
) { result in
switch result {
case .success(let key):
print(keys)
case .failure(let error):
print(error)
}
}
delete_a_key_1: |-
self.client.deleteKey(key: "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4")
{ result in
switch result {
case .success:
print("success")
case .failure(let error):
print(error)
}
}
security_guide_search_key_1: |-
client = MeiliSearch(host: "http://localhost:7700", apiKey: "apiKey")
client.index("patient_medical_records")
.search(parameters) { (result: Result<SearchResult<Record>, Swift.Error>) in
switch result {
case .success(let searchResult):
print(searchResult)
case .failure(let error):
print(error)
}
}
security_guide_update_key_1: |-
let keyParams = KeyParams(
indexes: ["doctors"]
)
self.client.updateKey(
key: "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4",
keyParams: keyParams,
) { result in
switch result {
case .success(let key):
print(keys)
case .failure(let error):
print(error)
}
}
security_guide_create_key_1: |-
let keyParams = KeyParams(
description: "Search patient records key",
actions: ["search"],
indexes: ["patient_medical_records"],
expiresAt: "2023-01-01T00:00:00Z"
)
self.client.createKey(keyParams) { result in
switch result {
case .success(let key):
print(keys)
case .failure(let error):
print(error)
}
}
security_guide_list_keys_1: |-
self.client.getKeys() { result in
switch result {
case .success(let keys):
print(keys)
case .failure(let error):
print(error)
}
}
security_guide_delete_key_1: |-
self.client.deleteKey(key: "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4")
{ result in
switch result {
case .success:
print("success")
case .failure(let error):
print(error)
}
}
get_settings_1: |-
client.index("movies").getSettings { (result: Result<Setting, Swift.Error>) in
switch result {
Expand Down
73 changes: 68 additions & 5 deletions Sources/MeiliSearch/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,80 @@ public struct MeiliSearch {
// MARK: Keys

/**
Each instance of MeiliSearch has three keys: a master, a private, and a public. Each key has a given
set of permissions on the API routes.
Get all keys.

- parameter masterKey: Master key to access the `keys` function.
- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Key` value.
If the request was sucessful or `Error` if a failure occured.
*/
public func keys(
public func getKeys(
_ completion: @escaping (Result<Results<Key>, Swift.Error>) -> Void) {
self.keys.getAll(completion)
}

/**
Get one key's information using the key value.

- parameter key: The key value.
- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Key` value.
If the request was sucessful or `Error` if a failure occured.
*/
public func getKey(
key: String,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
self.keys.get(key: key, completion)
}

/**
Create an API key.

- parameter keyParams: Parameters object required to create a key.
- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Key` value.
If the request was sucessful or `Error` if a failure occured.
*/
public func createKey(
_ keyParams: KeyParams,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
self.keys.get(completion)
self.keys.create(keyParams, completion)
}

/**
Update an API key.

- parameter key: The key value.
- parameter keyParams: Parameters object required to update a key.
- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Key` value.
If the request was sucessful or `Error` if a failure occured.
*/
public func updateKey(
key: String,
keyParams: KeyParams,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
self.keys.update(
key: key,
keyParams: keyParams,
completion
)
}

/**
Delete an API key.

- parameter key: The key value.
- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Key` value.
If the request was sucessful or `Error` if a failure occured.
*/
public func deleteKey(
key: String,
_ completion: @escaping (Result<(), Swift.Error>) -> Void) {
self.keys.delete(
key: key,
completion
)
}

// MARK: Stats
Expand Down
94 changes: 91 additions & 3 deletions Sources/MeiliSearch/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ struct Keys {
self.request = request
}

func get(_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
self.request.get(api: "/keys") { result in
func get(key: String, _ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
self.request.get(api: "/keys/\(key)") { result in
switch result {
case .success(let data):

guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
Expand All @@ -32,4 +31,93 @@ struct Keys {
}
}
}

func getAll(_ completion: @escaping (Result<Results<Key>, Swift.Error>) -> Void) {
self.request.get(api: "/keys") { result in
switch result {
case .success(let data):
guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}
do {
let keys: Results<Key> = try Constants.customJSONDecoder.decode(Results<Key>.self, from: data)
completion(.success(keys))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}

public func create(
_ keyParams: KeyParams,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
let data: Data
do {
data = try Constants.customJSONEecoder.encode(keyParams)
} catch {
completion(.failure(MeiliSearch.Error.invalidJSON))
return
}
self.request.post(api: "/keys", data) { result in
switch result {
case .success(let result):
do {
let key: Key = try Constants.customJSONDecoder.decode(
Key.self,
from: result)
completion(.success(key))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}

public func update(
key: String,
keyParams: KeyParams,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
let data: Data
do {
let encoder = JSONEncoder()
data = try encoder.encode(keyParams)
} catch {
completion(.failure(MeiliSearch.Error.invalidJSON))
return
}
self.request.patch(api: "/keys/\(key)", data) { result in
switch result {
case .success(let result):
do {
let key: Key = try Constants.customJSONDecoder.decode(
Key.self,
from: result)
completion(.success(key))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}

public func delete(
key: String,
_ completion: @escaping (Result<(), Swift.Error>) -> Void) {
self.request.delete(api: "/keys/\(key)") { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
}
13 changes: 7 additions & 6 deletions Sources/MeiliSearch/Model/Key.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Foundation

/**
Each instance of MeiliSearch has three keys: a master, a private, and a public.
Each key has a given set of permissions on the API routes.
*/
public struct Key: Codable, Equatable {
// MARK: Properties

/// Private key used to access a determined set of API routes.
public let `private`: String

/// Public key used to access a determined set of API routes.
public let `public`: String
public let description: String
public let key: String
public let actions: [String]
public let indexes: [String]
public let expiresAt: String?
public let createdAt: String
public let updatedAt: String
}
21 changes: 21 additions & 0 deletions Sources/MeiliSearch/Model/KeyParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

/**
`KeyParams` contains all the parameters to create an API key.
*/
public struct KeyParams: Codable, Equatable {
public let description: String
public let actions: [String]
public let indexes: [String]
public let expiresAt: String?

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(description, forKey: .description)
try container.encode(actions, forKey: .actions)
try container.encode(indexes, forKey: .indexes)
try container.encode(expiresAt, forKey: .expiresAt)
}

// MARK: Properties
}
Loading

0 comments on commit 93ca47d

Please sign in to comment.