BaseRequest
open class BaseRequest: NSObject, URLSessionTaskDelegate
Sends HTTP network requests.
BaseRequest
is a simpler alternative to BMSURLSession
that requires no familiarity with Swift’s URLSession API.
When building a BaseRequest object, all components of the HTTP request must be provided in the initializer, except for the requestBody
, which can be supplied as Data when sending the request via send(requestBody:completionHandler:)
.
Important
It is recommended to use theRequest
class instead of BaseRequest
, since it will replace BaseRequest
in the future.
-
URL that the request is being sent to.
Declaration
Swift
public private(set) var resourceUrl: String
-
The HTTP method (GET, POST, etc.).
Declaration
Swift
public let httpMethod: HttpMethod
-
Request timeout measured in seconds.
Declaration
Swift
public var timeout: Double
-
All request headers.
Declaration
Swift
public var headers: [String: String] = [:]
-
The query parameters to append to the
resourceURL
.Declaration
Swift
public var queryParameters: [String: String]?
-
The request body is set when sending the request via
send(requestBody:completionHandler:)
.Declaration
Swift
public private(set) var requestBody: Data?
-
Determines whether request should follow HTTP redirects.
Declaration
Swift
public var allowRedirects : Bool = true
-
Deterimes the cache policy to use for sending request.
Declaration
Swift
public var cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
-
Creates a new request.
Note
A relative
url
may be supplied if theBMSClient
class is initialized with a Bluemix app route beforehand.Declaration
Swift
public init(url: String, method: HttpMethod = HttpMethod.GET, headers: [String: String]? = nil, queryParameters: [String: String]? = nil, timeout: Double = BMSClient.sharedInstance.requestTimeout, cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy, autoRetries: Int = 0)
Parameters
url
The resource URL.
method
The HTTP method.
headers
Optional headers to add to the request.
queryParameters
Optional query parameters to add to the request.
timeout
Timeout in seconds for this request.
cachePolicy
Cache policy to use when sending request.
autoRetries
The number of times to retry each request if it fails to send. The conditions for retries are: request timeout, loss of network connectivity, failure to connect to the host, and 504 responses.
-
Send the request asynchronously with an optional request body.
The response received from the server is packaged into a
Response
object which is passed back via the supplied completion handler.If the
resourceUrl
string is a malformed url or if thequeryParameters
cannot be appended to it, the completion handler will be called back with an error and a nilResponse
.Declaration
Swift
public func send(requestBody: Data? = nil, completionHandler: BMSCompletionHandler?)
Parameters
requestBody
The HTTP request body.
completionHandler
The block that will be called when this request finishes.