Algent is a type safe algolia wrapper library.
You can use algolia with Swift decodable type as a search result.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Open AppDelegate.swift
and set your AlgoliaSearch AppID and APIKey.
Algent.initialize(appID: "YOUR_APP_ID", apiKey: "YOUR_API_KEY")
Add search result model with implementing decodable
class Diary: Decodable {
let title: String
let text: String
// Algolia hash tag info
_tags: [String]
}
Add search request class with implementing AlgoliaRequestProtocol
struct SearchDiaryRequest: AlgoliaRequestProtocol {
// set search result type
typealias HitType = Diary
let page: Int
let per: Int
let text: String?
let hashtags: [String]?
var indexName: String {
return "diaries"
}
var query: AlgentQuery {
let query = AlgentQuery(query: text)
query.page = UInt(page)
query.hitsPerPage = UInt(per)
if let hashtags = hashtags {
query.tagFilters = hashtags
}
return query
}
init(page: Int, per: Int, text: String? = nil, hashtags: [String]? = nil) {
self.page = page
self.per = per
self.text = text
self.hashtags = hashtags
}
}
Call Algent search method using your request.
let request = SarchDiaryRequest(page: 0, per: 20, text: "hello world", hashtags: ["trip"])
Algent.shared.search(request: request) { result in
switch result {
case .success(let response):
// response is AlgoliaResponse<Request.HitType>
print(response.hits) // see hit object
case .failure( let error):
print(error) // get error
}
}
Algent is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Algent'
miup, [email protected]
Algent is available under the MIT license. See the LICENSE file for more info.