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
urlmay be supplied if theBMSClientclass 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
urlThe resource URL.
methodThe HTTP method.
headersOptional headers to add to the request.
queryParametersOptional query parameters to add to the request.
timeoutTimeout in seconds for this request.
cachePolicyCache policy to use when sending request.
autoRetriesThe 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
Responseobject which is passed back via the supplied completion handler.If the
resourceUrlstring is a malformed url or if thequeryParameterscannot 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
requestBodyThe HTTP request body.
completionHandlerThe block that will be called when this request finishes.
View on GitHub
BaseRequest Class Reference