Skip to content

Commit

Permalink
Merge pull request #194 from ishkawa/si-swift3
Browse files Browse the repository at this point in the history
Swift 3 support
  • Loading branch information
ishkawa authored Sep 17, 2016
2 parents f31e271 + 8de24db commit 2296315
Show file tree
Hide file tree
Showing 54 changed files with 1,048 additions and 1,072 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
url = https://github.com/antitypical/Result.git
[submodule "Carthage/Checkouts/OHHTTPStubs"]
path = Carthage/Checkouts/OHHTTPStubs
url = https://github.com/ikesyo/OHHTTPStubs.git
url = https://github.com/AliSoftware/OHHTTPStubs.git
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ env:

matrix:
include:
- os: osx
language: objective-c
osx_image: xcode7.3
env: JOB=Xcode7.3
- os: osx
language: objective-c
osx_image: xcode8
Expand All @@ -21,9 +17,9 @@ install:
script:
- if [[ "$JOB" == "Xcode7.3" ]]; then pod lib lint; fi
- set -o pipefail
- xcodebuild test -workspace APIKit.xcworkspace -scheme APIKit | xcpretty -c
- xcodebuild test -workspace APIKit.xcworkspace -scheme APIKit -sdk iphonesimulator -destination "name=iPhone 6s" | xcpretty -c
- xcodebuild test -workspace APIKit.xcworkspace -scheme APIKit -sdk appletvsimulator -destination "name=Apple TV 1080p" | xcpretty -c
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit | xcpretty -c
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -sdk iphonesimulator -destination "name=iPhone 6s" | xcpretty -c
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -sdk appletvsimulator -destination "name=Apple TV 1080p" | xcpretty -c
- carthage build --no-skip-current

after_success:
Expand Down
98 changes: 47 additions & 51 deletions APIKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "antitypical/Result" ~> 2.1.2
github "antitypical/Result" ~> 3.0
2 changes: 1 addition & 1 deletion Cartfile.private
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "ikesyo/OHHTTPStubs" "swift2.3"
github "AliSoftware/OHHTTPStubs" "5.2.1-swift3"
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "ikesyo/OHHTTPStubs" "b9abbd044ebeecbedf8db10721ed24e4bfe07f29"
github "antitypical/Result" "2.1.2"
github "AliSoftware/OHHTTPStubs" "5.2.1-swift3"
github "antitypical/Result" "3.0.0"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/OHHTTPStubs
Submodule OHHTTPStubs updated 28 files
+10 −10 .travis.yml
+14 −1 CHANGELOG.md
+8 −8 Examples/ObjC/Podfile.lock
+0 −1 Examples/ObjC/Pods/Headers/Private/OHHTTPStubs/NSMutableURLRequest+HTTPBodyTesting.h
+1 −0 Examples/ObjC/Pods/Headers/Private/OHHTTPStubs/NSURLRequest+HTTPBodyTesting.h
+0 −1 Examples/ObjC/Pods/Headers/Public/OHHTTPStubs/NSMutableURLRequest+HTTPBodyTesting.h
+1 −0 Examples/ObjC/Pods/Headers/Public/OHHTTPStubs/NSURLRequest+HTTPBodyTesting.h
+2 −2 Examples/ObjC/Pods/Local Podspecs/OHHTTPStubs.podspec.json
+8 −8 Examples/ObjC/Pods/Manifest.lock
+52 −52 Examples/ObjC/Pods/Pods.xcodeproj/project.pbxproj
+9 −9 Examples/Swift/Podfile.lock
+2 −2 Examples/Swift/Pods/Local Podspecs/OHHTTPStubs.podspec.json
+9 −9 Examples/Swift/Pods/Manifest.lock
+58 −58 Examples/Swift/Pods/Pods.xcodeproj/project.pbxproj
+1 −1 Examples/Swift/Pods/Target Support Files/OHHTTPStubs/Info.plist
+1 −1 Examples/Swift/Pods/Target Support Files/OHHTTPStubs/OHHTTPStubs-umbrella.h
+1 −1 OHHTTPStubs.podspec
+29 −21 OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj
+1 −1 OHHTTPStubs/OHHTTPStubs.xcodeproj/xcshareddata/xcschemes/OHHTTPStubs Mac Framework.xcscheme
+1 −1 OHHTTPStubs/OHHTTPStubs.xcodeproj/xcshareddata/xcschemes/OHHTTPStubs iOS Framework.xcscheme
+1 −1 OHHTTPStubs/OHHTTPStubs.xcodeproj/xcshareddata/xcschemes/OHHTTPStubs iOS StaticLib.xcscheme
+1 −1 OHHTTPStubs/OHHTTPStubs.xcodeproj/xcshareddata/xcschemes/OHHTTPStubs tvOS Framework.xcscheme
+2 −2 OHHTTPStubs/Sources/HTTPMessage/OHHTTPStubsResponse+HTTPMessage.h
+2 −2 OHHTTPStubs/Sources/OHHTTPStubsResponse.h
+83 −25 OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift
+96 −13 OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift
+104 −26 README.md
+9 −7 Rakefile
32 changes: 16 additions & 16 deletions Demo.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
import XCPlayground
import PlaygroundSupport
import UIKit
import APIKit

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
PlaygroundPage.current.needsIndefiniteExecution = true

//: Step 1: Define request protocol
protocol GitHubRequestType: RequestType {
protocol GitHubRequest: Request {

}

extension GitHubRequestType {
var baseURL: NSURL {
return NSURL(string: "https://api.github.com")!
extension GitHubRequest {
var baseURL: URL {
return URL(string: "https://api.github.com")!
}
}

//: Step 2: Create model object
struct RateLimit {
let count: Int
let resetDate: NSDate
let resetDate: Date

init?(dictionary: [String: AnyObject]) {
guard let count = dictionary["rate"]?["limit"] as? Int else {
return nil
}

guard let resetDateString = dictionary["rate"]?["reset"] as? NSTimeInterval else {
guard let resetDateString = dictionary["rate"]?["reset"] as? TimeInterval else {
return nil
}

self.count = count
self.resetDate = NSDate(timeIntervalSince1970: resetDateString)
self.resetDate = Date(timeIntervalSince1970: resetDateString)
}
}

//: Step 3: Define request type conforming to created request protocol
// https://developer.github.com/v3/rate_limit/
struct GetRateLimitRequest: GitHubRequestType {
struct GetRateLimitRequest: GitHubRequest {
typealias Response = RateLimit

var method: HTTPMethod {
return .GET
return .get
}

var path: String {
return "/rate_limit"
}

func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> Response {
func response(from object: Any, urlResponse: HTTPURLResponse) throws -> Response {
guard let dictionary = object as? [String: AnyObject],
let rateLimit = RateLimit(dictionary: dictionary) else {
throw ResponseError.UnexpectedObject(object)
throw ResponseError.unexpectedObject(object)
}

return rateLimit
Expand All @@ -60,13 +60,13 @@ struct GetRateLimitRequest: GitHubRequestType {
//: Step 4: Send request
let request = GetRateLimitRequest()

Session.sendRequest(request) { result in
Session.send(request) { result in
switch result {
case .Success(let rateLimit):
case .success(let rateLimit):
print("count: \(rateLimit.count)")
print("reset: \(rateLimit.resetDate)")

case .Failure(let error):
case .failure(let error):
print("error: \(error)")
}
}
20 changes: 20 additions & 0 deletions Sources/BodyParameters/BodyParameters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

/// `RequestBodyEntity` represents entity of HTTP body.
public enum RequestBodyEntity {
/// Expresses entity as `Data`. The associated value will be set to `URLRequest.httpBody`.
case data(Data)

/// Expresses entity as `InputStream`. The associated value will be set to `URLRequest.httpBodyStream`.
case inputStream(InputStream)
}

/// `BodyParameters` provides interface to parse HTTP response body and to state `Content-Type` to accept.
public protocol BodyParameters {
/// `Content-Type` to send. The value for this property will be set to `Accept` HTTP header field.
var contentType: String { get }

/// Builds `RequestBodyEntity`.
/// Throws: `ErrorType`
func buildEntity() throws -> RequestBodyEntity
}
36 changes: 36 additions & 0 deletions Sources/BodyParameters/Data+InputStream.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Foundation

enum InputStreamError: Error {
case invalidDataCapacity(Int)
case unreadableStream(InputStream)
}

extension Data {
init(inputStream: InputStream, capacity: Int = Int(UInt16.max)) throws {
var data = Data(capacity: capacity)

let bufferSize = Swift.min(Int(UInt16.max), capacity)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)

var readSize: Int

repeat {
readSize = inputStream.read(buffer, maxLength: bufferSize)

switch readSize {
case let x where x > 0:
data.append(buffer, count: readSize)

case let x where x < 0:
throw InputStreamError.unreadableStream(inputStream)

default:
break
}
} while readSize > 0

buffer.deallocate(capacity: bufferSize)

self.init(data)
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import Foundation

/// `FormURLEncodedBodyParameters` serializes form object for HTTP body and states its content type is form.
public struct FormURLEncodedBodyParameters: BodyParametersType {
public struct FormURLEncodedBodyParameters: BodyParameters {
/// The form object to be serialized.
public let form: [String: AnyObject]
public let form: [String: Any]

/// The string encoding of the serialized form.
public let encoding: NSStringEncoding
public let encoding: String.Encoding

/// Returns `FormURLEncodedBodyParameters` that is initialized with form object and encoding.
public init(formObject: [String: AnyObject], encoding: NSStringEncoding = NSUTF8StringEncoding) {
public init(formObject: [String: Any], encoding: String.Encoding = .utf8) {
self.form = formObject
self.encoding = encoding
}

// MARK: - BodyParametersType
// MARK: - BodyParameters

/// `Content-Type` to send. The value for this property will be set to `Accept` HTTP header field.
public var contentType: String {
return "application/x-www-form-urlencoded"
}

/// Builds `RequestBodyEntity.Data` that represents `form`.
/// Builds `RequestBodyEntity.data` that represents `form`.
/// - Throws: `URLEncodedSerialization.Error` if `URLEncodedSerialization` fails to serialize form object.
public func buildEntity() throws -> RequestBodyEntity {
return .Data(try URLEncodedSerialization.dataFromObject(form, encoding: encoding))
return .data(try URLEncodedSerialization.data(from: form, encoding: encoding))
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import Foundation

/// `JSONBodyParameters` serializes JSON object for HTTP body and states its content type is JSON.
public struct JSONBodyParameters: BodyParametersType {
public struct JSONBodyParameters: BodyParameters {
/// The JSON object to be serialized.
public let JSONObject: AnyObject
public let JSONObject: Any

/// The writing options for serialization.
public let writingOptions: NSJSONWritingOptions
public let writingOptions: JSONSerialization.WritingOptions

/// Returns `JSONBodyParameters` that is initialized with JSON object and writing options.
public init(JSONObject: AnyObject, writingOptions: NSJSONWritingOptions = []) {
public init(JSONObject: Any, writingOptions: JSONSerialization.WritingOptions = []) {
self.JSONObject = JSONObject
self.writingOptions = writingOptions
}

// MARK: - BodyParametersType
// MARK: - BodyParameters

/// `Content-Type` to send. The value for this property will be set to `Accept` HTTP header field.
public var contentType: String {
return "application/json"
}

/// Builds `RequestBodyEntity.Data` that represents `JSONObject`.
/// - Throws: `NSError` if `NSJSONSerialization` fails to serialize `JSONObject`.
/// Builds `RequestBodyEntity.data` that represents `JSONObject`.
/// - Throws: `NSError` if `JSONSerialization` fails to serialize `JSONObject`.
public func buildEntity() throws -> RequestBodyEntity {
// If isValidJSONObject(_:) is false, dataWithJSONObject(_:options:) throws NSException.
guard NSJSONSerialization.isValidJSONObject(JSONObject) else {
guard JSONSerialization.isValidJSONObject(JSONObject) else {
throw NSError(domain: NSCocoaErrorDomain, code: 3840, userInfo: nil)
}

return .Data(try NSJSONSerialization.dataWithJSONObject(JSONObject, options: writingOptions))
return .data(try JSONSerialization.data(withJSONObject: JSONObject, options: writingOptions))
}
}
Loading

0 comments on commit 2296315

Please sign in to comment.