Skip to content

Commit

Permalink
Added Comment for Thenable Object
Browse files Browse the repository at this point in the history
  • Loading branch information
hainayanda committed Oct 28, 2020
1 parent 5d72ba4 commit 42adebc
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
14 changes: 14 additions & 0 deletions iONess/Classes/Thenable/DataRequestPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@

import Foundation

/// Thenable HTTPRequest for get response data
open class DataRequestPromise<Response: URLResponse>: HTTPRequestPromise<Response, URLResult> {

/// Default Init
/// - Parameters:
/// - request: HTTPRequestMessage object which describe the request
/// - networkSessionManager: NetworkSessionManager
/// - retryControl: RetryControl object
/// - Throws: Error when generating URLRequest from HTTPRequestMessage
public override init(request: HTTPRequestMessage, with networkSessionManager: NetworkSessionManager, retryControl: RetryControl?) throws {
try super.init(request: request, with: networkSessionManager, retryControl: retryControl)
}

/// Method to run closure after the request is finished.
/// - Parameter closure: closure which will be run when the request is finished
/// - Returns: DropableURLRequest<Response> object
@discardableResult
open override func then(run closure: @escaping (URLResult) -> Void) -> DropableURLRequest<Response> {
let dispatcher = self.dispatcher
Expand Down
11 changes: 11 additions & 0 deletions iONess/Classes/Thenable/DownloadRequestPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@

import Foundation

/// Thenable HTTPRequest for downloading data
open class DownloadRequestPromise: HTTPRequestPromise<URLResponse, DownloadResult> {
var targetUrl: URL

/// Default Init
/// - Parameters:
/// - request: HTTPRequestMessage object which describe the request
/// - networkSessionManager: NetworkSessionManager object
/// - retryControl: RetryControl object
/// - targetUrl: local URL file which data will be downloaded
/// - Throws: Error when generating URLRequest from HTTPRequestMessage
public init(request: HTTPRequestMessage, with networkSessionManager: NetworkSessionManager, retryControl: RetryControl?, targetUrl: URL) throws {
self.targetUrl = targetUrl
try super.init(request: request, with: networkSessionManager, retryControl: retryControl)
}

/// Method to run closure after the request is finished.
/// - Parameter closure: closure which will be run when the request is finished
/// - Returns: DropableURLRequest<Response> object
@discardableResult
open override func then(run closure: @escaping (DownloadResult) -> Void) -> DropableURLRequest<Response> {
let dispatcher = self.dispatcher
Expand Down
19 changes: 19 additions & 0 deletions iONess/Classes/Thenable/Thenable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,40 @@

import Foundation

/// Thenable protocol
/// This protocol are designed to run the thenable closure after doing some task which produced Result and Dropable object
public protocol Thenable {
associatedtype Result
associatedtype DropablePromise: Dropable

/// Method to run task and then running closures passed
/// - Parameters:
/// - closure: closure which will be run when task succeed
/// - failClosure: closure which will be run when task fail
@discardableResult
func then(run closure: @escaping (Result) -> Void, whenFailed failClosure: @escaping (Result) -> Void) -> DropablePromise

/// Method to run task and then running closure passed
/// - Parameter closure: closure which will be run when task finished
@discardableResult
func then(run closure: @escaping (Result) -> Void) -> DropablePromise

/// Method to run task and then running closures passed
/// - Parameters:
/// - closure: closure which will be run when task succeed
/// - failClosure: closure which will be run when task fail
/// - deferClosure: closure which will be run after closure or failClosure
@discardableResult
func then(run closure: @escaping (Result) -> Void, whenFailed failClosure: @escaping (Result) -> Void, finally deferClosure: @escaping (Result) -> Void) -> DropablePromise
}

public extension Thenable {
/// Method to run task and then running closures passed
/// - Parameters:
/// - closure: closure which will be run when task succeed
/// - failClosure: closure which will be run when task fail
/// - deferClosure: closure which will be run after closure or failClosure
/// - Returns: DropablePromise object
@discardableResult
func then(run closure: @escaping (Result) -> Void, whenFailed failClosure: @escaping (Result) -> Void, finally deferClosure: @escaping (Result) -> Void) -> DropablePromise {
then(
Expand Down
24 changes: 23 additions & 1 deletion iONess/Classes/Thenable/URLRequestPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,38 @@

import Foundation

/// Base class for Thenable URLRequest
open class URLRequestPromise<Response: URLResponse, Result: NetworkResult>: URLThenableRequest {
/// URLValidator which will validate result from URL Request
public var urlValidator: URLValidator?
/// DispatchQueue which will run the completion closure
public var dispatcher: DispatchQueue = .main

/// Default init
public init() { }

/// Method to run closure after the request is finished. It should be overriden, otherwise it will throw fatalError
/// - Parameter closure: closure which will be run when the request is finished
/// - Returns: DropableURLRequest<Response> object
@discardableResult
open func then(run closure: @escaping (Result) -> Void) -> DropableURLRequest<Response> {
fatalError("iONess Error: method did not overridden 'then(run closure: @escaping (URLResult) -> Void) -> DropableURLRequest<Response>'")
}
}

/// Thenable URLRequest which should be used as Thenable for URLRequest which already failed when prepared
open class ErrorRequestPromise<Response: URLResponse, Result: NetworkResult>: URLRequestPromise<Response, Result> {
var error: Error

init(error: Error) {
/// Default Init
/// - Parameter error: Error on prepared
public init(error: Error) {
self.error = error
}

/// Method to run closure after the request is finished. It should automatically fail
/// - Parameter closure: Closure which will fail
/// - Returns: FailedURLRequest object
@discardableResult
open override func then(run closure: @escaping (Result) -> Void) -> DropableURLRequest<Response> {
let error = self.error
Expand All @@ -35,11 +50,18 @@ open class ErrorRequestPromise<Response: URLResponse, Result: NetworkResult>: UR

}

/// Thenable HTTPRequest
open class HTTPRequestPromise<Response: URLResponse, Result: NetworkResult>: URLRequestPromise<Response, Result> {
var urlRequest: URLRequest
var networkSessionManager: NetworkSessionManager
var retryControl: RetryControl?

/// Default Init
/// - Parameters:
/// - request: HTTPRequestMessage object which describe the request
/// - networkSessionManager: NetworkSessionManager
/// - retryControl: RetryControl object
/// - Throws: Error when generating URLRequest from HTTPRequestMessage
public init(request: HTTPRequestMessage, with networkSessionManager: NetworkSessionManager, retryControl: RetryControl?) throws {
let mutableRequest = try request.getFullUrl().asMutableRequest()
for header in request.headers {
Expand Down
29 changes: 29 additions & 0 deletions iONess/Classes/Thenable/URLThenableRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@

import Foundation

/// Thenable URL Request protocol
public protocol URLThenableRequest: Thenable where DropablePromise: DropableURLRequest<Response>, Result: NetworkResult {
associatedtype Response: URLResponse
/// URLValidator which will validate result from URL Request
var urlValidator: URLValidator? { get set }
/// DispatchQueue which will run the completion closure
var dispatcher: DispatchQueue { get set }
/// Method to execute and ignore any result
@discardableResult
func executeAndForget() -> DropableURLRequest<Response>
}

public extension URLThenableRequest {

/// Method to execute request and then run any closure passed
/// - Parameters:
/// - closure: Closure which will run when request succeed
/// - failClosure: Closure which will run when request fail
/// - Returns: DroppableURLRequest object
@discardableResult
func then(run closure: @escaping (Result) -> Void, whenFailed failClosure: @escaping (Result) -> Void) -> DropableURLRequest<Response> {
return then { result in
Expand All @@ -28,6 +37,8 @@ public extension URLThenableRequest {
}
}

/// Method to execute and ignore any result
/// - Returns: DroppableURLRequest object
@discardableResult
func executeAndForget() -> DropableURLRequest<Response> {
then { _ in }
Expand All @@ -36,28 +47,43 @@ public extension URLThenableRequest {

public extension URLThenableRequest {

/// Method to set DispatchQueue where the completion will run
/// - Parameter dispatcher: DispatchQueue object
/// - Returns: URLThenableRequest which have custom DispatchQueue
@discardableResult
func completionDispatch(on dispatcher: DispatchQueue) -> Self {
var requestWithDispatch = self
requestWithDispatch.dispatcher = dispatcher
return requestWithDispatch
}

/// Method to add URLValidator which validate using status code
/// - Parameter statusCode: valid status code
/// - Returns: URLThenableRequest which have status code URLValidator combined with previous validator if have any
@discardableResult
func validate(statusCode: Int) -> Self {
return validate(statusCodes: statusCode..<statusCode + 1)
}

/// Method to add URLValidator which validate using status codes
/// - Parameter statusCodes: valid status codes
/// - Returns: URLThenableRequest which have status codes URLValidator combined with previous validator if have any
@discardableResult
func validate(statusCodes: Range<Int>) -> Self {
validate(using: StatusCodeValidator(statusCodes))
}

/// Method to add URLValidator which validate result headers
/// - Parameter headers: valid result headers
/// - Returns: URLThenableRequest which have headers URLValidator combined with previous validator if have any
@discardableResult
func validate(shouldHaveHeaders headers: [String:String]) -> Self {
validate(using: HeaderValidator(headers))
}

/// Method to add custom URLValidator
/// - Parameter validator: custom URLValidator
/// - Returns: URLThenableRequest which have custom URLValidator combined with previous validator if have any
@discardableResult
func validate(using validator: URLValidator) -> Self {
var requestWithValidation = self
Expand All @@ -71,6 +97,9 @@ public extension URLThenableRequest {
}

public extension URLThenableRequest {
/// Method to aggregate request with another request
/// - Parameter request: other request to aggregate
/// - Returns: RequestAggregator object
func aggregate(with request: Self) -> RequestAggregator<Self> {
RequestAggregator(requests: [self, request])
}
Expand Down
17 changes: 14 additions & 3 deletions iONess/Classes/Thenable/UploadRequestPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@

import Foundation

/// Thenable HTTPRequest for uploading data
open class UploadRequestPromise: HTTPRequestPromise<URLResponse, URLResult> {
var fileURL: URL

/// Default init
/// - Parameters:
/// - request: HTTPRequestMessage object which describe the request
/// - networkSessionManager: NetworkSessionManager object
/// - retryControl: RetryControl object
/// - fileURL: local URL file which will be uploaded
/// - Throws: Error when generating URLRequest from HTTPRequestMessage
public init(request: HTTPRequestMessage, with networkSessionManager: NetworkSessionManager, retryControl: RetryControl?, fileURL: URL) throws {
self.fileURL = fileURL
try super.init(request: request, with: networkSessionManager, retryControl: retryControl)
}

/// Method to run closure after the request is finished.
/// - Parameter closure: closure which will be run when the request is finished
/// - Returns: DropableURLRequest<Response> object
@discardableResult
open override func then(run closure: @escaping (URLResult) -> Void) -> DropableURLRequest<Response> {
let dispatcher = self.dispatcher
Expand All @@ -24,9 +35,9 @@ open class UploadRequestPromise: HTTPRequestPromise<URLResponse, URLResult> {
fileUrl: fileURL,
retryControl: retryControl,
urlValidator: urlValidator) { result in
dispatcher.async {
closure(result)
}
dispatcher.async {
closure(result)
}
}
}
}

0 comments on commit 42adebc

Please sign in to comment.