2 Evolved during multiple client projects at you & the gang (Jobspotting, HelloFresh, Lufthansa Innovation Hub,…). Make development of API layers in apps: • Simple (use the same patterns in every app) • Fast (don’t rely on the actual API) • Fun (we love the Swift)
String? } API MockProtocol JSON File in App Bundle API.request(Router.GetCollection) {… Response Object Deserialization • MockProtocol works based on regular routers • API.mocker is optional (remove to go „live“) MockProtocol 5/5
get } var path: String { get } var encoding: ParameterEncoding { get } var baseURLString: String { get } var uploadData: (data: NSData, name: String, fileName: String, mimeType: String)? { get } } enum Router: RouterProtocol { case GetAuthToken(username: String, password: String) case GetOffice(officeName: String) case GetMember(officeName: String, memberPIN: String) … • Requests are build using RouterProtocol • We use an enum for all the routers • Parameters in the case • Switch on the router RouterProtocol 2/2
from routers, set by API.parameterMapper: public class ParameterMapper { public func parametersForRouter(router: RouterProtocol) -> [String : AnyObject] public func headersForRouter(router: RouterProtocol) -> [String : String] … API.parameterMapper = Mapper() ParameterMapper 2/4
has a pattern for this API TokenRefreshDelegate tokenRefreshIsIndicated(response) true tokenRefresh(completion) completion(success) TokenRefreshDelegate 2/4
• Operations are non-blocking, so still in parallel API.request(Router.GetCollection) {… performRouter() performRouter() performRouter() Serial Operation Queue enqueues non-blocking operation TokenRefreshDelegate 3/4
operation is enqueued • The failed request and all other failing requests are enqueued again to be performed after the refresh • Seamless for the calling code (just some little delay) performRouter() TokenRefreshDelegate.tokenRefresh() Serial Operation Queue API.tokenRefresh() TokenRefreshDelegate 4/4