Skip to content

Commit b28f8a6

Browse files
authored
Merge pull request #30 from willtempleton/update/v0-25
Updates for Typesense v0.25
2 parents 955c0ed + 199765b commit b28f8a6

39 files changed

+922
-56
lines changed

Sources/Typesense/Analytics.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
3+
public struct Analytics {
4+
static let resourcePath: String = "/analytics"
5+
6+
private var analyticsRules: AnalyticsRules
7+
var apiCall: ApiCall
8+
9+
init(config: Configuration) {
10+
self.apiCall = ApiCall(config: config)
11+
self.analyticsRules = AnalyticsRules(apiCall: apiCall)
12+
}
13+
14+
func rule(id: String) -> AnalyticsRule {
15+
return AnalyticsRule(name: id, apiCall: self.apiCall)
16+
}
17+
18+
func rules() -> AnalyticsRules {
19+
return AnalyticsRules(apiCall: self.apiCall)
20+
}
21+
}
22+
23+
24+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Foundation
2+
3+
public struct AnalyticsRule {
4+
var name: String
5+
private var apiCall: ApiCall
6+
init(name: String, apiCall: ApiCall) {
7+
self.name = name
8+
self.apiCall = apiCall
9+
}
10+
11+
public func retrieve() async throws -> (AnalyticsRuleSchema?, URLResponse?) {
12+
let (data, response) = try await self.apiCall.get(endPoint: endpointPath())
13+
if let result = data {
14+
if let notFound = try? decoder.decode(ApiResponse.self, from: result) {
15+
throw ResponseError.analyticsRuleDoesNotExist(desc: notFound.message)
16+
}
17+
let fetchedRule = try decoder.decode(AnalyticsRuleSchema.self, from: result)
18+
return (fetchedRule, response)
19+
}
20+
return (nil, response)
21+
}
22+
23+
public func delete() async throws -> (AnalyticsRuleSchema?, URLResponse?) {
24+
let (data, response) = try await self.apiCall.delete(endPoint: endpointPath())
25+
if let result = data {
26+
if let notFound = try? decoder.decode(ApiResponse.self, from: result) {
27+
throw ResponseError.analyticsRuleDoesNotExist(desc: notFound.message)
28+
}
29+
let deletedRule = try decoder.decode(AnalyticsRuleSchema.self, from: result)
30+
return (deletedRule, response)
31+
}
32+
return (nil, response)
33+
}
34+
35+
private func endpointPath() -> String {
36+
return "\(AnalyticsRules.resourcePath)/\(name)"
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Foundation
2+
3+
public struct AnalyticsRules {
4+
private var apiCall: ApiCall
5+
static var resourcePath: String = "\(Analytics.resourcePath)/rules"
6+
7+
init(apiCall: ApiCall) {
8+
self.apiCall = apiCall
9+
}
10+
11+
func upsert(params: AnalyticsRuleSchema) async throws -> (AnalyticsRuleSchema?, URLResponse?) {
12+
let ruleData = try encoder.encode(params)
13+
let (data, response) = try await self.apiCall.put(endPoint: endpointPath(params.name), body: ruleData)
14+
if let result = data {
15+
let ruleResult = try decoder.decode(AnalyticsRuleSchema.self, from: result)
16+
return (ruleResult, response)
17+
}
18+
19+
return (nil, response)
20+
}
21+
22+
func retrieveAll() async throws -> (AnalyticsRulesRetrieveSchema?, URLResponse?) {
23+
let (data, response) = try await self.apiCall.get(endPoint: endpointPath())
24+
if let result = data {
25+
let rules = try decoder.decode(AnalyticsRulesRetrieveSchema.self, from: result)
26+
return (rules, response)
27+
}
28+
29+
return (nil, response)
30+
}
31+
32+
private func endpointPath(_ operation: String? = nil) -> String {
33+
if let operation = operation {
34+
return "\(AnalyticsRules.resourcePath)/\(operation)"
35+
} else {
36+
return AnalyticsRules.resourcePath
37+
}
38+
}
39+
}

Sources/Typesense/Client.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ public struct Client {
3131
public func multiSearch() -> MultiSearch {
3232
return MultiSearch(config: self.configuration)
3333
}
34+
35+
public func analytics() -> Analytics {
36+
return Analytics(config: self.configuration)
37+
}
3438
}

Sources/Typesense/Documents.swift

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public struct Documents {
6262
searchQueryParams.append(URLQueryItem(name: "query_by_weights", value: queryByWeights))
6363
}
6464

65-
if let maxHits = searchParameters.maxHits {
66-
searchQueryParams.append(URLQueryItem(name: "max_hits", value: maxHits))
65+
if let textMatchType = searchParameters.textMatchType {
66+
searchQueryParams.append(URLQueryItem(name: "text_match_type", value: textMatchType))
6767
}
6868

6969
if let _prefix = searchParameters._prefix {
@@ -72,10 +72,21 @@ public struct Documents {
7272
fullString.append(String(item))
7373
fullString.append(",")
7474
}
75-
7675
searchQueryParams.append(URLQueryItem(name: "prefix", value: String(fullString.dropLast())))
7776
}
7877

78+
if let _infix = searchParameters._infix {
79+
searchQueryParams.append(URLQueryItem(name: "infix", value: _infix))
80+
}
81+
82+
if let maxExtraPrefix = searchParameters.maxExtraPrefix {
83+
searchQueryParams.append(URLQueryItem(name: "max_extra_prefix", value: String(maxExtraPrefix)))
84+
}
85+
86+
if let maxExtraSuffix = searchParameters.maxExtraSuffix {
87+
searchQueryParams.append(URLQueryItem(name: "max_extra_suffix", value: String(maxExtraSuffix)))
88+
}
89+
7990
if let filterBy = searchParameters.filterBy {
8091
searchQueryParams.append(URLQueryItem(name: "filter_by", value: filterBy))
8192
}
@@ -108,6 +119,14 @@ public struct Documents {
108119
searchQueryParams.append(URLQueryItem(name: "per_page", value: String(perPage)))
109120
}
110121

122+
if let limit = searchParameters.limit {
123+
searchQueryParams.append(URLQueryItem(name: "limit", value: String(limit)))
124+
}
125+
126+
if let offset = searchParameters.offset {
127+
searchQueryParams.append(URLQueryItem(name: "offset", value: String(offset)))
128+
}
129+
111130
if let groupBy = searchParameters.groupBy {
112131
searchQueryParams.append(URLQueryItem(name: "group_by", value: groupBy))
113132
}
@@ -140,6 +159,10 @@ public struct Documents {
140159
searchQueryParams.append(URLQueryItem(name: "highlight_end_tag", value: highlightEndTag))
141160
}
142161

162+
if let enableHighlightV1 = searchParameters.enableHighlightV1 {
163+
searchQueryParams.append(URLQueryItem(name: "enable_highlight_v1", value: String(enableHighlightV1)))
164+
}
165+
143166
if let snippetThreshold = searchParameters.snippetThreshold {
144167
searchQueryParams.append(URLQueryItem(name: "snippet_threshold", value: String(snippetThreshold)))
145168
}
@@ -164,10 +187,18 @@ public struct Documents {
164187
searchQueryParams.append(URLQueryItem(name: "highlight_fields", value: highlightFields))
165188
}
166189

190+
if let splitJoinTokens = searchParameters.splitJoinTokens {
191+
searchQueryParams.append(URLQueryItem(name: "split_join_tokens", value: splitJoinTokens))
192+
}
193+
167194
if let preSegmentedQuery = searchParameters.preSegmentedQuery {
168195
searchQueryParams.append(URLQueryItem(name: "pre_segmented_query", value: String(preSegmentedQuery)))
169196
}
170197

198+
if let preset = searchParameters.preset {
199+
searchQueryParams.append(URLQueryItem(name: "preset", value: preset))
200+
}
201+
171202
if let enableOverrides = searchParameters.enableOverrides {
172203
searchQueryParams.append(URLQueryItem(name: "enable_overrides", value: String(enableOverrides)))
173204
}
@@ -176,6 +207,14 @@ public struct Documents {
176207
searchQueryParams.append(URLQueryItem(name: "prioritize_exact_match", value: String(prioritizeExactMatch)))
177208
}
178209

210+
if let maxCandidates = searchParameters.maxCandidates {
211+
searchQueryParams.append(URLQueryItem(name: "max_candidates", value: String(maxCandidates)))
212+
}
213+
214+
if let prioritizeTokenPosition = searchParameters.prioritizeTokenPosition {
215+
searchQueryParams.append(URLQueryItem(name: "prioritize_token_position", value: String(prioritizeTokenPosition)))
216+
}
217+
179218
if let exhaustiveSearch = searchParameters.exhaustiveSearch {
180219
searchQueryParams.append(URLQueryItem(name: "exhaustive_search", value: String(exhaustiveSearch)))
181220
}
@@ -200,6 +239,17 @@ public struct Documents {
200239
searchQueryParams.append(URLQueryItem(name: "min_len2type", value: String(minLen2typo)))
201240
}
202241

242+
if let vectorQuery = searchParameters.vectorQuery {
243+
searchQueryParams.append(URLQueryItem(name: "vector_query", value: vectorQuery))
244+
}
245+
246+
if let remoteEmbeddingTimeoutMS = searchParameters.remoteEmbeddingTimeoutMs {
247+
searchQueryParams.append(URLQueryItem(name: "remote_embedding_timeout_ms", value: String(remoteEmbeddingTimeoutMS)))
248+
}
249+
250+
if let remoteEmbeddingNumTries = searchParameters.remoteEmbeddingNumTries {
251+
searchQueryParams.append(URLQueryItem(name: "remote_embedding_num_tries", value: String(remoteEmbeddingNumTries)))
252+
}
203253

204254
let (data, response) = try await apiCall.get(endPoint: "\(RESOURCEPATH)/search", queryParameters: searchQueryParams)
205255

Sources/Typesense/Errors.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ public enum ResponseError: Error {
2121
case invalidCollection(desc: String)
2222
case apiKeyNotFound(desc: String)
2323
case aliasNotFound(desc: String)
24+
case analyticsRuleDoesNotExist(desc: String)
2425
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// AnalyticsRuleParameters.swift
3+
//
4+
// Generated by swagger-codegen
5+
// https://github.com/swagger-api/swagger-codegen
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
public struct AnalyticsRuleParameters: Codable {
13+
14+
public var source: AnalyticsRuleParametersSource
15+
public var destination: AnalyticsRuleParametersDestination
16+
public var limit: Int
17+
18+
public init(source: AnalyticsRuleParametersSource, destination: AnalyticsRuleParametersDestination, limit: Int) {
19+
self.source = source
20+
self.destination = destination
21+
self.limit = limit
22+
}
23+
24+
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// AnalyticsRuleParametersDestination.swift
3+
//
4+
// Generated by swagger-codegen
5+
// https://github.com/swagger-api/swagger-codegen
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
public struct AnalyticsRuleParametersDestination: Codable {
13+
14+
public var collection: String?
15+
16+
public init(collection: String? = nil) {
17+
self.collection = collection
18+
}
19+
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// AnalyticsRuleParametersSource.swift
3+
//
4+
// Generated by swagger-codegen
5+
// https://github.com/swagger-api/swagger-codegen
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
public struct AnalyticsRuleParametersSource: Codable {
13+
14+
public var collections: [String]?
15+
16+
public init(collections: [String]? = nil) {
17+
self.collections = collections
18+
}
19+
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// AnalyticsRuleSchema.swift
3+
//
4+
// Generated by swagger-codegen
5+
// https://github.com/swagger-api/swagger-codegen
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
public struct AnalyticsRuleSchema: Codable {
13+
14+
public var name: String
15+
public var type: String?
16+
public var params: AnalyticsRuleParameters?
17+
18+
public init(name: String, type: String, params: AnalyticsRuleParameters) {
19+
self.name = name
20+
self.type = type
21+
self.params = params
22+
}
23+
24+
25+
}

0 commit comments

Comments
 (0)