Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Execution
- extension RestCall {
- /**
- This method execute and return the task back, developper can use the task ref to cancel it, pause it, resume it back...
- - Remark: The result back to the user on main thread
- - Parameter session: **URLSession** the connection that will create the task that will be performed
- - Parameter completion: **Result** The result of the execution an enum <success, fail>.
- - Returns: The URLSessionDataTask to be executed
- */
- @discardableResult /// supress warning in unused result ...
- func execute(_ session :URLSession, completion: @escaping (Result<T,GenericError>) -> Void) -> URLSessionDataTask? {
- /// it all about the url !
- /// the map that will guide data through it's journey
- if isMalformed {
- /// Result.failure(error) => return nil
- }
- /// build the request with suitable params
- do {
- var request = try RequestBuilder.build(self)
- /// in case the call require interception of the header for additional config u can use a CallInterceptor
- self.callInterceptor.intercept(&request)
- /// create dataTask using a session object (might be custom)
- /// prepare the mission
- /// dear data, ride this task and go to the back end, ask him for data exchange, u can do it. blessed, see u soon.
- let task = session.dataTask(with: request, completionHandler: { data, response, error in
- /** handle response
- it might be a : Result.failure(error) => return nil
- or : Result.success(T)
- */
- })
- /// maybe u need (task ref) in some cases
- /// Open the pipe & Start The mission
- /// see u soon request
- /// u can do it ;)
- task.resume()
- return task
- }
- catch (let error as GenericError){
- /// Result.failure(error)
- return nil
- }
- catch{
- /// Result.failure(error)
- return nil
- }
- }
- /**
- This method mock Rest Call from json files
- - Remark: The result back to the user on main thread
- - Parameter fileName: **String** the file that contains mock data
- - Parameter completion: **Result** The result of the execution an enum <success, fail>.
- */
- func mock(_ fileName : String, completion: @escaping (Result<T,GenericError>) -> Void){
- if let path = Bundle.main.path(forResource: fileName, ofType: "json") {
- do {
- let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
- do {
- /** handle response
- it might be a : Result.failure(error)
- or : Result.success(T)
- */
- }
- } catch {
- /// File content Cant be seen as Data
- /// Result.failure(error)
- }
- }else {
- /// File Not Found
- /// Result.failure(error)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement