AbstractClient
@objcMembers
public class AbstractClient : NSObject
An abstract API client.
Stateful hosts
In order to avoid hitting timeouts at every request when one or more hosts are not working properly (whatever the reason: DNS failure, no route to host, server down…), the client maintains a known status for each host. That status can be either up, down or unknown. Initially, all hosts are in the unknown state. Then a given host’s status is updated whenever a request to it returns a response or an error.
When a host is flagged as down, it will not be considered for subsequent requests. However, to avoid discarding
hosts permanently, statuses are only remembered for a given timeframe, indicated by hostStatusTimeout
. (You may
adjust it as needed, although the default value defaultHostStatusTimeout
should make sense for most applications.)
-
HTTP headers that will be sent with every request.
Declaration
Swift
@objc public var headers: [String : String]
-
The list of libraries used by this client.
Warning
Deprecated. Now a static property of theClient
class. The instance properties are just an alias for the static property.Declaration
Swift
@available(*, deprecated: 4.8) @objc public var userAgents: [LibraryVersion] { get }
-
The list of libraries used by instances of this class. They are passed in the
User-Agent
HTTP header of every request. It is initially set to contain only this library and the OS, but may be overridden to include other libraries.Warning
The user agent is crucial to proper statistics in your Algolia dashboard. Please leave it as is. This field is publicly exposed only for the sake of other Algolia libraries.Declaration
Swift
@objc public private(set) static var userAgents: [LibraryVersion] { get set }
-
Default timeout for network requests. Default: 30 seconds.
Declaration
Swift
@objc public var timeout: TimeInterval
-
Specific timeout for search requests. Default: 5 seconds.
Declaration
Swift
@objc public var searchTimeout: TimeInterval
-
Hosts for read queries, in priority order. The first host will always be used, then subsequent hosts in case of retry.
Warning
The default values should be appropriate for most use cases. Change them only if you know what you are doing.Declaration
Swift
@objc public var readHosts: [String] { get set }
-
Hosts for write queries, in priority order. The first host will always be used, then subsequent hosts in case of retry.
Warning
The default values should be appropriate for most use cases. Change them only if you know what you are doing.Declaration
Swift
@objc public var writeHosts: [String] { get set }
-
The timeout for host statuses.
Declaration
Swift
@objc public var hostStatusTimeout: TimeInterval
-
Operation queue used to run completion handlers. Default = main queue.
Note
This will affect completion handlers of all methods on all indices attached to this client.
Warning
The queue is not retained by the client. You must ensure that it remains valid for the lifetime of the client.
Declaration
Swift
@objc public weak var completionQueue: OperationQueue?
-
The default timeout for host statuses.
Declaration
Swift
@objc public static let defaultHostStatusTimeout: TimeInterval
-
Whether to use network reachability to decide if online requests should be attempted.
- When
true
(default), if the network reachability (as reported by the System Configuration framework) is down, online requests will not be attempted and report to fail immediately.When
false
, online requests will always be attempted (if the strategy involves them), even if the network does not seem to be reachable.Note
Not available on watchOS (the System Configuration framework is not available there).
Declaration
Swift
@objc public var useReachability: Bool
- When
-
Set read and write hosts to the same value (convenience method).
Warning
The default values should be appropriate for most use cases. Change them only if you know what you are doing.Declaration
Swift
@objc(setHosts:) public func setHosts(_ hosts: [String])
-
Set an HTTP header that will be sent with every request.
Note
You may also use the
headers
property directly.Declaration
Swift
@objc(setHeaderWithName:to:) public func setHeader(withName name: String, to value: String?)
Parameters
name
Header name.
value
Value for the header. If
nil
, the header will be removed. -
Get an HTTP header.
Note
You may also use the
headers
property directly.Declaration
Swift
@objc(headerWithName:) public func header(withName name: String) -> String?
Parameters
name
Header name.
Return Value
The header’s value, or
nil
if the header does not exist. -
Add a library version to the global list of user agents.
Note
It is safe to call this function multiple times. Adding an already existing library is a no-op.
Declaration
Swift
@objc public static func addUserAgent(_ libraryVersion: LibraryVersion)
Parameters
libraryVersion
Library version to add.
-
Ping the server. This method returns nothing except a message indicating that the server is alive.
Declaration
Swift
@discardableResult @objc(isAlive:) public func isAlive(completionHandler: @escaping CompletionHandler) -> Operation
Parameters
completionHandler
Completion handler to be notified of the request’s outcome.
Return Value
A cancellable operation.