Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ get_all_tasks_1: |-
}
}
get_one_key_1: |-
self.client.getKey(key: "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4") { result in
self.client.getKey(keyOrUid: "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4") { result in
switch result {
case .success(let key):
print(key)
Expand Down
14 changes: 7 additions & 7 deletions Sources/MeiliSearch/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public struct MeiliSearch {
*/
public func getIndexes(
params: IndexesQuery? = nil,
_ completion: @escaping (Result<Results<Index>, Swift.Error>) -> Void) {
_ completion: @escaping (Result<IndexesResults, Swift.Error>) -> Void) {
Indexes.getAll(config: self.config, params: params, completion)
}

Expand Down Expand Up @@ -198,8 +198,8 @@ public struct MeiliSearch {
*/
public func getTasks(
params: TasksQuery? = nil,
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
self.tasks.getAll(params: params, completion)
_ completion: @escaping (Result<TasksResults, Swift.Error>) -> Void) {
self.tasks.getTasks(params: params, completion)
}

// MARK: Keys
Expand All @@ -213,22 +213,22 @@ public struct MeiliSearch {
*/
public func getKeys(
params: KeysQuery? = nil,
_ completion: @escaping (Result<Results<Key>, Swift.Error>) -> Void) {
_ completion: @escaping (Result<KeysResults, Swift.Error>) -> Void) {
self.keys.getAll(params: params, completion)
}

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

- parameter key: The key value.
- parameter keyOrUid: 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,
keyOrUid: String,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
self.keys.get(key: key, completion)
self.keys.get(key: keyOrUid, completion)
}

/**
Expand Down
7 changes: 5 additions & 2 deletions Sources/MeiliSearch/Documents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ struct Documents {
var query: String = "/indexes/\(uid)/documents/\(identifier)"

if fields != nil {
query.append(fields?.joined(separator: ",") ?? "")
let fieldsQuery = "?fields=\(fields?.joined(separator: ",") ?? "")"
query.append(fieldsQuery)
}

self.request.get(api: query) { result in
Expand All @@ -45,7 +46,9 @@ struct Documents {
params: DocumentsQuery? = nil,
_ completion: @escaping (Result<DocumentsResults<T>, Swift.Error>) -> Void)
where T: Codable, T: Equatable {
request.get(api: "/indexes/\(uid)/documents\(params?.toQuery() ?? "")") { result in
let queryParams = params?.toQuery() ?? ""

request.get(api: "/indexes/\(uid)/documents\(queryParams)") { result in
switch result {
case .success(let data):
guard let data: Data = data else {
Expand Down
9 changes: 5 additions & 4 deletions Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public struct Indexes {
completes the query request. It returns a `Result` object that contains `[Index]`
value if the request was successful or `Error` if a failure occurred.
*/
public static func getAll(config: Config, params: IndexesQuery? = nil, _ completion: @escaping (Result<Results<Index>, Swift.Error>) -> Void) {
public static func getAll(config: Config, params: IndexesQuery? = nil, _ completion: @escaping (Result<IndexesResults, Swift.Error>) -> Void) {
Request(config).get(api: "/indexes", param: params?.toQuery()) { result in
switch result {
case .success(let result):
Expand All @@ -111,7 +111,8 @@ public struct Indexes {
}

do {
let indexes: Results<Index> = try Constants.customJSONDecoder.decode(Results<Index>.self, from: result)
let indexes = try Constants.customJSONDecoder.decode(IndexesResults.self, from: result)

completion(.success(indexes))
} catch let error {
completion(.failure(error))
Expand Down Expand Up @@ -471,8 +472,8 @@ public struct Indexes {
*/
public func getTasks(
params: TasksQuery? = nil,
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
self.tasks.getAll(uid: self.uid, params: params, completion)
_ completion: @escaping (Result<TasksResults, Swift.Error>) -> Void) {
self.tasks.getTasks(uid: self.uid, params: params, completion)
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Sources/MeiliSearch/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ struct Keys {
}
}

func getAll(params: KeysQuery?, _ completion: @escaping (Result<Results<Key>, Swift.Error>) -> Void) {
func getAll(params: KeysQuery?, _ completion: @escaping (Result<KeysResults, Swift.Error>) -> Void) {
self.request.get(api: "/keys", param: params?.toQuery()) { 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)
let keys = try Constants.customJSONDecoder.decode(KeysResults.self, from: data)

completion(.success(keys))
} catch {
completion(.failure(error))
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Model/IndexesResults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Foundation
`IndexesResults` is a wrapper used in the indexes routes to handle the returned data.
*/

public struct IndexesResults<T: Decodable & Encodable & Equatable>: Codable, Equatable {
public let results: [T]
public struct IndexesResults: Codable, Equatable {
public let results: [Index]
public let offset: Int
public let limit: Int
public let total: Int
Expand Down
6 changes: 2 additions & 4 deletions Sources/MeiliSearch/Model/KeyParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ public struct KeyParams: Codable, Equatable {

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

if uid != nil {
try container.encode(uid, forKey: .uid)
}

if name != nil {
try container.encode(name, forKey: .name)
}

try container.encode(name, forKey: .name)
try container.encode(description, forKey: .description)
try container.encode(actions, forKey: .actions)
try container.encode(indexes, forKey: .indexes)
Expand Down
12 changes: 12 additions & 0 deletions Sources/MeiliSearch/Model/KeysResults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

/**
`KeysResults` is a wrapper used in the indexes routes to handle the returned data.
*/

public struct KeysResults: Codable, Equatable {
public let results: [Key]
public let offset: Int
public let limit: Int
public let total: Int
}
9 changes: 0 additions & 9 deletions Sources/MeiliSearch/Model/Results.swift

This file was deleted.

12 changes: 12 additions & 0 deletions Sources/MeiliSearch/Model/TasksResults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

/**
`IndexesResults` is a wrapper used in the indexes routes to handle the returned data.
*/

public struct TasksResults: Codable, Equatable {
public let results: [Task]
public let next: Int?
public let from: Int?
public let limit: Int
}
6 changes: 3 additions & 3 deletions Sources/MeiliSearch/QueryParameters/DocumentsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import Foundation
public class DocumentsQuery: Queryable {
private var limit: Int?
private var offset: Int?
private var fields: [String]
private var fields: [String]?

init(limit: Int? = nil, offset: Int? = nil, fields: [String]? = nil) {
self.offset = offset
self.limit = limit
self.fields = fields ?? []
self.fields = fields
}

internal func buildQuery() -> [String: Codable?] {
[
"limit": limit,
"offset": offset,
"fields": fields.isEmpty ? nil : fields.joined(separator: ",")
"fields": fields != nil ? fields?.joined(separator: ",") : nil
]
}
}
3 changes: 2 additions & 1 deletion Sources/MeiliSearch/QueryParameters/TasksQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ public class TasksQuery: Queryable {
private var limit: Int?
private var next: Int?
private var types: [String]
var indexUid: [String]
private var status: [String]

var indexUid: [String]

init(limit: Int? = nil, from: Int? = nil, next: Int? = nil, status: [String]? = nil, types: [String]? = nil, indexUid: [String]? = nil) {
self.from = from
self.limit = limit
Expand Down
22 changes: 11 additions & 11 deletions Sources/MeiliSearch/Tasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ struct Tasks {
}

// get all on client
func getAll(
func getTasks(
params: TasksQuery? = nil,
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
getAll(path: "/tasks", params: params, completion)
_ completion: @escaping (Result<TasksResults, Swift.Error>) -> Void) {
listTasks(params: params, completion)
}

// get all on index
func getAll(
func getTasks(
uid: String,
params: TasksQuery? = nil,
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TasksResults, Swift.Error>) -> Void) {
var query: TasksQuery?

if params != nil {
Expand All @@ -61,18 +61,18 @@ struct Tasks {
query = TasksQuery(indexUid: [uid])
}

getAll(path: "/tasks", params: query, completion)
listTasks(params: query, completion)
}

private func getAll(
path: String,
private func listTasks(
params: TasksQuery? = nil,
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
self.request.get(api: path, param: params?.toQuery()) { result in
_ completion: @escaping (Result<TasksResults, Swift.Error>) -> Void) {
self.request.get(api: "/tasks", param: params?.toQuery()) { result in
switch result {
case .success(let data):
do {
let task: Result<Results<Task>, Swift.Error> = try Constants.resultDecoder(data: data)
let task: Result<TasksResults, Swift.Error> = try Constants.resultDecoder(data: data)

completion(task)
} catch {
completion(.failure(error))
Expand Down
4 changes: 2 additions & 2 deletions Tests/MeiliSearchIntegrationTests/KeysTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class KeysTests: XCTestCase {
self.client.createKey(KeyParams(actions: ["*"], indexes: ["*"], expiresAt: nil)) { result in
switch result {
case .success(let createdKey):
self.client.getKey(key: createdKey.uid) { result in
self.client.getKey(keyOrUid: createdKey.uid) { result in
switch result {
case .success(let fetchedKey):
XCTAssertEqual(fetchedKey.expiresAt, nil)
Expand Down Expand Up @@ -206,7 +206,7 @@ class KeysTests: XCTestCase {
self.client.deleteKey(key: key.key) { result in
switch result {
case .success:
self.client.getKey(key: key.key) { result in
self.client.getKey(keyOrUid: key.key) { result in
switch result {
case .success(let key):
XCTAssertNotNil(key.description)
Expand Down
6 changes: 3 additions & 3 deletions Tests/MeiliSearchIntegrationTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class SearchTests: XCTestCase {
// MARK: Matches tests

func testSearchMatches() {
let expectation = XCTestExpectation(description: "Search for Books using matches")
let expectation = XCTestExpectation(description: "Search for Books using showMatchesPosition")

typealias MeiliResult = Result<SearchResult<Book>, Swift.Error>
let limit = 5
Expand Down Expand Up @@ -793,7 +793,7 @@ class SearchTests: XCTestCase {
// MARK: Facet distribution

func testSearchFacetDistribution() {
let expectation = XCTestExpectation(description: "Search for Books using facets distribution")
let expectation = XCTestExpectation(description: "Search for Books using facets")

configureFilters { result in
switch result {
Expand Down Expand Up @@ -841,7 +841,7 @@ class SearchTests: XCTestCase {
}

func testSearchFacetDistributionNullValue() {
let expectation = XCTestExpectation(description: "Search for Books using facets distribution with 0 value")
let expectation = XCTestExpectation(description: "Search for Books using facets with 0 value")

configureFilters { result in
switch result {
Expand Down
6 changes: 3 additions & 3 deletions Tests/MeiliSearchIntegrationTests/TaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TasksTests: XCTestCase {
}

func testGetTasksIndex() {
let expectation = XCTestExpectation(description: "Add documents")
let expectation = XCTestExpectation(description: "List tasks from index")
let indexUid = "\(self.uid)_\(UUID().uuidString)"

self.client.createIndex(uid: indexUid) { result in
Expand All @@ -67,7 +67,7 @@ class TasksTests: XCTestCase {
switch result {
case .success:
let index = self.client.index(indexUid)
index.getTasks { (result: Result<Results<Task>, Swift.Error>) in
index.getTasks { (result: Result<TasksResults, Swift.Error>) in
switch result {
case .success(let tasks):
// Only one because index has been deleted and recreated
Expand Down Expand Up @@ -134,7 +134,7 @@ class TasksTests: XCTestCase {
self.wait(for: [addDocExpectation], timeout: TESTS_TIME_OUT)

let expectation = XCTestExpectation(description: "Get all tasks of an index")
self.client.getTasks { (result: Result<Results<Task>, Swift.Error>) in
self.client.getTasks { (result: Result<TasksResults, Swift.Error>) in
switch result {
case .success(let tasks):
XCTAssertNotNil(tasks.results)
Expand Down
8 changes: 4 additions & 4 deletions Tests/MeiliSearchUnitTests/DocumentsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Foundation
// swiftlint:disable force_try
// swiftlint:disable line_length
private struct Movie: Codable, Equatable {
let id: Int?
let title: String?
let id: Int
let title: String
let overview: String?
let releaseDate: Date?

Expand Down Expand Up @@ -241,6 +241,8 @@ class DocumentsTests: XCTestCase {
self.index.getDocument(identifier, fields: ["title", "id"]) { (result: Result<Movie, Swift.Error>) in
switch result {
case .success(let movie):
XCTAssertEqual(self.session.nextDataTask.request?.url?.query, "fields=title,id")

XCTAssertEqual(stubMovie, movie)
case .failure:
XCTFail("Failed to get Movies document")
Expand All @@ -250,8 +252,6 @@ class DocumentsTests: XCTestCase {
} catch {
XCTFail("Failed to parse document")
}

// self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

func testGetDocumentsWithParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DocumentsQueryTests: XCTestCase {
["?limit=2": DocumentsQuery(limit: 2, offset: nil)],
["?offset=2": DocumentsQuery(offset: 2)],
["?limit=10&offset=0": DocumentsQuery(limit: 10, offset: 0)],
["?fields=&offset=0": DocumentsQuery(offset: 0, fields: [])],
["": DocumentsQuery()]
]

Expand Down