提交 950eda96 编写于 作者: M Mattt Thompson

Rebasing xcode-6.3 from master.

上级 2f39b863
......@@ -35,6 +35,8 @@ Alamofire is an HTTP networking library written in Swift, from the [creator](htt
> **Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.**
>
> To use Alamofire with a project targeting iOS 7, you must include the `Alamofire.swift` source file directly in your project. See the ['Source File'](#source-file) section for instructions.
>
> For Swift 1.2 using the Xcode 6.3 Beta, use the [xcode-6.3 branch](https://github.com/Alamofire/Alamofire/tree/xcode-6.3).
### CocoaPods
......@@ -626,49 +628,59 @@ extension Alamofire.Request {
### URLStringConvertible
Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to construct URL requests. Top-level convenience methods taking a `URLStringConvertible` argument are provided to allow for type-safe routing behavior.
Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to construct URL requests. `NSString`, `NSURL`, `NSURLComponents`, and `NSURLRequest` conform to `URLStringConvertible` by default, allowing any of them to be passed as `URLString` parameters to the `request`, `upload`, and `download` methods:
```swift
let string = NSString(string: "http://httpbin.org/post")
Alamofire.request(.POST, string)
let URL = NSURL(string: string)!
Alamofire.request(.POST, URL)
let URLRequest = NSURLRequest(URL: URL)
Alamofire.request(.POST, URLRequest) // overrides `HTTPMethod` of `URLRequest`
Applications interacting with web applications in a significant manner are encouraged to adopt either `URLStringConvertible` or `URLRequestConvertible` as a way to ensure consistency of requested endpoints.
let URLComponents = NSURLComponents(URL: URL, resolvingAgainstBaseURL: true)
Alamofire.request(.POST, URLComponents)
```
Applications interacting with web applications in a significant manner are encouraged to have custom types conform to `URLStringConvertible` as a convenient way to map domain-specific models to server resources.
#### Type-Safe Routing
```swift
enum Router: URLStringConvertible {
extension User: URLStringConvertible {
static let baseURLString = "http://example.com"
case Root
case User(String)
case Post(Int, Int, String)
// MARK: URLStringConvertible
var URLString: String {
let path: String = {
switch self {
case .Root:
return "/"
case .User(let username):
return "/users/\(username)"
case .Post(let year, let month, let title):
let slug = title.stringByReplacingOccurrencesOfString(" ", withString: "-").lowercaseString
return "/\(year)/\(month)/\(slug)"
}
}()
return Router.baseURLString + path
return User.baseURLString + "/users/\(username)/"
}
}
```
```swift
Alamofire.request(.GET, Router.User("mattt"))
let user = User(username: "mattt")
Alamofire.request(.GET, user) // http://example.com/users/mattt
```
### URLRequestConvertible
Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests. Like `URLStringConvertible`, this is recommended for applications with any significant interactions between client and server.
Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests. `NSURLRequest` conforms to `URLRequestConvertible` by default, allowing it to be passed into `request`, `upload`, and `download` methods directly (this is the recommended way to specify custom HTTP header fields or HTTP body for individual requests):
```swift
let URL = NSURL(string: "http://httpbin.org/post")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = "POST"
let parameters = ["foo": "bar"]
var JSONSerializationError: NSError? = nil
mutableURLRequest.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &JSONSerializationError)
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(mutableURLRequest)
```
Top-level and instance methods on `Manager` taking `URLRequestConvertible` arguments are provided as a way to provide type-safe routing. Such an approach can be used to abstract away server-side inconsistencies, as well as manage authentication credentials and other state.
Applications interacting with web applications in a significant manner are encouraged to have custom types conform to `URLRequestConvertible` as a way to ensure consistency of requested endpoints. Such an approach can be used to abstract away server-side inconsistencies and provide type-safe routing, as well as manage authentication credentials and other state.
#### API Parameter Abstraction
......
......@@ -1009,6 +1009,21 @@ extension Manager {
public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
return upload(.File(URLRequest.URLRequest, file))
}
/**
Creates a request for uploading a file to the specified URL request.
If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
:param: method The HTTP method.
:param: URLString The URL string.
:param: file The file to upload
:returns: The created upload request.
*/
public func upload(method: Method, _ URLString: URLStringConvertible, file: NSURL) -> Request {
return upload(URLRequest(method, URLString), file: file)
}
// MARK: Data
......@@ -1025,6 +1040,21 @@ extension Manager {
public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
return upload(.Data(URLRequest.URLRequest, data))
}
/**
Creates a request for uploading data to the specified URL request.
If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
:param: method The HTTP method.
:param: URLString The URL string.
:param: data The data to upload
:returns: The created upload request.
*/
public func upload(method: Method, _ URLString: URLStringConvertible, data: NSData) -> Request {
return upload(URLRequest(method, URLString), data: data)
}
// MARK: Stream
......@@ -1041,6 +1071,21 @@ extension Manager {
public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
return upload(.Stream(URLRequest.URLRequest, stream))
}
/**
Creates a request for uploading a stream to the specified URL request.
If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
:param: method The HTTP method.
:param: URLString The URL string.
:param: stream The stream to upload.
:returns: The created upload request.
*/
public func upload(method: Method, _ URLString: URLStringConvertible, stream: NSInputStream) -> Request {
return upload(URLRequest(method, URLString), stream: stream)
}
}
extension Request {
......@@ -1094,6 +1139,19 @@ extension Manager {
// MARK: Request
/**
Creates a download request using the shared manager instance for the specified method and URL string.
:param: method The HTTP method.
:param: URLString The URL string.
:param: destination The closure used to determine the destination of the downloaded file.
:returns: The created download request.
*/
public func download(method: Method, _ URLString: URLStringConvertible, destination: Request.DownloadFileDestination) -> Request {
return download(URLRequest(method, URLString), destination: destination)
}
/**
Creates a request for downloading from the specified URL request.
......@@ -1442,7 +1500,7 @@ private func URLRequest(method: Method, URL: URLStringConvertible) -> NSURLReque
:returns: The created request.
*/
public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request {
return request(encoding.encode(URLRequest(method, URLString), parameters: parameters).0)
return Manager.sharedInstance.request(method, URLString, parameters: parameters, encoding: encoding)
}
/**
......@@ -1472,7 +1530,7 @@ public func request(URLRequest: URLRequestConvertible) -> Request {
:returns: The created upload request.
*/
public func upload(method: Method, URLString: URLStringConvertible, file: NSURL) -> Request {
return Manager.sharedInstance.upload(URLRequest(method, URLString), file: file)
return Manager.sharedInstance.upload(method, URLString, file: file)
}
/**
......@@ -1499,7 +1557,7 @@ public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
:returns: The created upload request.
*/
public func upload(method: Method, URLString: URLStringConvertible, data: NSData) -> Request {
return Manager.sharedInstance.upload(URLRequest(method, URLString), data: data)
return Manager.sharedInstance.upload(method, URLString, data: data)
}
/**
......@@ -1526,7 +1584,7 @@ public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
:returns: The created upload request.
*/
public func upload(method: Method, URLString: URLStringConvertible, stream: NSInputStream) -> Request {
return Manager.sharedInstance.upload(URLRequest(method, URLString), stream: stream)
return Manager.sharedInstance.upload(method, URLString, stream: stream)
}
/**
......@@ -1555,7 +1613,7 @@ public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) ->
:returns: The created download request.
*/
public func download(method: Method, URLString: URLStringConvertible, destination: Request.DownloadFileDestination) -> Request {
return Manager.sharedInstance.download(URLRequest(method, URLString), destination: destination)
return Manager.sharedInstance.download(method, URLString, destination: destination)
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册