Apic communicates with RESTful services, parses the JSON HTTP response and delivers objects.
pod 'Apic' '~> 2.2.4'
pod 'Apic' '~> 3.9.6'
pod 'Apic' '> 4.0.0'
pod 'Apic' '> 5.0.0'
The repository is a class that implements the logic to communicate with the services that provide the REST resources, basically a Repository has methods that correspond to endpoints in the backend, all repositories inherit from the generic class AbstractRepository
:
class GistsRepository: AbstractRepository {
func requestGistsOfUser(_ user: String, completion: ([Gist]) -> Void) -> Request<[Gist]> {
return requestArray(route: .get("https://api.github.com/users/\(user)/gists"), completion: completion)
}
}
The only requirement is that Gist
implements Decodable
This repository can now be used by a ViewController
to request a user's gists.
class GistsController: UITableViewController {
let gistsRepository = GistsRepository()
var gists: [Gist]?
var gistsRequest: Request<[Gist]>?
override func viewDidLoad() {
super.viewDidLoad()
requestGists()
}
func requestGists() {
gistsRequest?.cancel()
gistsRequest = gistsRepository.requestGistsOfUser("UserName", completion: { [weak self] gists in
self?.gists = gists
}).fail { [weak self] error in
self?.handleError(error)
}.finished { [weak self] in
self?.tableView.reloadData()
}
}
}
When the GistsRepository
finishes calling the service and parsing the response it calls the completion closure that you provide sending you an array of [Gist]
in this case.