Created
February 18, 2017 18:21
-
-
Save sammoore/1b48d6badd7f64da3352a61e55484d55 to your computer and use it in GitHub Desktop.
This file contains 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
class APIManager: Alamofire.Manager { | |
private let baseURL: String | |
private let outboundEncoding: Alamofire.ParameterEncoding | |
init(baseURL: Alamofire.URLStringConvertible, outboundEncoding: Alamofire.ParameterEncoding = .URL) { | |
self.baseURL = baseURL.URLString | |
} | |
private func URLStringWithPath(path: String) -> String { | |
return (baseURL as NSString).stringByAppendingPathComponent(path) | |
} | |
func request( | |
method: Alamofire.Method, | |
path: String, | |
parameters: [String : AnyObject]? = nil, | |
headers: [String : String]? = nil) | |
-> Alamofire.Request | |
{ | |
super.request(method, URLStringWithPath(path), parameters: parameters, encoding: outboundEncoding, headers: headers) | |
} | |
// This method is slower on APIManager as it checks to see if URLString contains a protocol separator "://", incase incorrectly used. | |
override func request( | |
method: Alamofire.Method, | |
_ URLString: Alamofire.URLStringConvertible, | |
parameters: [String : AnyObject]? = nil, | |
encoding: Alamofire.ParameterEncoding? = nil, | |
headers: [String : String]? = nil) | |
-> Alamofire.Request | |
{ | |
var path = URLString.URLString | |
if !(path ~= "://") { | |
path = URLStringWithPath(path) | |
} | |
super.request(method, path, parameters: parameters, encoding: encoding ?? outboundEncoding, headers: headers) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment