Created
February 6, 2015 16:32
-
-
Save justinmakaila/21d3daf6829972fa4582 to your computer and use it in GitHub Desktop.
Alamofire API Route Protocol
This file contains hidden or 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
/** | |
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