|
| 1 | +// |
| 2 | +// RecommendClient.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Vladislav Fitc on 31/08/2021. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +#if canImport(FoundationNetworking) |
| 11 | +import FoundationNetworking |
| 12 | +#endif |
| 13 | + |
| 14 | +/// Client to perform recommend operations. |
| 15 | +public struct RecommendClient: Credentials { |
| 16 | + |
| 17 | + let transport: Transport |
| 18 | + let operationLauncher: OperationLauncher |
| 19 | + let configuration: Configuration |
| 20 | + |
| 21 | + public var applicationID: ApplicationID { |
| 22 | + return transport.applicationID |
| 23 | + } |
| 24 | + |
| 25 | + public var apiKey: APIKey { |
| 26 | + return transport.apiKey |
| 27 | + } |
| 28 | + |
| 29 | + public init(appID: ApplicationID, apiKey: APIKey) { |
| 30 | + |
| 31 | + let configuration = SearchConfiguration(applicationID: appID, apiKey: apiKey) |
| 32 | + |
| 33 | + let sessionConfiguration: URLSessionConfiguration = .default |
| 34 | + sessionConfiguration.httpAdditionalHeaders = configuration.defaultHeaders |
| 35 | + let session = URLSession(configuration: sessionConfiguration) |
| 36 | + |
| 37 | + self.init(configuration: configuration, requester: session) |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + public init(configuration: SearchConfiguration, |
| 42 | + requester: HTTPRequester = URLSession(configuration: .default)) { |
| 43 | + |
| 44 | + let queue = OperationQueue() |
| 45 | + queue.qualityOfService = .userInitiated |
| 46 | + let operationLauncher = OperationLauncher(queue: queue) |
| 47 | + |
| 48 | + let retryStrategy = AlgoliaRetryStrategy(configuration: configuration) |
| 49 | + |
| 50 | + let httpTransport = HTTPTransport(requester: requester, |
| 51 | + configuration: configuration, |
| 52 | + retryStrategy: retryStrategy, |
| 53 | + credentials: configuration, |
| 54 | + operationLauncher: operationLauncher) |
| 55 | + self.init(transport: httpTransport, operationLauncher: operationLauncher, configuration: configuration) |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + init(transport: Transport, |
| 60 | + operationLauncher: OperationLauncher, |
| 61 | + configuration: Configuration) { |
| 62 | + self.transport = transport |
| 63 | + self.operationLauncher = operationLauncher |
| 64 | + self.configuration = configuration |
| 65 | + } |
| 66 | + |
| 67 | + /// Initialize an Index configured with SearchConfiguration. |
| 68 | + public func index(withName indexName: IndexName) -> Index { |
| 69 | + return Index(name: indexName, transport: transport, operationLauncher: operationLauncher, configuration: configuration) |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +extension RecommendClient: TransportContainer {} |
| 75 | + |
| 76 | +extension RecommendClient { |
| 77 | + |
| 78 | + @discardableResult func launch<O: Operation>(_ operation: O) -> O { |
| 79 | + return operationLauncher.launch(operation) |
| 80 | + } |
| 81 | + |
| 82 | + func launch<O: OperationWithResult>(_ operation: O) throws -> O.ResultValue { |
| 83 | + return try operationLauncher.launchSync(operation) |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +public extension RecommendClient { |
| 89 | + |
| 90 | + /** |
| 91 | + Returns recommendations. |
| 92 | + |
| 93 | + - parameter options: Recommend request options |
| 94 | + - parameter requestOptions: Configure request locally with RequestOptions |
| 95 | + - parameter completion: Result completion |
| 96 | + - returns: Launched asynchronous operation |
| 97 | + */ |
| 98 | + @discardableResult func getRecommendations(options: [RecommendationsOptions], |
| 99 | + requestOptions: RequestOptions? = nil, |
| 100 | + completion: @escaping ResultCallback<SearchesResponse>) -> Operation { |
| 101 | + let command = Command.Recommend.GetRecommendations(options: options, requestOptions: requestOptions) |
| 102 | + return execute(command, completion: completion) |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + Returns recommendations. |
| 107 | + |
| 108 | + - parameter options: Recommend request options |
| 109 | + - parameter requestOptions: Configure request locally with RequestOptions |
| 110 | + - returns: SearchesResponse object |
| 111 | + */ |
| 112 | + @discardableResult func getRecommendations(options: [RecommendationsOptions], |
| 113 | + requestOptions: RequestOptions? = nil) throws -> SearchesResponse { |
| 114 | + let command = Command.Recommend.GetRecommendations(options: options, requestOptions: requestOptions) |
| 115 | + return try execute(command) |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + Returns [Related Products](https://algolia.com/doc/guides/algolia-ai/recommend/#related-products). |
| 120 | + |
| 121 | + - parameter options: Recommend request options |
| 122 | + - parameter requestOptions: Configure request locally with RequestOptions |
| 123 | + - parameter completion: Result completion |
| 124 | + - returns: Launched asynchronous operation |
| 125 | + */ |
| 126 | + @discardableResult func getRelatedProducts(options: [RelatedProductsOptions], |
| 127 | + requestOptions: RequestOptions? = nil, |
| 128 | + completion: @escaping ResultCallback<SearchesResponse>) -> Operation { |
| 129 | + return getRecommendations(options: options.map(\.recommendationsOptions), |
| 130 | + requestOptions: requestOptions, |
| 131 | + completion: completion) |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + Returns [Related Products](https://algolia.com/doc/guides/algolia-ai/recommend/#related-products). |
| 136 | + |
| 137 | + - parameter options: Recommend request options |
| 138 | + - parameter requestOptions: Configure request locally with RequestOptions |
| 139 | + - returns: SearchesResponse object |
| 140 | + */ |
| 141 | + @discardableResult func getRelatedProducts(options: [RelatedProductsOptions], |
| 142 | + requestOptions: RequestOptions? = nil) throws -> SearchesResponse { |
| 143 | + return try getRecommendations(options: options.map(\.recommendationsOptions), |
| 144 | + requestOptions: requestOptions) |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + Returns [Frequently Bought Together](https://algolia.com/doc/guides/algolia-ai/recommend/#frequently-bought-together) products. |
| 149 | + |
| 150 | + - parameter options: Recommend request options |
| 151 | + - parameter requestOptions: Configure request locally with RequestOptions |
| 152 | + - parameter completion: Result completion |
| 153 | + - returns: Launched asynchronous operation |
| 154 | + */ |
| 155 | + @discardableResult func getFrequentlyBoughtTogether(options: [FrequentlyBoughtTogetherOptions], |
| 156 | + requestOptions: RequestOptions? = nil, |
| 157 | + completion: @escaping ResultCallback<SearchesResponse>) -> Operation { |
| 158 | + return getRecommendations(options: options.map(\.recommendationsOptions), |
| 159 | + requestOptions: requestOptions, |
| 160 | + completion: completion) |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + Returns [Frequently Bought Together](https://algolia.com/doc/guides/algolia-ai/recommend/#frequently-bought-together) products. |
| 165 | + |
| 166 | + - parameter options: Recommend request options |
| 167 | + - parameter requestOptions: Configure request locally with RequestOptions |
| 168 | + - returns: SearchesResponse object |
| 169 | + */ |
| 170 | + @discardableResult func getFrequentlyBoughtTogether(options: [FrequentlyBoughtTogetherOptions], |
| 171 | + requestOptions: RequestOptions? = nil) throws -> SearchesResponse { |
| 172 | + return try getRecommendations(options: options.map(\.recommendationsOptions), |
| 173 | + requestOptions: requestOptions) |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments