Skip to content

Commit

Permalink
Merge pull request #33 from phiHero/feat/presets
Browse files Browse the repository at this point in the history
Add presets and stopwords endpoints
  • Loading branch information
jasonbosco authored Aug 7, 2024
2 parents ac11f7a + 5e62e20 commit dad237f
Show file tree
Hide file tree
Showing 25 changed files with 976 additions and 127 deletions.
67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ let (data, response) = try await client.collection(name: "books").overrides().up
### Retrieve all overrides

```swift
let (data, response) = try await client.collection(name: "books").overrides().retrieve(metadataType: Never.self )
let (data, response) = try await client.collection(name: "books").overrides().retrieve(metadataType: Never.self)
```

### Retrieve an override

```swift
let (data, response) = try await client.collection(name: "books").override("test-id").retrieve(metadataType: MetadataType.self )
let (data, response) = try await client.collection(name: "books").override("test-id").retrieve(metadataType: MetadataType.self)
```

### Delete an override
Expand All @@ -124,6 +124,69 @@ let (data, response) = try await client.collection(name: "books").override("test
let (data, response) = try await client.collection(name: "books").override("test-id").delete()
```

### Create or update a preset

```swift
let schema = PresetUpsertSchema(
value: PresetValue.singleCollectionSearch(SearchParameters(q: "apple"))
// or: value: PresetValue.multiSearch(MultiSearchSearchesParameter(searches: [MultiSearchCollectionParameters(q: "apple")]))
)
let (data, response) = try await client.presets().upsert(presetName: "listing_view", params: schema)
```

### Retrieve all presets

```swift
let (data, response) = try await client.presets().retrieve()
```

### Retrieve a preset

```swift
let (data, response) = try await client.preset("listing_view").retrieve()

switch data?.value {
case .singleCollectionSearch(let value):
print(value)
case .multiSearch(let value):
print(value)
}
```

### Delete a preset

```swift
let (data, response) = try await client.preset("listing_view").delete()
```

### Create or update a stopwords set

```swift
let schema = StopwordsSetUpsertSchema(
stopwords: ["states","united"],
locale: "en"
)
let (data, response) = try await client.stopwords().upsert(stopwordsSetId: "stopword_set1", params: schema)
```

### Retrieve all stopwords sets

```swift
let (data, response) = try await client.stopwords().retrieve()
```

### Retrieve a stopwords set

```swift
let (data, response) = try await client.stopword("stopword_set1").retrieve()
```

### Delete a preset

```swift
let (data, response) = try await client.stopword("stopword_set1").delete()
```

## Contributing

Issues and pull requests are welcome on GitHub at [Typesense Swift](https://github.com/typesense/typesense-swift). Do note that the Models used in the Swift client are generated by [Swagger-Codegen](https://github.com/swagger-api/swagger-codegen) and are automated to be modified in order to prevent major errors. So please do use the shell script that is provided in the repo to generate the models:
Expand Down
32 changes: 24 additions & 8 deletions Sources/Typesense/Client.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
import Foundation

public struct Client {

var configuration: Configuration
var apiCall: ApiCall
public var collections: Collections

public init(config: Configuration) {
self.configuration = config
self.apiCall = ApiCall(config: config)
self.collections = Collections(config: config)
}

public func collection(name: String) -> Collection {
return Collection(config: self.configuration, collectionName: name)
}

public func keys() -> ApiKeys {
return ApiKeys(config: self.configuration)
}

public func aliases() -> Alias {
return Alias(config: self.configuration)
}

public func operations() -> Operations {
return Operations(config: self.configuration)
}

public func multiSearch() -> MultiSearch {
return MultiSearch(config: self.configuration)
}

public func analytics() -> Analytics {
return Analytics(config: self.configuration)
}

public func presets() -> Presets {
return Presets(apiCall: apiCall)
}

public func preset(_ presetName: String) -> Preset {
return Preset(apiCall: apiCall, presetName: presetName)
}

public func stopwords() -> Stopwords {
return Stopwords(apiCall: apiCall)
}

public func stopword(_ stopwordsSetId: String) -> Stopword {
return Stopword(apiCall: apiCall, stopwordsSetId: stopwordsSetId)
}
}
21 changes: 21 additions & 0 deletions Sources/Typesense/Models/PresetDeleteSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// PresetDeleteSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct PresetDeleteSchema: Codable {

public var name: String

public init(name: String) {
self.name = name
}


}
15 changes: 15 additions & 0 deletions Sources/Typesense/Models/PresetSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation



public struct PresetSchema: Codable {
public var name: String
public var value: PresetValue

public init(name: String, value: PresetValue) {
self.name = name
self.value = value
}


}
14 changes: 14 additions & 0 deletions Sources/Typesense/Models/PresetUpsertSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation



public struct PresetUpsertSchema: Codable {

public var value: PresetValue

public init(value: PresetValue) {
self.value = value
}


}
28 changes: 28 additions & 0 deletions Sources/Typesense/Models/PresetValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public enum PresetValue: Codable {
case multiSearch(MultiSearchSearchesParameter)
case singleCollectionSearch(SearchParameters)

public init (from decoder: Decoder) throws {
if let multiSearch = try? MultiSearchSearchesParameter(from: decoder) {
self = .multiSearch(multiSearch)
}
else if let singleCollectionSearch = try? SearchParameters(from: decoder) {
self = .singleCollectionSearch(singleCollectionSearch)
} else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Unable to decode value for preset `value`"
)
)
}
}

public func encode(to encoder: Encoder) throws {
switch self {
case .multiSearch(let multiSearch):
try multiSearch.encode(to: encoder)
case .singleCollectionSearch(let singleCollectionSearch):
try singleCollectionSearch.encode(to: encoder)
}
}
}
14 changes: 14 additions & 0 deletions Sources/Typesense/Models/PresetsRetrieveSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation



public struct PresetsRetrieveSchema: Codable {

public var presets: [PresetSchema]

public init(presets: [PresetSchema]) {
self.presets = presets
}


}
16 changes: 16 additions & 0 deletions Sources/Typesense/Models/StopwordsSetDeleteSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation



public struct StopwordsSetDeleteSchema: Codable {
public var _id: String

public init(_id: String) {
self._id = _id
}

public enum CodingKeys: String, CodingKey {
case _id = "id"
}

}
21 changes: 21 additions & 0 deletions Sources/Typesense/Models/StopwordsSetRetrieveSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// StopwordsSetRetrieveSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetRetrieveSchema: Codable {

public var stopwords: StopwordsSetSchema

public init(stopwords: StopwordsSetSchema) {
self.stopwords = stopwords
}


}
30 changes: 30 additions & 0 deletions Sources/Typesense/Models/StopwordsSetSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// StopwordsSetSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetSchema: Codable {

public var _id: String
public var stopwords: [String]
public var locale: String?

public init(_id: String, stopwords: [String], locale: String? = nil) {
self._id = _id
self.stopwords = stopwords
self.locale = locale
}

public enum CodingKeys: String, CodingKey {
case _id = "id"
case stopwords
case locale
}

}
23 changes: 23 additions & 0 deletions Sources/Typesense/Models/StopwordsSetUpsertSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// StopwordsSetUpsertSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetUpsertSchema: Codable {

public var stopwords: [String]
public var locale: String?

public init(stopwords: [String], locale: String? = nil) {
self.stopwords = stopwords
self.locale = locale
}


}
21 changes: 21 additions & 0 deletions Sources/Typesense/Models/StopwordsSetsRetrieveAllSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// StopwordsSetsRetrieveAllSchema.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct StopwordsSetsRetrieveAllSchema: Codable {

public var stopwords: [StopwordsSetSchema]

public init(stopwords: [StopwordsSetSchema]) {
self.stopwords = stopwords
}


}
4 changes: 2 additions & 2 deletions Sources/Typesense/Override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public struct Override {
public func delete() async throws -> (SearchOverrideDeleteResponse?, URLResponse?) {
let (data, response) = try await apiCall.delete(endPoint: endpointPath())
if let result = data {
let overrides = try decoder.decode(SearchOverrideDeleteResponse.self, from: result)
return (overrides, response)
let decodedData = try decoder.decode(SearchOverrideDeleteResponse.self, from: result)
return (decodedData, response)
}
return (nil, response)
}
Expand Down
Loading

0 comments on commit dad237f

Please sign in to comment.