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
92 changes: 46 additions & 46 deletions Sources/MeiliSearch/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Settings {
func update(
_ uid: String,
_ setting: Setting,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -51,11 +51,11 @@ struct Settings {
return
}

self.request.post(api: "/indexes/\(uid)/settings", data) { result in
self.request.patch(api: "/indexes/\(uid)/settings", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -69,7 +69,7 @@ struct Settings {
// can this be refactor
func reset(
_ uid: String,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

self.request.delete(api: "/indexes/\(uid)/settings") { result in
switch result {
Expand All @@ -79,7 +79,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -118,7 +118,7 @@ struct Settings {
func updateSynonyms(
_ uid: String,
_ synonyms: [String: [String]]? = [:],
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -127,11 +127,11 @@ struct Settings {
completion(.failure(error))
return
}
self.request.post(api: "/indexes/\(uid)/settings/synonyms", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/synonyms", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -144,7 +144,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/synonyms") { result in
switch result {
Expand All @@ -154,7 +154,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -192,7 +192,7 @@ struct Settings {
func updateStopWords(
_ uid: String,
_ stopWords: [String]? = [],
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -201,11 +201,11 @@ struct Settings {
completion(.failure(error))
return
}
self.request.post(api: "/indexes/\(uid)/settings/stop-words", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/stop-words", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -218,7 +218,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/stop-words") { result in
switch result {
Expand All @@ -228,7 +228,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -266,7 +266,7 @@ struct Settings {
func updateRankingRules(
_ uid: String,
_ rankingRules: [String],
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -276,11 +276,11 @@ struct Settings {
return
}

self.request.post(api: "/indexes/\(uid)/settings/ranking-rules", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/ranking-rules", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -293,7 +293,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/ranking-rules") { result in
switch result {
Expand All @@ -303,7 +303,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -347,21 +347,21 @@ struct Settings {
func updateDistinctAttribute(
_ uid: String,
_ distinctAttribute: String,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
data = try Constants.customJSONEecoder.encode(DistinctAttributePayload(distinctAttribute: distinctAttribute))
data = try JSONEncoder().encode(distinctAttribute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were we able to change Constants.customJSONEecoder. here but not in the other methods?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because this distinct-attribute setting just wants a simple string and the other methods require a complex object/array

} catch {
completion(.failure(error))
return
}

self.request.post(api: "/indexes/\(uid)/settings", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/distinct-attribute", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -374,7 +374,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/distinct-attribute") { result in
switch result {
Expand All @@ -384,7 +384,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -423,7 +423,7 @@ struct Settings {
func updateSearchableAttributes(
_ uid: String,
_ searchableAttributes: [String],
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -432,11 +432,11 @@ struct Settings {
completion(.failure(error))
return
}
self.request.post(api: "/indexes/\(uid)/settings/searchable-attributes", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/searchable-attributes", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -449,7 +449,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/searchable-attributes") { result in
switch result {
Expand All @@ -459,7 +459,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -498,7 +498,7 @@ struct Settings {
func updateDisplayedAttributes(
_ uid: String,
_ displayedAttributes: [String],
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -507,11 +507,11 @@ struct Settings {
completion(.failure(error))
return
}
self.request.post(api: "/indexes/\(uid)/settings/displayed-attributes", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/displayed-attributes", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -524,7 +524,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/displayed-attributes") { result in
switch result {
Expand All @@ -534,7 +534,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -573,7 +573,7 @@ struct Settings {
func updateFilterableAttributes(
_ uid: String,
_ attributes: [String],
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -583,11 +583,11 @@ struct Settings {
return
}

self.request.post(api: "/indexes/\(uid)/settings/filterable-attributes", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/filterable-attributes", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -600,7 +600,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/filterable-attributes") { result in
switch result {
Expand All @@ -610,7 +610,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down Expand Up @@ -649,7 +649,7 @@ struct Settings {
func updateSortableAttributes(
_ uid: String,
_ attributes: [String],
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
_ completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void) {

let data: Data
do {
Expand All @@ -659,11 +659,11 @@ struct Settings {
return
}

self.request.post(api: "/indexes/\(uid)/settings/sortable-attributes", data) { result in
self.request.put(api: "/indexes/\(uid)/settings/sortable-attributes", data) { result in
switch result {
case .success(let data):
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand All @@ -676,7 +676,7 @@ struct Settings {

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

self.request.delete(api: "/indexes/\(uid)/settings/sortable-attributes") { result in
switch result {
Expand All @@ -686,7 +686,7 @@ struct Settings {
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(Task.self, from: data)
let task: TaskInfo = try Constants.customJSONDecoder.decode(TaskInfo.self, from: data)
completion(.success(task))
} catch {
completion(.failure(error))
Expand Down
Loading