Skip to content

Commit

Permalink
Refactor after Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherlouk committed Sep 27, 2023
1 parent 330acbc commit a5cacc9
Showing 1 changed file with 45 additions and 76 deletions.
121 changes: 45 additions & 76 deletions Sources/MeiliSearch/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,51 @@ struct Settings {
resetSetting(uid: uid, key: "pagination", completion: completion)
}

// MARK: Typo Tolerance

func getTypoTolerance(
_ uid: String,
_ completion: @escaping (Result<TypoToleranceResult, Swift.Error>) -> Void) {

getSetting(uid: uid, key: "typo-tolerance", completion: completion)
}

func updateTypoTolerance(
_ uid: String,
_ setting: TypoTolerance,
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
data = try JSONEncoder().encode(setting)
} catch {
completion(.failure(error))
return
}

// this uses patch instead of put for networking, so shouldn't use the reusable 'updateSetting' function
self.request.patch(api: "/indexes/\(uid)/settings/typo-tolerance", data) { result in
switch result {
case .success(let data):
do {
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}

func resetTypoTolerance(
_ uid: String,
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

resetSetting(uid: uid, key: "typo-tolerance", completion: completion)
}

// MARK: Reusable Requests

private func getSetting<ResponseType: Decodable>(
Expand Down Expand Up @@ -456,80 +501,4 @@ struct Settings {
}
}
}

// MARK: Typo Tolerance

func getTypoTolerance(
_ uid: String,
_ completion: @escaping (Result<TypoToleranceResult, Swift.Error>) -> Void) {

self.request.get(api: "/indexes/\(uid)/settings/typo-tolerance") { result in
switch result {
case .success(let data):
guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}
do {
let response: TypoToleranceResult = try Constants.customJSONDecoder.decode(TypoToleranceResult.self, from: data)
completion(.success(response))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}

func updateTypoTolerance(
_ uid: String,
_ setting: TypoTolerance,
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
data = try JSONEncoder().encode(setting)
} catch {
completion(.failure(error))
return
}

self.request.patch(api: "/indexes/\(uid)/settings/typo-tolerance", data) { result in
switch result {
case .success(let data):
do {
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}

func resetTypoTolerance(
_ uid: String,
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

self.request.delete(api: "/indexes/\(uid)/settings/typo-tolerance") { result in
switch result {
case .success(let data):
guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}
do {
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}

0 comments on commit a5cacc9

Please sign in to comment.