SHARE
TWEET

Untitled

a guest May 24th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Execution
  2. extension RestCall {
  3.    
  4.     /**
  5.      This method execute and return the task back, developper can use the task ref to cancel it, pause it, resume it back...
  6.      
  7.      - Remark: The result back to the user on main thread
  8.      - Parameter session: **URLSession** the connection that will create the task that will be performed
  9.      - Parameter completion: **Result** The result of the execution an enum <success, fail>.
  10.      - Returns: The URLSessionDataTask to be executed
  11.      */
  12.     @discardableResult /// supress warning in unused result ...
  13.     func execute(_ session :URLSession, completion: @escaping (Result<T,GenericError>) -> Void) -> URLSessionDataTask? {
  14.        
  15.         /// it all about the url !
  16.         /// the map that will guide data through it's journey
  17.         if isMalformed {
  18.            /// Result.failure(error) => return nil
  19.         }
  20.        
  21.         /// build the request with suitable params
  22.         do {
  23.             var request = try RequestBuilder.build(self)
  24.            
  25.            
  26.             /// in case the call require interception of the header for additional config u can use a CallInterceptor
  27.             self.callInterceptor.intercept(&request)
  28.            
  29.             /// create dataTask using a session object (might be custom)
  30.             /// prepare the mission
  31.             /// dear data, ride this task and go to the back end, ask him for data exchange, u can do it. blessed, see u soon.
  32.             let task = session.dataTask(with: request, completionHandler: { data, response, error in
  33.                 /** handle response
  34.                 it might be a : Result.failure(error) => return nil
  35.                 or : Result.success(T)
  36.                 */
  37.                
  38.             })
  39.            
  40.             /// maybe u need (task ref) in some cases
  41.             /// Open the pipe & Start The mission
  42.             /// see u soon request
  43.             /// u can do it ;)
  44.             task.resume()
  45.            
  46.             return task
  47.         }
  48.         catch (let error as GenericError){
  49.             /// Result.failure(error)
  50.             return nil
  51.         }
  52.         catch{
  53.             /// Result.failure(error)
  54.             return nil
  55.         }
  56.     }
  57.    
  58.     /**
  59.      This method mock Rest Call from json files
  60.      
  61.      - Remark: The result back to the user on main thread
  62.      - Parameter fileName: **String** the file that contains mock data
  63.      - Parameter completion: **Result** The result of the execution an enum <success, fail>.
  64.      */
  65.     func mock(_ fileName : String, completion: @escaping (Result<T,GenericError>) -> Void){
  66.         if let path = Bundle.main.path(forResource: fileName, ofType: "json") {
  67.             do {
  68.                 let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
  69.                 do {
  70.                     /** handle response
  71.                     it might be a : Result.failure(error)
  72.                     or : Result.success(T)
  73.                     */
  74.                 }
  75.             } catch {
  76.                 /// File content Cant be seen as Data
  77.                 /// Result.failure(error)
  78.             }
  79.         }else {
  80.             /// File Not Found
  81.             /// Result.failure(error)
  82.         }
  83.     }
  84.    
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top