NetworkResource
public class NetworkResource : NSObject
A wrapper over NSMutableURLRequest, it’s an alias for a network request in Swifty.
It provides the chaining modifier syntax and also stores attributes and directions for Swifty to run the given network request.
-
The actual NSMutableURLRequest this resource wraps over
Declaration
Swift
public let request: NSMutableURLRequest -
Tags allow you to categorize your requests, which helps you to recognize them in your interceptors and constraints to take selective action
Declaration
Swift
public var tags: Set<String> -
Error (if any) encountered while Webservice was creating this request.
Set this error in your own extensions of
NetworkResourceorNetworkResourceWithBodyModifiers to inform callers of errors that fail the request, for example, JSON encoding failures.If this not
nilat the time theloadmethod on this request is called, the request will automatically fail with this error without ever hitting the network.Declaration
Swift
public var creationError: NSError?
-
Initializes the NetworkResource with the given URL, and HTTP Method.
Declaration
Swift
public convenience init(url: URL, method: String)Parameters
urlURL
methodhttpMethod
-
Initializes the NetworkResource with the given NSMutableURLRequest
Declaration
Swift
public init(request: NSMutableURLRequest, networkInterface: WebServiceNetworkInterface? = nil)Parameters
requestNSMutableURLRequest
networkInterfaceWebServiceNetworkInterface
-
Initializes the NetworkResource with the given URLRequest
Declaration
Swift
public convenience init(request: URLRequest)Parameters
requestURLRequest.
-
The resource’s parameters in readable format, including the URL, Headers, Method, and the HTTP Body
Declaration
Swift
public override var description: String { get } -
Prints the resource’s parameters in readable format, including the URL, Headers, Method, and the HTTP Body
Declaration
Swift
@discardableResult public func printDetails() -> NetworkResource
-
Loads the network resource, and calls the completion block with an unserialized NetworkResponse
Declaration
Swift
@objc public func load(completion: @escaping (NetworkResponse) -> Void)Parameters
completioncompletion block to be executed when resource is successfully loaded.
-
Loads the network resource, and calls the success block with unserialized Data or the failure block with the error
Declaration
Swift
@objc public func load(successBlock: @escaping (_ responseObject: Data?) -> Void, failureBlock: @escaping (_ error: NSError) -> Void)Parameters
successBlockblock to be executed when response doesn’t have any errors.
failureBlockblock to be executed when response has an error.
-
Loads the network resource, and calls the successBlock with the parsed JSON, or the failure block with the error.
Declaration
Swift
@objc public func loadJSON(readingOptions: JSONSerialization.ReadingOptions = [], successBlock: @escaping (_ responseObject: Any?) -> Void, failureBlock: @escaping (_ error: NSError) -> Void)Parameters
readingOptionsJSONSerialization.ReadingOptions, empty by default.
successBlockblock to be executed when response doesn’t have any errors.
failureBlockblock to be executed when response has an error.
-
Loads the network resource, and calls the successBlock if the decoder was successfully able to convert the response JSON Data into the specified
Decodabletype, or the failure block with the error.Declaration
Swift
public func loadJSON<T: Decodable>(_ decodable: T.Type, decoder: JSONDecoder = JSONDecoder(), successBlock: @escaping (_ response: T) -> Void, failureBlock: @escaping (_ error: NSError) -> Void)Parameters
decodableThe type to decode the response JSON Data into.
decoderThe
JSONDecoderto use, defaults toJSONDecoder()successBlockblock to be executed when response doesn’t have any errors, and is successfully parsed into the given
Decodabletype.failureBlockblock to be executed when response has an error.
-
Adds the given credentials as a Basic HTTP Hidden Authorization Header into to the resource.
The
usernameandpasswordare base64 encoded before being set into theAuthorizationheader of request.Declaration
Swift
@discardableResult @objc func authorizationHeader(username: String, password: String) -> NetworkResourceParameters
userThe username
passwordThe password
Return Value
NetworkResource
-
Adds the given header to the resource, or updates it’s value if it already exists.
Declaration
Swift
@discardableResult @objc func header(key: String, value: String?) -> NetworkResourceParameters
keyheader name
valueheader value
Return Value
NetworkResource
-
Adds the given headers to the resource, and updates the value of the ones that already exist.
Declaration
Swift
@discardableResult @objc func headers(_ dictionary: [String : String]) -> NetworkResourceParameters
dictionaryDictionary of header elements.
Return Value
NetworkResource
-
Encodes the given dictionary into URL Allowed query parameters and adds them to the resource’s URL
Declaration
Swift
@discardableResult @objc func query(_ dictionary: [String : Any]) -> NetworkResourceParameters
dictionaryDictionary containing the query parameters
Return Value
NetworkResource
-
Mocks the response of the resource with the contents of the given filename. Note that if a request is mocked, it’ll never hit the network, and will NOT pass the Request Interceptors. It will, however, pass through the Response Intereptors.
Declaration
Swift
@discardableResult @objc func mock(withFile: String, ofType: String = "json") -> NetworkResourceParameters
withFileThe name (without extension) of the file containing the mocked response. The file must be present in the main bundle (
Bundle.main)ofTypeThe extension of the file. Defaults to
.jsonif not provided.Return Value
NetworkResource
-
Sets whether the request should wait for Constraints or not.
falseby default.If false, this request will not call any of the given Constraint’s methods, and will directly go the the Request Interceptors.
Declaration
Swift
@discardableResult @objc func canHaveConstraints(_ flag: Bool) -> NetworkResource -
Sets the priority for the resource to be passed on to URLSession while excuting, defaults to normal priority (0.5)
Declaration
Swift
@discardableResult func priority(_ priority: URLSessionTaskPriority) -> NetworkResource -
Adds the given tag to the resource
Declaration
Swift
@discardableResult @objc func tag(_ tag: String) -> NetworkResource -
Adds the given tags to the resource
Declaration
Swift
@discardableResult @objc func tags(_ tags: [String]) -> NetworkResource -
Sets the Queue on which the response should be delivered on. By default, every response is delivered on the main queue.
Declaration
Swift
@discardableResult @objc func deliverOn(thread: DispatchQueue) -> NetworkResource -
Checks whether the resource has the given tag
Declaration
Swift
@objc func hasTag(_ tag: String) -> Bool -
Sets the Content-Type header of the resource.
Declaration
Swift
@discardableResult @objc func contentType(_ contentType: String) -> NetworkResourceParameters
contentTypeContent-Type
Return Value
NetworkResource
View on GitHub
Install in Dash
NetworkResource Class Reference