Created
June 25, 2018 23:58
-
-
Save aybekckaya/63dd0083ef873f811d3a6fd990b1c227 to your computer and use it in GitHub Desktop.
Sample Networking with RxSwift and Alamofire
This file contains 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
typealias NetworkResponse = (data:JSON? , error:NetworkError?) | |
enum NetworkError { | |
case userNotFound | |
case cannotGetUserId | |
case cannotGetServerResponse | |
case unknownError | |
func stringify()->String { | |
switch self { | |
case .userNotFound: | |
return "User not found !!!" | |
case .cannotGetUserId: | |
return "Ooops.. I could not get the userId. There maybe an error at Flickr API" | |
case .cannotGetServerResponse: | |
return "Server is not responding. Try again later." | |
case .unknownError: | |
return "There is an error occured. You may call the programmer of this app via phone. His number is 0535 373 33 57" | |
default : return "" | |
} | |
} | |
} | |
enum Endpoint { | |
case search(username:String) | |
case photos(userId:String) | |
case userDetailedInfo(userId:String) | |
case recentPhotos | |
// case userFullInfo(userId:String) | |
} | |
extension Endpoint { | |
fileprivate func requestType()->HTTPMethod { | |
switch self { | |
case .search(_): return HTTPMethod.get | |
case .photos(_) : return HTTPMethod.get | |
case .userDetailedInfo(_): return HTTPMethod.get | |
case .recentPhotos : return HTTPMethod.get | |
} | |
} | |
} | |
extension Endpoint { | |
fileprivate func endpoint()->String { | |
switch self { | |
case .search(let username): | |
return Configuration.baseURL+"flickr.urls.lookupUser&api_key="+Configuration.apiKey+"&url=http://www.flickr.com/photos/"+username+"&format=json&nojsoncallback=1" | |
case .photos(let userId): | |
return Configuration.baseURL+"flickr.people.getPhotos&api_key="+Configuration.apiKey+"&user_id="+userId.replacingOccurrences(of: "@", with: "%40")+"&per_page="+Configuration.imagesPerPage+"&format=json&nojsoncallback=1" | |
case .userDetailedInfo(let userId): | |
// https://api.flickr.com/services/rest/?method=flickr.people.getInfo&api_key=e632db2f6334e3f98a2a3f0e9ac3b33b&user_id=56851472%40N00&format=json&nojsoncallback=1 | |
return Configuration.baseURL+"flickr.people.getInfo&api_key="+Configuration.apiKey+"&user_id="+userId.replacingOccurrences(of: "@", with: "%40")+"&format=json&nojsoncallback=1" | |
case .recentPhotos: | |
// https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=e632db2f6334e3f98a2a3f0e9ac3b33b&extras=owner_name%2C+icon_server&per_page=20&format=json&nojsoncallback=1 | |
// https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=e632db2f6334e3f98a2a3f0e9ac3b33b&extras=++owner_name%2C+icon_server%2C+url_o+%2C+url_m&per_page=20&format=json&nojsoncallback=1 | |
return Configuration.baseURL+"flickr.photos.getRecent&api_key="+Configuration.apiKey+"&extras=owner_name%2C+icon_server%2C+url_o+%2C+url_m&per_page="+Configuration.imagesPerPage+"&format=json&nojsoncallback=1" | |
} | |
} | |
} | |
extension Endpoint { | |
fileprivate func parameters()->[String:Any]? { | |
return nil | |
} | |
} | |
extension Endpoint { | |
func request()->Observable<NetworkResponse> { | |
return Observable.create({ observable in | |
Alamofire.request(self.endpoint(), method: self.requestType(), parameters: self.parameters(), encoding: URLEncoding.default, headers: nil).responseJSON(completionHandler: { response in | |
if let error = response.error { | |
observable.onNext(self.handleError(error: error)) | |
return | |
} | |
let jsonDct = JSON(response.result.value) | |
observable.onNext((data: jsonDct, error: nil)) | |
}) | |
return Disposables.create() | |
}) | |
} | |
private func handleError(error:Error)->NetworkResponse { | |
return NetworkResponse(data:nil , error:NetworkError.unknownError) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment