Created
January 9, 2020 03:21
-
-
Save alcidesjunior/2f098527ee4258fdd9ba758b32402e9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getAll(_ completion: @escaping (Result<Movie, Error>)->Void){ | |
let url = "\(self.moviesUrl)\(EndPoints.apiKey.rawValue)" | |
Just.get(url){ (result) in | |
guard let data = result.content else {return} | |
do{ | |
let decoder = JSONDecoder() | |
switch result.statusCode{ | |
case 401: | |
let errorDecoded = try decoder.decode(GenericErrors.self, from: data) | |
completion(.failure(CustomError(errorDecoded.statusMessage))) | |
case 404: | |
let errorDecoded = try decoder.decode(GenericErrors.self, from: data) | |
completion(.failure(CustomError(errorDecoded.statusMessage))) | |
case 200: | |
let moviesDecoded = try decoder.decode(Movie.self, from: data) | |
completion(.success(moviesDecoded)) | |
case .none: | |
break | |
case .some(_): | |
break | |
} | |
}catch{ | |
completion(.failure(CustomError("An unexpected error happened, please try again later"))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment