Skip to content

Commit

Permalink
Merge pull request #30 from willtempleton/update/v0-25
Browse files Browse the repository at this point in the history
Updates for Typesense v0.25
  • Loading branch information
jasonbosco authored Feb 6, 2024
2 parents 955c0ed + 199765b commit b28f8a6
Show file tree
Hide file tree
Showing 39 changed files with 922 additions and 56 deletions.
24 changes: 24 additions & 0 deletions Sources/Typesense/Analytics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

public struct Analytics {
static let resourcePath: String = "/analytics"

private var analyticsRules: AnalyticsRules
var apiCall: ApiCall

init(config: Configuration) {
self.apiCall = ApiCall(config: config)
self.analyticsRules = AnalyticsRules(apiCall: apiCall)
}

func rule(id: String) -> AnalyticsRule {
return AnalyticsRule(name: id, apiCall: self.apiCall)
}

func rules() -> AnalyticsRules {
return AnalyticsRules(apiCall: self.apiCall)
}
}



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

public struct AnalyticsRule {
var name: String
private var apiCall: ApiCall
init(name: String, apiCall: ApiCall) {
self.name = name
self.apiCall = apiCall
}

public func retrieve() async throws -> (AnalyticsRuleSchema?, URLResponse?) {
let (data, response) = try await self.apiCall.get(endPoint: endpointPath())
if let result = data {
if let notFound = try? decoder.decode(ApiResponse.self, from: result) {
throw ResponseError.analyticsRuleDoesNotExist(desc: notFound.message)
}
let fetchedRule = try decoder.decode(AnalyticsRuleSchema.self, from: result)
return (fetchedRule, response)
}
return (nil, response)
}

public func delete() async throws -> (AnalyticsRuleSchema?, URLResponse?) {
let (data, response) = try await self.apiCall.delete(endPoint: endpointPath())
if let result = data {
if let notFound = try? decoder.decode(ApiResponse.self, from: result) {
throw ResponseError.analyticsRuleDoesNotExist(desc: notFound.message)
}
let deletedRule = try decoder.decode(AnalyticsRuleSchema.self, from: result)
return (deletedRule, response)
}
return (nil, response)
}

private func endpointPath() -> String {
return "\(AnalyticsRules.resourcePath)/\(name)"
}
}
39 changes: 39 additions & 0 deletions Sources/Typesense/AnalyticsRules.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

public struct AnalyticsRules {
private var apiCall: ApiCall
static var resourcePath: String = "\(Analytics.resourcePath)/rules"

init(apiCall: ApiCall) {
self.apiCall = apiCall
}

func upsert(params: AnalyticsRuleSchema) async throws -> (AnalyticsRuleSchema?, URLResponse?) {
let ruleData = try encoder.encode(params)
let (data, response) = try await self.apiCall.put(endPoint: endpointPath(params.name), body: ruleData)
if let result = data {
let ruleResult = try decoder.decode(AnalyticsRuleSchema.self, from: result)
return (ruleResult, response)
}

return (nil, response)
}

func retrieveAll() async throws -> (AnalyticsRulesRetrieveSchema?, URLResponse?) {
let (data, response) = try await self.apiCall.get(endPoint: endpointPath())
if let result = data {
let rules = try decoder.decode(AnalyticsRulesRetrieveSchema.self, from: result)
return (rules, response)
}

return (nil, response)
}

private func endpointPath(_ operation: String? = nil) -> String {
if let operation = operation {
return "\(AnalyticsRules.resourcePath)/\(operation)"
} else {
return AnalyticsRules.resourcePath
}
}
}
4 changes: 4 additions & 0 deletions Sources/Typesense/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public struct Client {
public func multiSearch() -> MultiSearch {
return MultiSearch(config: self.configuration)
}

public func analytics() -> Analytics {
return Analytics(config: self.configuration)
}
}
56 changes: 53 additions & 3 deletions Sources/Typesense/Documents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public struct Documents {
searchQueryParams.append(URLQueryItem(name: "query_by_weights", value: queryByWeights))
}

if let maxHits = searchParameters.maxHits {
searchQueryParams.append(URLQueryItem(name: "max_hits", value: maxHits))
if let textMatchType = searchParameters.textMatchType {
searchQueryParams.append(URLQueryItem(name: "text_match_type", value: textMatchType))
}

if let _prefix = searchParameters._prefix {
Expand All @@ -72,10 +72,21 @@ public struct Documents {
fullString.append(String(item))
fullString.append(",")
}

searchQueryParams.append(URLQueryItem(name: "prefix", value: String(fullString.dropLast())))
}

if let _infix = searchParameters._infix {
searchQueryParams.append(URLQueryItem(name: "infix", value: _infix))
}

if let maxExtraPrefix = searchParameters.maxExtraPrefix {
searchQueryParams.append(URLQueryItem(name: "max_extra_prefix", value: String(maxExtraPrefix)))
}

if let maxExtraSuffix = searchParameters.maxExtraSuffix {
searchQueryParams.append(URLQueryItem(name: "max_extra_suffix", value: String(maxExtraSuffix)))
}

if let filterBy = searchParameters.filterBy {
searchQueryParams.append(URLQueryItem(name: "filter_by", value: filterBy))
}
Expand Down Expand Up @@ -108,6 +119,14 @@ public struct Documents {
searchQueryParams.append(URLQueryItem(name: "per_page", value: String(perPage)))
}

if let limit = searchParameters.limit {
searchQueryParams.append(URLQueryItem(name: "limit", value: String(limit)))
}

if let offset = searchParameters.offset {
searchQueryParams.append(URLQueryItem(name: "offset", value: String(offset)))
}

if let groupBy = searchParameters.groupBy {
searchQueryParams.append(URLQueryItem(name: "group_by", value: groupBy))
}
Expand Down Expand Up @@ -140,6 +159,10 @@ public struct Documents {
searchQueryParams.append(URLQueryItem(name: "highlight_end_tag", value: highlightEndTag))
}

if let enableHighlightV1 = searchParameters.enableHighlightV1 {
searchQueryParams.append(URLQueryItem(name: "enable_highlight_v1", value: String(enableHighlightV1)))
}

if let snippetThreshold = searchParameters.snippetThreshold {
searchQueryParams.append(URLQueryItem(name: "snippet_threshold", value: String(snippetThreshold)))
}
Expand All @@ -164,10 +187,18 @@ public struct Documents {
searchQueryParams.append(URLQueryItem(name: "highlight_fields", value: highlightFields))
}

if let splitJoinTokens = searchParameters.splitJoinTokens {
searchQueryParams.append(URLQueryItem(name: "split_join_tokens", value: splitJoinTokens))
}

if let preSegmentedQuery = searchParameters.preSegmentedQuery {
searchQueryParams.append(URLQueryItem(name: "pre_segmented_query", value: String(preSegmentedQuery)))
}

if let preset = searchParameters.preset {
searchQueryParams.append(URLQueryItem(name: "preset", value: preset))
}

if let enableOverrides = searchParameters.enableOverrides {
searchQueryParams.append(URLQueryItem(name: "enable_overrides", value: String(enableOverrides)))
}
Expand All @@ -176,6 +207,14 @@ public struct Documents {
searchQueryParams.append(URLQueryItem(name: "prioritize_exact_match", value: String(prioritizeExactMatch)))
}

if let maxCandidates = searchParameters.maxCandidates {
searchQueryParams.append(URLQueryItem(name: "max_candidates", value: String(maxCandidates)))
}

if let prioritizeTokenPosition = searchParameters.prioritizeTokenPosition {
searchQueryParams.append(URLQueryItem(name: "prioritize_token_position", value: String(prioritizeTokenPosition)))
}

if let exhaustiveSearch = searchParameters.exhaustiveSearch {
searchQueryParams.append(URLQueryItem(name: "exhaustive_search", value: String(exhaustiveSearch)))
}
Expand All @@ -200,6 +239,17 @@ public struct Documents {
searchQueryParams.append(URLQueryItem(name: "min_len2type", value: String(minLen2typo)))
}

if let vectorQuery = searchParameters.vectorQuery {
searchQueryParams.append(URLQueryItem(name: "vector_query", value: vectorQuery))
}

if let remoteEmbeddingTimeoutMS = searchParameters.remoteEmbeddingTimeoutMs {
searchQueryParams.append(URLQueryItem(name: "remote_embedding_timeout_ms", value: String(remoteEmbeddingTimeoutMS)))
}

if let remoteEmbeddingNumTries = searchParameters.remoteEmbeddingNumTries {
searchQueryParams.append(URLQueryItem(name: "remote_embedding_num_tries", value: String(remoteEmbeddingNumTries)))
}

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

Expand Down
1 change: 1 addition & 0 deletions Sources/Typesense/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public enum ResponseError: Error {
case invalidCollection(desc: String)
case apiKeyNotFound(desc: String)
case aliasNotFound(desc: String)
case analyticsRuleDoesNotExist(desc: String)
}
25 changes: 25 additions & 0 deletions Sources/Typesense/Models/AnalyticsRuleParameters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// AnalyticsRuleParameters.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct AnalyticsRuleParameters: Codable {

public var source: AnalyticsRuleParametersSource
public var destination: AnalyticsRuleParametersDestination
public var limit: Int

public init(source: AnalyticsRuleParametersSource, destination: AnalyticsRuleParametersDestination, limit: Int) {
self.source = source
self.destination = destination
self.limit = limit
}


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

import Foundation



public struct AnalyticsRuleParametersDestination: Codable {

public var collection: String?

public init(collection: String? = nil) {
self.collection = collection
}


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

import Foundation



public struct AnalyticsRuleParametersSource: Codable {

public var collections: [String]?

public init(collections: [String]? = nil) {
self.collections = collections
}


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

import Foundation



public struct AnalyticsRuleSchema: Codable {

public var name: String
public var type: String?
public var params: AnalyticsRuleParameters?

public init(name: String, type: String, params: AnalyticsRuleParameters) {
self.name = name
self.type = type
self.params = params
}


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

import Foundation



public struct AnalyticsRulesRetrieveSchema: Codable {

public var rules: [AnalyticsRuleSchema]?

public init(rules: [AnalyticsRuleSchema]? = nil) {
self.rules = rules
}


}
12 changes: 6 additions & 6 deletions Sources/Typesense/Models/ApiKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ import Foundation

public struct ApiKey: Codable {

public var _description: String?
public var value: String?
public var _description: String
public var actions: [String]
public var collections: [String]
public var expiresAt: Int64?
public var _id: Int64
public var value: String?
public var _id: Int64?
public var valuePrefix: String?

public init(_description: String? = nil, actions: [String], collections: [String], expiresAt: Int64? = nil, _id: Int64, value: String? = nil, valuePrefix: String? = nil) {
public init(value: String? = nil, _description: String, actions: [String], collections: [String], expiresAt: Int64? = nil, _id: Int64? = nil, valuePrefix: String? = nil) {
self.value = value
self._description = _description
self.actions = actions
self.collections = collections
self.expiresAt = expiresAt
self._id = _id
self.value = value
self.valuePrefix = valuePrefix
}

public enum CodingKeys: String, CodingKey {
case value
case _description = "description"
case actions
case collections
case expiresAt = "expires_at"
case _id = "id"
case value
case valuePrefix = "value_prefix"
}

Expand Down
Loading

0 comments on commit b28f8a6

Please sign in to comment.