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 NetworkResource or NetworkResourceWithBody Modifiers to inform callers of errors that fail the request, for example, JSON encoding failures.

    If this not nil at the time the load method 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

    url

    URL

    method

    httpMethod

  • Initializes the NetworkResource with the given NSMutableURLRequest

    Declaration

    Swift

    public init(request: NSMutableURLRequest, networkInterface: WebServiceNetworkInterface? = nil)

    Parameters

    request

    NSMutableURLRequest

    networkInterface

    WebServiceNetworkInterface

  • Initializes the NetworkResource with the given URLRequest

    Declaration

    Swift

    public convenience init(request: URLRequest)

    Parameters

    request

    URLRequest.

  • 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

    completion

    completion 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

    successBlock

    block to be executed when response doesn’t have any errors.

    failureBlock

    block 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

    readingOptions

    JSONSerialization.ReadingOptions, empty by default.

    successBlock

    block to be executed when response doesn’t have any errors.

    failureBlock

    block 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 Decodable type, 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

    decodable

    The type to decode the response JSON Data into.

    decoder

    The JSONDecoder to use, defaults to JSONDecoder()

    successBlock

    block to be executed when response doesn’t have any errors, and is successfully parsed into the given Decodable type.

    failureBlock

    block to be executed when response has an error.

  • Adds the given credentials as a Basic HTTP Hidden Authorization Header into to the resource.

    The username and password are base64 encoded before being set into the Authorization header of request.

    Declaration

    Swift

    @discardableResult
    @objc
    func authorizationHeader(username: String, password: String) -> NetworkResource

    Parameters

    user

    The username

    password

    The 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?) -> NetworkResource

    Parameters

    key

    header name

    value

    header 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]) -> NetworkResource

    Parameters

    dictionary

    Dictionary 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]) -> NetworkResource

    Parameters

    dictionary

    Dictionary 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") -> NetworkResource

    Parameters

    withFile

    The name (without extension) of the file containing the mocked response. The file must be present in the main bundle (Bundle.main)

    ofType

    The extension of the file. Defaults to .json if not provided.

    Return Value

    NetworkResource

  • Sets whether the request should wait for Constraints or not. false by 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) -> NetworkResource

    Parameters

    contentType

    Content-Type

    Return Value

    NetworkResource