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])
  • key

    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 more

    Declaration

    Swift

    public enum RequestError: Error
  • API-specific errors.

    See more

    Declaration

    Swift

    public enum APIError: Error
  • Undocumented

    See more

    Declaration

    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 is nil. - 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 is nil.

    proxyPort

    The port on the proxy server to connect to.