APIClient
open class APIClient: NSObject, URLSessionDataDelegate
An APIClient communicates to the Stack Exchange API over HTTP.
-
The URLSession for this client.
Declaration
Swift
open var session: URLSession!
-
The queue used for asynchronous operations.
Declaration
Swift
open let queue = DispatchQueue(label: "API queue", attributes: [.concurrent])
-
The API key to use.
Declaration
Swift
open var key: String?
-
Which API filter to use if none is specified.
Declaration
Swift
open var defaultFilter: String?
-
Whether to use a secure connection to communicate with the API.
Declaration
Swift
open var useSSL: Bool = true
-
Which site to use if none is specified.
Declaration
Swift
open var defaultSite: String = "stackoverflow"
-
The API quota remaining, or
nil
if it is not known.Declaration
Swift
open private(set) var quota: Int?
-
The maximum API quota, or
nil
if it is not known.Declaration
Swift
open private(set) var maxQuota: Int?
-
The methods which have
Declaration
Swift
open var backoffs = [String:Date]()
-
Errors that can occur while performing a HTTP request.
See moreDeclaration
Swift
public enum RequestError: Error
-
API-specific errors.
See moreDeclaration
Swift
public enum APIError: Error
-
Undocumented
See moreDeclaration
Swift
open class APIClient: NSObject, URLSessionDataDelegate
-
Performs an API request. All functions using API calls should funnel into this one.
Declaration
Swift
open func performAPIRequest<T>( _ request: String, parameters: [String:String] = [:], backoffBehavior: BackoffBehavior = .wait ) throws -> APIResponse<T>
Parameters
request
The request to make, for example
users/{ids}/answers
.parameters
Parameters to be URLEncoded into the request.
-
Performs an URLRequest.
Warning
On Linux, this function will not work to perform a POST request.Declaration
Swift
open func performRequest(_ request: URLRequest) throws -> (Data, HTTPURLResponse)
Parameters
request
The request to perform.
Return Value
The data and response returned by the request.
-
Performs a GET request to the specified URL.
Declaration
Swift
open func get(_ url: String) throws -> (Data, HTTPURLResponse)
Parameters
url
The URL to send the request to.
Return Value
The data and response returned by the request.
-
Performs a POST request to the specifed URL.
parameters:
- url: The URL to send the request to.
- fields: The data to POST.
Declaration
Swift
open func post(_ url: String, fields: [String:String]) throws -> (Data, HTTPURLResponse)
Parameters
url
The URL to send the request to.
fields
The data to POST.
Return Value
The data and response returned by the request.
-
Performs an URLRequest.
Warning
On Linux, this function will not work to perform a POST request.Declaration
Swift
open func performRequest(_ request: URLRequest) throws -> String
Parameters
request
The request to perform.
Return Value
The text returned by the request.
-
Performs a GET request to the specified URL.
Declaration
Swift
open func get(_ url: String) throws -> String
Parameters
url
The URL to send the request to.
Return Value
The text returned by the request.
-
Performs a POST request to the specifed URL.
parameters:
- url: The URL to send the request to.
- fields: The data to POST.
Declaration
Swift
open func post(_ url: String, fields: [String:String]) throws -> String
Parameters
url
The URL to send the request to.
fields
The data to POST.
Return Value
The text returned by the request.
-
Parses JSON using JSONSerialization. - parameter json: The JSON to parse. - returns: The parsed JSON.
Declaration
Swift
open func parseJSON(_ json: String) throws -> Any
Parameters
json
The JSON to parse.
Return Value
The parsed JSON.
-
Initializes an APIClient with an optional proxy. - parameter proxyAddress: The address of the proxy to use, or
nil
for no proxy. Default isnil
. - parameter proxyPort: The port on the proxy server to connect to.Declaration
Swift
public init(proxyAddress: String? = nil, proxyPort: Int = 80)
Parameters
proxyAddress
The address of the proxy to use, or
nil
for no proxy. Default isnil
.proxyPort
The port on the proxy server to connect to.
-
Undocumented
Declaration
Swift
open class APIClient: NSObject, URLSessionDataDelegate
-
Undocumented
Declaration
Swift
open class APIClient: NSObject, URLSessionDataDelegate
-
Undocumented
Declaration
Swift
open class APIClient: NSObject, URLSessionDataDelegate
-
Undocumented
Declaration
Swift
open class APIClient: NSObject, URLSessionDataDelegate
-
Fetches questions synchronously.
Declaration
Swift
public func fetchQuestions( _ ids: [Int], parameters: [String:String] = [:], backoffBehavior: BackoffBehavior = .wait) throws -> APIResponse<Question>
Parameters
ids
The question IDs to fetch.
parameters
The dictionary of parameters used in the request
backoffBehavior
The behavior when an APIRequest has a backoff
Return Value
The list of sites as
APIResponse<Question>
-
Fetches questions asynchronously.
Declaration
Swift
public func fetchQuestions( _ ids: [Int], parameters: [String: String] = [:], backoffBehavior: BackoffBehavior = .wait, completionHandler: @escaping (APIResponse<Question>?, Error?) -> ())
Parameters
ids
The question IDs to fetch.
parameters
The dictionary of parameters used in the request
backoffBehavior
The behavior when an APIRequest has a backoff
completion
-
Fetches a question synchronously.
Declaration
Swift
public func fetchQuestion( _ id: Int, parameters: [String:String] = [:], backoffBehavior: BackoffBehavior = .wait) throws -> APIResponse<Question>
Parameters
ids
The question IDs to fetch.
parameters
The dictionary of parameters used in the request
backoffBehavior
The behavior when an APIRequest has a backoff
Return Value
The list of sites as
APIResponse<Question>
-
Fetches a question asynchronously.
Declaration
Swift
public func fetchQuestion( _ id: Int, parameters: [String: String] = [:], backoffBehavior: BackoffBehavior = .wait, completionHandler: @escaping (APIResponse<Question>?, Error?) -> ())
Parameters
ids
The question IDs to fetch.
parameters
The dictionary of parameters used in the request
backoffBehavior
The behavior when an APIRequest has a backoff
completion
-
Fetches all
Sites
in the Stack Exchange network synchronously.Declaration
Swift
public func fetchSites( parameters: [String:String] = [:], backoffBehavior: BackoffBehavior = .wait) throws -> APIResponse<Site>
Parameters
parameters
The dictionary of parameters used in the request
backoffBehavior
The behavior when an APIRequest has a backoff
Return Value
The list of sites as
APIResponse<Site>
-
Fetches all
Sites
in the Stack Exchange network asynchronously.Declaration
Swift
public func fetchSites(parameters: [String: String] = [:], backoffBehavior: BackoffBehavior = .wait, completionHandler: @escaping (APIResponse<Site>?, Error?) -> ())
Parameters
parameters
The dictionary of parameters used in the request
backoffBehavior
The behavior when an APIRequest has a backoff