Skip to content

Instantly share code, notes, and snippets.

@justinmakaila
Created February 6, 2015 16:32
Show Gist options
  • Save justinmakaila/21d3daf6829972fa4582 to your computer and use it in GitHub Desktop.
Save justinmakaila/21d3daf6829972fa4582 to your computer and use it in GitHub Desktop.
Alamofire API Route Protocol
/**
An API Route
*/
protocol APIRoute {
var method: Alamofire.Method { get }
var encoding: Alamofire.ParameterEncoding { get }
var path: String { get }
var parameters: [String: AnyObject]? { get }
var requestConvertible: APIRequestConvertible { get }
}
/**
URL Request Convertible for use by Alamofire
*/
struct APIRequestConvertible: URLRequestConvertible {
// TODO: This should be changed
static var baseUrl: NSURL! = NSURL(string: "http://www.yourUrlHere.com")!
let route: APIRoute
var URLRequest: NSURLRequest {
let requestUrl = APIRequestConvertible.baseUrl.URLByAppendingPathComponent(route.path)
// Create a request with `requestUrl`, returning cached data if available, with a 15 second timeout.
let request = NSMutableURLRequest(URL: requestUrl, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 15)
request.HTTPMethod = route.method.rawValue
return route.encoding.encode(request, parameters: route.parameters).0
}
init(route: APIRoute) {
self.route = route
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment