Skip to content

Instantly share code, notes, and snippets.

@zafarivaev
Created January 14, 2020 19:20
Show Gist options
  • Save zafarivaev/0902904f334093a76555b1c7335545bc to your computer and use it in GitHub Desktop.
Save zafarivaev/0902904f334093a76555b1c7335545bc to your computer and use it in GitHub Desktop.
class APIClient {
.....
func get<T: BaseMappable>(urlString: String, parameters: [String: Any] = [:], success: @escaping (Int, T) -> (), failure: @escaping (String) -> ()) {
var parameters = parameters
parameters["key"] = APIManager.shared.apiKey
guard let url = NSURL(string: urlString , relativeTo: self.baseURL as URL?) else {
return
}
let urlString = url.absoluteString!
_ = request(.get,
urlString,
parameters: parameters,
headers: nil)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON()
.observeOn(MainScheduler.instance)
.subscribe(onNext: { (response) in
let statusCode = response.response?.statusCode
let model = Mapper<T>().map(JSON: response.result.value as! [String : Any])
success(statusCode!, model!)
}, onError: { (error) in
failure(error.localizedDescription)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment