Skip to content

Commit

Permalink
Migrate to Swift 3 (#15)
Browse files Browse the repository at this point in the history
* [swift-3.0] Migrate to Swift3

* [swift-3.0] Remove redundant input paramter in the Adapter adapt method

* [swift-3.0] Deprecate Oauth2 components aiming for the use of Paparajote

* [swift-3.0] Add pod repo update command to travis

* [swift-3.0] Update the pod repo update script to be silent

* [swift-3.0] Update gem dependencies

* [swift-3.0] Disable some tests

* [swift-3.0] Re-enable tests and fix the travis build script

* [swift-3.0] Disable some tests that are no passing on travis-ci

* [swift-3.0] Update CHANGELOG & Quick version
  • Loading branch information
Pedro Piñera Buendía authored Sep 24, 2016
1 parent 2205a25 commit 936b0c4
Show file tree
Hide file tree
Showing 31 changed files with 363 additions and 346 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
osx_image: xcode7.3
osx_image: xcode8
language: objective-c
notifications:
email: false
before_install:
- gem install bundler
- bundle install
- bundle exec pod repo add carambalabs https://github.com/carambalabs/CarambaKit
- bundle exec pod repo update --silent
- git remote set-url origin "https://${GITHUB_TOKEN}@github.com/carambalabs/CarambaKit.git"
script:
- bundle exec fastlane travis
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 1.0.0
- Swift 3.0 support.
- Dropped Swift 2.3 support.

### 0.0.41

* Add Danger
10 changes: 5 additions & 5 deletions CarambaKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'CarambaKit'
s.version = '0.0.43'
s.version = '1.0.0'
s.summary = 'Core components used for our projects'
s.description = <<-DESC
Set of Core components, including Networking, Persistence, .. that are used in our apps.s
Expand All @@ -17,14 +17,14 @@ Set of Core components, including Networking, Persistence, .. that are used in o

s.subspec "Foundation" do |sp|
sp.source_files = 'CarambaKit/Classes/Foundation/**/*'
sp.dependency 'RxSwift', '~> 2.6'
sp.dependency 'RxCocoa', '~> 2.6'
sp.dependency 'SwiftyJSON', '~> 2.3'
sp.dependency 'RxSwift', '~> 3.0.0-beta.1'
sp.dependency 'RxCocoa', '~> 3.0.0-beta.1'
sp.dependency 'SwiftyJSON', '~> 3.0'
end

s.subspec "Networking" do |sp|
sp.dependency 'CarambaKit/Foundation'
sp.dependency 'KeychainSwift', '~> 3.0'
sp.dependency 'KeychainSwift', '~> 6.0'
sp.source_files = 'CarambaKit/Classes/Networking/**/*'
end

Expand Down
2 changes: 1 addition & 1 deletion CarambaKit/Classes/Foundation/Protocols/Adapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Adapter<I, O> {

public init() {}

public func adapt(input: I) -> O! {
public func adapt(_ input: I) -> O! {
assertionFailure("This method must be overriden")
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions CarambaKit/Classes/Networking/Client/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ public class HttpClient<T> {
// MARK: - Attributes

private let requestDispatcher: UrlRequestDispatcher
private let sessionAdapter: Adapter<NSURLRequest, NSURLRequest>?
private let responseAdapter: Adapter<(data: NSData?, response: NSURLResponse?), (T, NSURLResponse?)>
private let sessionAdapter: Adapter<URLRequest, URLRequest>?
private let responseAdapter: Adapter<(data: Data?, response: URLResponse?), (T, URLResponse?)>

// MARK: - Init

public init(responseAdapter: Adapter<(data: NSData?, response: NSURLResponse?), (T, NSURLResponse?)>,
public init(responseAdapter: Adapter<(data: Data?, response: URLResponse?), (T, URLResponse?)>,
requestDispatcher: UrlRequestDispatcher = UrlRequestDispatcher(),
sessionAdapter: Adapter<NSURLRequest, NSURLRequest>? = nil) {
sessionAdapter: Adapter<URLRequest, URLRequest>? = nil) {
self.responseAdapter = responseAdapter
self.requestDispatcher = requestDispatcher
self.sessionAdapter = sessionAdapter
}

// MARK: - Init

public func request(request: NSURLRequest) -> Observable<(T, NSURLResponse?)> {
public func request(request: URLRequest) -> Observable<(T, URLResponse?)> {
var authenticatedRequest = self.sessionAdapter?.adapt(request) ?? request
return self.requestDispatcher.dispatch(authenticatedRequest)
return self.requestDispatcher.dispatch(request: authenticatedRequest)
.map({self.responseAdapter.adapt($0)!})
}

Expand Down
12 changes: 6 additions & 6 deletions CarambaKit/Classes/Networking/Client/JsonHttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ public class JsonHttpClient: HttpClient<JSON> {

// MARK: - Init

public init(requestDispatcher: UrlRequestDispatcher = UrlRequestDispatcher(), sessionAdapter: Adapter<NSURLRequest, NSURLRequest>? = nil) {
public init(requestDispatcher: UrlRequestDispatcher = UrlRequestDispatcher(), sessionAdapter: Adapter<URLRequest, URLRequest>? = nil) {
super.init(responseAdapter: UrlJsonResponseAdapter.instance, requestDispatcher: requestDispatcher, sessionAdapter: sessionAdapter)
}

override public func request(request: NSURLRequest) -> Observable<(JSON, NSURLResponse?)> {
return super.request(self.requestWithJSONHeaders(request))
override public func request(request: URLRequest) -> Observable<(JSON, URLResponse?)> {
return super.request(request: self.requestWithJSONHeaders(request: request))
}

// MARK: - Private

private func requestWithJSONHeaders(request: NSURLRequest) -> NSURLRequest {
let mutableRequest: NSMutableURLRequest = request.mutableCopy() as! NSMutableURLRequest
private func requestWithJSONHeaders(request: URLRequest) -> URLRequest {
var mutableRequest: URLRequest = request
var headers = mutableRequest.allHTTPHeaderFields ?? [:]
headers["Accept"] = "application/json"
mutableRequest.allHTTPHeaderFields = headers
return mutableRequest.copy() as! NSURLRequest
return mutableRequest
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Foundation
import SwiftyJSON

public class UrlJsonResponseAdapter: Adapter<(data: NSData?, response: NSURLResponse?), (JSON, NSURLResponse?)> {
public class UrlJsonResponseAdapter: Adapter<(data: Data?, response: URLResponse?), (JSON, URLResponse?)> {

// MARK: - Singleton

public static var instance: UrlJsonResponseAdapter = UrlJsonResponseAdapter()

// MARK: - Public

public override func adapt(input: (data: NSData?, response: NSURLResponse?)) -> (JSON, NSURLResponse?)! {
let json = JSON(data: input.data ?? NSData())
public override func adapt(_ input: (data: Data?, response: URLResponse?)) -> (JSON, URLResponse?)! {
let json = JSON(data: input.data ?? Data())
return (json: json, response: input.response)
}

Expand Down
11 changes: 6 additions & 5 deletions CarambaKit/Classes/Networking/Client/UrlRequestDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ public class UrlRequestDispatcher {

// MARK: - Attributes

let configuration: NSURLSessionConfiguration
let configuration: URLSessionConfiguration

// MARK: - Singleton

public static var instance: UrlRequestDispatcher = UrlRequestDispatcher()

// MARK: - Init

public init(configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()) {
public init(configuration: URLSessionConfiguration = URLSessionConfiguration.default) {
self.configuration = configuration
}

// MARK: - Public

public func dispatch(request: NSURLRequest) -> Observable<(data: NSData?, response: NSURLResponse?)> {
let session = NSURLSession(configuration: self.configuration)
public func dispatch(request request: URLRequest) -> Observable<(data: Data?, response: URLResponse?)> {
let session = URLSession(configuration: self.configuration)
return Observable.create { (observer) -> Disposable in
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
if let error = error {
print(error)
observer.onError(error)
} else {
observer.onNext((data: data, response: response))
Expand Down
3 changes: 2 additions & 1 deletion CarambaKit/Classes/Networking/Oauth2/Oauth2Entity.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Foundation
import SwiftyJSON

@available(*, deprecated, message: "Use https://github.com/carambalabs/paparajote instead")
public protocol Oauth2Entity {

func authorizationUrl() -> String
func authenticationRequestFromUrl(url: String) -> NSURLRequest?
func authenticationRequestFromUrl(url: String) -> URLRequest?
func sessionFromJSON(response: JSON) throws -> Oauth2Session

}
5 changes: 3 additions & 2 deletions CarambaKit/Classes/Networking/Oauth2/Oauth2Error.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

public enum Oauth2Error: ErrorType {
case AlreadyStarted
@available(*, deprecated, message: "Use https://github.com/carambalabs/paparajote instead")
public enum Oauth2Error: Error {
case alreadyStarted
}
7 changes: 4 additions & 3 deletions CarambaKit/Classes/Networking/Oauth2/Oauth2Event.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Foundation

@available(*, deprecated, message: "Use https://github.com/carambalabs/paparajote instead")
public protocol Oauth2Delegate: class {

func oauth2OpenUrl(url: String)
func oauth2DidFail(withError error: ErrorType)
func oauth2DidComplete(withSession session: Oauth2Session)
func oauth2Open(url url: String)
func oauth2DidFail(with error: Error)
func oauth2DidComplete(with session: Oauth2Session)

}
21 changes: 11 additions & 10 deletions CarambaKit/Classes/Networking/Oauth2/Oauth2Handler.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import RxSwift

@available(*, deprecated, message: "Use https://github.com/carambalabs/paparajote instead")
public class Oauth2Handler {

// MARK: - Attributes
Expand All @@ -22,36 +23,36 @@ public class Oauth2Handler {

public func start() throws {
if self.active {
throw Oauth2Error.AlreadyStarted
throw Oauth2Error.alreadyStarted
}
self.active = true
self.delegate?.oauth2OpenUrl(self.entity.authorizationUrl())
self.delegate?.oauth2Open(url: self.entity.authorizationUrl())
}

public func shouldRedirectUrl(url: String) -> Bool {
let request = self.entity.authenticationRequestFromUrl(url)
let request = self.entity.authenticationRequestFromUrl(url: url)
if let request = request {
self.performAuthentication(request)
self.performAuthentication(request: request)
}
return request == nil
}

// MARK: - Private

private func performAuthentication(request: NSURLRequest) {
_ = self.client.request(request)
private func performAuthentication(request: URLRequest) {
_ = self.client.request(request: request)
.doOnNext { [weak self] (json, response) in
do {
if let session = try self?.entity.sessionFromJSON(json) {
self?.delegate?.oauth2DidComplete(withSession: session)
if let session = try self?.entity.sessionFromJSON(response: json) {
self?.delegate?.oauth2DidComplete(with: session)
}
} catch {
self?.delegate?.oauth2DidFail(withError: error)
self?.delegate?.oauth2DidFail(with: error)
}
self?.active = false
}
.doOnError { [weak self] (error) in
self?.delegate?.oauth2DidFail(withError: error)
self?.delegate?.oauth2DidFail(with: error)
self?.active = false
}
.subscribe()
Expand Down
1 change: 1 addition & 0 deletions CarambaKit/Classes/Networking/Oauth2/Oauth2Session.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@available(*, deprecated, message: "Use https://github.com/carambalabs/paparajote instead")
public struct Oauth2Session: Equatable {
public let accessToken: String
public let refreshToken: String
Expand Down
Loading

0 comments on commit 936b0c4

Please sign in to comment.