Skip to content

Commit

Permalink
Merge branch 'feature/double_generic_results'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Alamofire.xcodeproj/project.pbxproj
  • Loading branch information
cnoon committed Sep 22, 2015
2 parents af231fd + 00a0130 commit 88de889
Show file tree
Hide file tree
Showing 18 changed files with 568 additions and 486 deletions.
8 changes: 8 additions & 0 deletions Alamofire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
/* Begin PBXBuildFile section */
4C0B58391B747A4400C0B99C /* ResponseSerializationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0B58381B747A4400C0B99C /* ResponseSerializationTests.swift */; };
4C0B583A1B747A4400C0B99C /* ResponseSerializationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0B58381B747A4400C0B99C /* ResponseSerializationTests.swift */; };
4C0B62511BB1001C009302D3 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0B62501BB1001C009302D3 /* Response.swift */; settings = {ASSET_TAGS = (); }; };
4C0B62521BB1001C009302D3 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0B62501BB1001C009302D3 /* Response.swift */; settings = {ASSET_TAGS = (); }; };
4C0B62531BB1001C009302D3 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0B62501BB1001C009302D3 /* Response.swift */; settings = {ASSET_TAGS = (); }; };
4C0E5BF81B673D3400816CCC /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0E5BF71B673D3400816CCC /* Result.swift */; };
4C0E5BF91B673D3400816CCC /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0E5BF71B673D3400816CCC /* Result.swift */; };
4C1DC8541B68908E00476DE3 /* Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C1DC8531B68908E00476DE3 /* Error.swift */; };
Expand Down Expand Up @@ -142,6 +145,7 @@

/* Begin PBXFileReference section */
4C0B58381B747A4400C0B99C /* ResponseSerializationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResponseSerializationTests.swift; sourceTree = "<group>"; };
4C0B62501BB1001C009302D3 /* Response.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = "<group>"; };
4C0E5BF71B673D3400816CCC /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
4C1DC8531B68908E00476DE3 /* Error.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Error.swift; sourceTree = "<group>"; };
4C23EB421B327C5B0090E0BC /* MultipartFormData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultipartFormData.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -383,6 +387,7 @@
4CDE2C361AF8932A00BABAE5 /* Manager.swift */,
4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */,
4CDE2C391AF899EC00BABAE5 /* Request.swift */,
4C0B62501BB1001C009302D3 /* Response.swift */,
4C0E5BF71B673D3400816CCC /* Result.swift */,
);
name = Core;
Expand Down Expand Up @@ -712,6 +717,7 @@
4CDE2C471AF89FF300BABAE5 /* ResponseSerialization.swift in Sources */,
4C1DC8551B68908E00476DE3 /* Error.swift in Sources */,
4CDE2C381AF8932A00BABAE5 /* Manager.swift in Sources */,
4C0B62521BB1001C009302D3 /* Response.swift in Sources */,
4DD67C251A5C590000ED2280 /* Alamofire.swift in Sources */,
4C23EB441B327C5B0090E0BC /* MultipartFormData.swift in Sources */,
4C811F8E1B51856D00E0F59A /* ServerTrustPolicy.swift in Sources */,
Expand All @@ -732,6 +738,7 @@
4CEC605A1B745C9100E684F4 /* Error.swift in Sources */,
E4202FD21B667AA100C997FB /* ResponseSerialization.swift in Sources */,
E4202FD31B667AA100C997FB /* Manager.swift in Sources */,
4C0B62531BB1001C009302D3 /* Response.swift in Sources */,
4CEC605B1B745C9100E684F4 /* Result.swift in Sources */,
E4202FD41B667AA100C997FB /* Alamofire.swift in Sources */,
E4202FD51B667AA100C997FB /* MultipartFormData.swift in Sources */,
Expand All @@ -752,6 +759,7 @@
4CDE2C461AF89FF300BABAE5 /* ResponseSerialization.swift in Sources */,
4C1DC8541B68908E00476DE3 /* Error.swift in Sources */,
4CDE2C371AF8932A00BABAE5 /* Manager.swift in Sources */,
4C0B62511BB1001C009302D3 /* Response.swift in Sources */,
F897FF4119AA800700AB5182 /* Alamofire.swift in Sources */,
4C23EB431B327C5B0090E0BC /* MultipartFormData.swift in Sources */,
4C811F8D1B51856D00E0F59A /* ServerTrustPolicy.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions Example/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class DetailViewController: UITableViewController {
refreshControl?.beginRefreshing()

let start = CACurrentMediaTime()
request.responseString { request, response, result in
request.responseString { response in
let end = CACurrentMediaTime()
self.elapsedTime = end - start

if let response = response {
if let response = response.response {
for (field, value) in response.allHeaderFields {
self.headers["\(field)"] = "\(value)"
}
Expand All @@ -88,7 +88,7 @@ class DetailViewController: UITableViewController {
if let segueIdentifier = self.segueIdentifier {
switch segueIdentifier {
case "GET", "POST", "PUT", "DELETE":
self.body = result.value
self.body = response.result.value
case "DOWNLOAD":
self.body = self.downloadedBodyString()
default:
Expand Down
2 changes: 1 addition & 1 deletion Source/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public class Request {
let progress: NSProgress

var data: NSData? { return nil }
var error: ErrorType?
var error: NSError?

var credential: NSURLCredential?

Expand Down
83 changes: 83 additions & 0 deletions Source/Response.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Result.swift
//
// Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

/// Used to store all response data returned from a completed `Request`.
public struct Response<Value, Error: ErrorType> {
/// The URL request sent to the server.
public let request: NSURLRequest?

/// The server's response to the URL request.
public let response: NSHTTPURLResponse?

/// The data returned by the server.
public let data: NSData?

/// The result of response serialization.
public let result: Result<Value, Error>

/**
Initializes the `Response` instance with the specified URL request, URL response, server data and response
serialization result.

- parameter request: The URL request sent to the server.
- parameter response: The server's response to the URL request.
- parameter data: The data returned by the server.
- parameter result: The result of response serialization.

- returns: the new `Response` instance.
*/
public init(request: NSURLRequest?, response: NSHTTPURLResponse?, data: NSData?, result: Result<Value, Error>) {
self.request = request
self.response = response
self.data = data
self.result = result
}
}

// MARK: - CustomStringConvertible

extension Response: CustomStringConvertible {
/// The textual representation used when written to an output stream, which includes whether the result was a
/// success or failure.
public var description: String {
return result.debugDescription
}
}

// MARK: - CustomDebugStringConvertible

extension Response: CustomDebugStringConvertible {
/// The debug textual representation used when written to an output stream, which includes the URL request, the URL
/// response, the server data and the response serialization result.
public var debugDescription: String {
var output: [String] = []

output.append(request != nil ? "[Request]: \(request!)" : "[Request]: nil")
output.append(response != nil ? "[Response]: \(response!)" : "[Response]: nil")
output.append("[Data]: \(data?.length ?? 0) bytes")
output.append("[Result]: \(result.debugDescription)")

return output.joinWithSeparator("\n")
}
}
Loading

0 comments on commit 88de889

Please sign in to comment.