From 431d43a70c9c40e53aa8424fbc12cc39e5877fbc Mon Sep 17 00:00:00 2001 From: John Nilsen Date: Fri, 7 Oct 2016 10:39:35 +0200 Subject: [PATCH 1/3] Remove alamofire from router --- Sources/Internal/Router.swift | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/Sources/Internal/Router.swift b/Sources/Internal/Router.swift index 7aff841..c4bc969 100644 --- a/Sources/Internal/Router.swift +++ b/Sources/Internal/Router.swift @@ -6,7 +6,7 @@ // Copyright © 2016 Tapglue. All rights reserved. // -import Alamofire +import Foundation class Router { @@ -63,13 +63,13 @@ class Router { request.setValue(UIDevice.current.systemVersion, forHTTPHeaderField: "X-Tapglue-OSVersion") if method == .post || method == .put{ - let convertible = TapglueURLConvertible(request: request) do { - return try Alamofire.JSONEncoding.default.encode(convertible, with: payload) + request.httpBody = try JSONSerialization.data(withJSONObject: payload, options: []) + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + return request } catch { return request } - //return Alamofire.ParameterEncoding.json.encode(request, parameters: payload).0 } else { return request } @@ -110,22 +110,6 @@ class Router { } } -class TapglueURLConvertible: URLRequestConvertible { - var request: URLRequest - init(request: URLRequest) { - self.request = request - } - - /// Returns a URL request or throws if an `Error` was encountered. - /// - /// - throws: An `Error` if the underlying `URLRequest` is `nil`. - /// - /// - returns: A URL request. - public func asURLRequest() throws -> URLRequest { - return request as URLRequest - } -} - enum Method: String { case get = "get", post = "post", put = "put", delete = "delete" } From 7051fb472802d88e012811c7fc1d774b1f568509 Mon Sep 17 00:00:00 2001 From: John Nilsen Date: Fri, 7 Oct 2016 15:13:23 +0200 Subject: [PATCH 2/3] Replace alamofire code with native networking --- Example/Example.xcodeproj/project.pbxproj | 4 + Example/Example/Example.entitlements | 8 ++ Example/Example/ViewController.swift | 11 +-- Sources/Entities/TapglueError.swift | 2 +- Sources/Internal/Alamofire+Logging.swift | 18 ----- Sources/Internal/Http.swift | 94 ++++++++++++++--------- Sources/Internal/Network.swift | 1 - Sources/RxCompositePage.swift | 3 +- Sources/RxTapglue.swift | 1 - Sources/Sims/DefaultSimsApi.swift | 26 +++++-- Tapglue.xcodeproj/project.pbxproj | 8 +- 11 files changed, 100 insertions(+), 76 deletions(-) create mode 100644 Example/Example/Example.entitlements delete mode 100644 Sources/Internal/Alamofire+Logging.swift diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 19ef28c..1256023 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -89,6 +89,7 @@ 5202B76B1D266A920069BA06 /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = ""; }; 5215FB681D35152700142FCE /* RxBlocking.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxBlocking.framework; path = Carthage/Build/iOS/RxBlocking.framework; sourceTree = ""; }; 5215FB6B1D3516EE00142FCE /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/iOS/Nimble.framework; sourceTree = ""; }; + 5256925A1DA7D56900E71DB9 /* Example.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Example.entitlements; sourceTree = ""; }; 526B17701D2567DE0086EF95 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; 526B17731D2567DE0086EF95 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 526B17751D2567DE0086EF95 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -162,6 +163,7 @@ 526B17721D2567DE0086EF95 /* Example */ = { isa = PBXGroup; children = ( + 5256925A1DA7D56900E71DB9 /* Example.entitlements */, 526B17731D2567DE0086EF95 /* AppDelegate.swift */, 526B17751D2567DE0086EF95 /* ViewController.swift */, 526B17771D2567DE0086EF95 /* Main.storyboard */, @@ -497,6 +499,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; EMBEDDED_CONTENT_CONTAINS_SWIFT = YES; @@ -518,6 +521,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; EMBEDDED_CONTENT_CONTAINS_SWIFT = YES; diff --git a/Example/Example/Example.entitlements b/Example/Example/Example.entitlements new file mode 100644 index 0000000..903def2 --- /dev/null +++ b/Example/Example/Example.entitlements @@ -0,0 +1,8 @@ + + + + + aps-environment + development + + diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index e59045b..55c0b9c 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -26,13 +26,10 @@ class ViewController: UIViewController { // }.addDisposableTo(self.disposeBag) // }.subscribe().addDisposableTo(disposeBag) - //let tapglue = Tapglue(configuration: AppDelegate.config) - //tapglue.loginUser("pablo", password: "supersecret") { user, error in - // print(user?.username ?? "was nil!") - // tapglue.retrieveActivitiesByUser(user!.id!) { activities, error in - // - // } - //} + let tapglue = Tapglue(configuration: AppDelegate.config) + tapglue.loginUser("pablo", password: "supersecret") { user, error in + print(user?.username ?? "was nil!") + } } override func didReceiveMemoryWarning() { diff --git a/Sources/Entities/TapglueError.swift b/Sources/Entities/TapglueError.swift index 1097606..8c17661 100644 --- a/Sources/Entities/TapglueError.swift +++ b/Sources/Entities/TapglueError.swift @@ -10,7 +10,7 @@ import ObjectMapper open class TapglueError: Mappable, Error { open var code: Int? - open var message: String? + open var message: String = "unknown error" init() { diff --git a/Sources/Internal/Alamofire+Logging.swift b/Sources/Internal/Alamofire+Logging.swift deleted file mode 100644 index 067a740..0000000 --- a/Sources/Internal/Alamofire+Logging.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// Alamofire+Logging.swift -// Tapglue -// -// Created by John Nilsen on 7/1/16. -// Copyright © 2016 Tapglue. All rights reserved. -// - -import Alamofire - -extension Request { - public func debugLog() -> Self { - #if DEBUG - log(self) - #endif - return self - } -} \ No newline at end of file diff --git a/Sources/Internal/Http.swift b/Sources/Internal/Http.swift index 3c213e3..30a843c 100644 --- a/Sources/Internal/Http.swift +++ b/Sources/Internal/Http.swift @@ -8,59 +8,70 @@ import Foundation import RxSwift -import Alamofire import ObjectMapper class Http { + private var successCodes: Range = 200..<299 - func execute(_ request: URLRequestConvertible) -> Observable { + func execute(_ request: URLRequest) -> Observable { return Observable.create {observer in - Alamofire.request(request) - .validate() - .debugLog() - .responseJSON { (response:DataResponse) in - log(response.response?.statusCode) - log(response.result.value) - switch(response.result) { - case .success(let value): - log(value) - if let object = Mapper().map(JSONObject: value) { - observer.on(.next(object)) - } else { - if let nf = T.self as? NullableFeed.Type { - let defaultObject = nf.init() - observer.on(.next(defaultObject as! T)) - } + let session = URLSession.shared + var task: URLSessionDataTask? + + log(request) + task = session.dataTask(with: request) { (data: Data?, response:URLResponse?, error:Error?) in + let httpResponse = response as! HTTPURLResponse + log(response) + guard self.successCodes.contains(httpResponse.statusCode) else { + self.handleError(data, onObserver: observer, withDefaultError: error) + return + } + if let data = data { + let json = self.dataToJSON(data: data) + log(json) + if let object = Mapper().map(JSONObject: json) { + observer.on(.next(object)) + } else { + if let nf = T.self as? NullableFeed.Type { + let defaultObject = nf.init() + observer.on(.next(defaultObject as! T)) } - observer.on(.completed) - case .failure(let error): - self.handleError(response.data, onObserver: observer, withDefaultError:error) + } + observer.on(.completed) } } + + task?.resume() + return Disposables.create() } } - func execute(_ request:URLRequestConvertible) -> Observable { + func execute(_ request:URLRequest) -> Observable { + return Observable.create {observer in - Alamofire.request(request) - .validate() - .debugLog() - .responseJSON { response in - log(response.response?.statusCode) - switch(response.result) { - case .success: - observer.on(.completed) - case .failure(let error): - self.handleError(response.data, onObserver: observer, withDefaultError: error) - } + let session = URLSession.shared + var task: URLSessionDataTask? + + log(request) + task = session.dataTask(with: request) { (data: Data?, response:URLResponse?, error:Error?) in + let httpResponse = response as! HTTPURLResponse + log(response) + guard self.successCodes.contains(httpResponse.statusCode) else { + self.handleError(data, onObserver: observer, withDefaultError: error) + return + } + observer.on(.completed) } + + task?.resume() + return Disposables.create() } } fileprivate func handleError(_ data: Data?, onObserver observer: AnyObserver, - withDefaultError error: Error) { + withDefaultError error: Error?) { if let data = data { let json = String(data: data, encoding: String.Encoding.utf8)! if let errorFeed = Mapper().map(JSONString: json) { @@ -68,8 +79,21 @@ class Http { let tapglueErrors = errorFeed.errors! observer.on(.error(tapglueErrors[0])) } else { - observer.on(.error(error)) + if let error = error { + observer.on(.error(error)) + } else { + observer.on(.error(TapglueError())) + } } } } + + fileprivate func dataToJSON(data: Data) -> Any? { + do { + return try JSONSerialization.jsonObject(with: data, options: .mutableContainers) + } catch let myJSONError { + print(myJSONError) + } + return nil + } } diff --git a/Sources/Internal/Network.swift b/Sources/Internal/Network.swift index 9d1325c..e97586c 100644 --- a/Sources/Internal/Network.swift +++ b/Sources/Internal/Network.swift @@ -6,7 +6,6 @@ // Copyright © 2016 Tapglue. All rights reserved. // -import Alamofire import RxSwift class Network { diff --git a/Sources/RxCompositePage.swift b/Sources/RxCompositePage.swift index a2e7760..570de0d 100644 --- a/Sources/RxCompositePage.swift +++ b/Sources/RxCompositePage.swift @@ -7,7 +7,6 @@ // import RxSwift -import Alamofire open class RxCompositePage { open var data: T { @@ -38,7 +37,7 @@ open class RxCompositePage { var request: URLRequest request = Router.getOnURL(prevPointer) - return Http().execute(request as URLRequestConvertible).map { (feed: CompositeFlattenableFeed) in + return Http().execute(request).map { (feed: CompositeFlattenableFeed) in let page = RxCompositePage(feed: feed, previousPointer: feed.page?.before) return page } diff --git a/Sources/RxTapglue.swift b/Sources/RxTapglue.swift index f73a928..db59bca 100644 --- a/Sources/RxTapglue.swift +++ b/Sources/RxTapglue.swift @@ -7,7 +7,6 @@ // import Foundation -import Alamofire import RxSwift /// Provides a RxSwift interface to the tapglue sdk diff --git a/Sources/Sims/DefaultSimsApi.swift b/Sources/Sims/DefaultSimsApi.swift index 7409f59..66b4745 100644 --- a/Sources/Sims/DefaultSimsApi.swift +++ b/Sources/Sims/DefaultSimsApi.swift @@ -7,7 +7,6 @@ // import Foundation -import Alamofire class DefaultSimsApi: SimsApi { @@ -27,15 +26,32 @@ class DefaultSimsApi: SimsApi { let language = Locale.preferredLanguages[0] let headers = [ "Authorization": "Basic " + encodedAuthString!, - "Accept": "application/json" + "Accept": "application/json", + "Content-Type": "application/json" ] let parameters = ["token": deviceToken, "platform": environment.rawValue, "language": language] as [String : Any] - + let idfv = UIDevice.current.identifierForVendor?.uuidString - _ = Alamofire.request(url + deviceRegistrationPath + idfv!, method: HTTPMethod.put, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in - debugPrint(response) + let URL = Foundation.URL(string: self.url + self.deviceRegistrationPath + idfv!)! + + var request = URLRequest(url: URL) + request.allHTTPHeaderFields = headers + request.httpMethod = "put" + do { + request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: []) + + let session = URLSession.shared + var task: URLSessionDataTask? + log(request) + task = session.dataTask(with: request) { (data: Data?, response:URLResponse?, error:Error?) in + log(response) + } + + task?.resume() + } catch { + return } } } diff --git a/Tapglue.xcodeproj/project.pbxproj b/Tapglue.xcodeproj/project.pbxproj index 7229ec9..3dc4af1 100644 --- a/Tapglue.xcodeproj/project.pbxproj +++ b/Tapglue.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 5202B7671D266A0A0069BA06 /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5202B7661D266A0A0069BA06 /* RxSwift.framework */; }; - 5202B76E1D26B30E0069BA06 /* Alamofire+Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5202B76D1D26B30E0069BA06 /* Alamofire+Logging.swift */; }; 5207E25E1D884AE1002EB7F3 /* PageFeed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5207E25D1D884AE1002EB7F3 /* PageFeed.swift */; }; 5211EB901D40EB890030CF9C /* ErrorFeed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5211EB8F1D40EB890030CF9C /* ErrorFeed.swift */; }; 5211EB931D460F2B0030CF9C /* Tapglue.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 5211EB921D460F2B0030CF9C /* Tapglue.podspec */; }; @@ -118,7 +117,6 @@ /* Begin PBXFileReference section */ 5202B7661D266A0A0069BA06 /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = ""; }; - 5202B76D1D26B30E0069BA06 /* Alamofire+Logging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Alamofire+Logging.swift"; sourceTree = ""; }; 5207E25D1D884AE1002EB7F3 /* PageFeed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PageFeed.swift; sourceTree = ""; }; 5211EB8F1D40EB890030CF9C /* ErrorFeed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorFeed.swift; sourceTree = ""; }; 5211EB921D460F2B0030CF9C /* Tapglue.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Tapglue.podspec; sourceTree = ""; }; @@ -338,7 +336,6 @@ isa = PBXGroup; children = ( 52BA9FE81D2FA55A005B4622 /* Feeds */, - 5202B76D1D26B30E0069BA06 /* Alamofire+Logging.swift */, 52E380371D2A847E003A157E /* Router.swift */, 528481DB1D2BAFD80090BA29 /* Encoder.swift */, 52540EE11D2BE2DC0007A9DB /* Network.swift */, @@ -501,7 +498,6 @@ 5207E25E1D884AE1002EB7F3 /* PageFeed.swift in Sources */, 529A3D261D33C5AB0069BF10 /* UIDevice+Tapglue.swift in Sources */, 529A3D2E1D34D97F0069BF10 /* Observable+unwrapping.swift in Sources */, - 5202B76E1D26B30E0069BA06 /* Alamofire+Logging.swift in Sources */, 5274A3351D508A1E00C72378 /* ConnectionType.swift in Sources */, 52970FAB1D9EA0FE0055E626 /* DefaultInstanceEntity.swift in Sources */, 52BA9FEA1D2FA56A005B4622 /* UserFeed.swift in Sources */, @@ -689,7 +685,7 @@ ); INFOPLIST_FILE = Sources/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; OTHER_SWIFT_FLAGS = ""; "OTHER_SWIFT_FLAGS[arch=*]" = "-D DEBUG"; @@ -716,7 +712,7 @@ ); INFOPLIST_FILE = Sources/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; OTHER_SWIFT_FLAGS = ""; PRODUCT_BUNDLE_IDENTIFIER = com.tapglue.Tapglue; From dce148d6c60bb3c85060b4ee1a70212cdce3e00d Mon Sep 17 00:00:00 2001 From: John Nilsen Date: Fri, 7 Oct 2016 16:01:58 +0200 Subject: [PATCH 3/3] Remove alamofire dependency from project files --- Cartfile | 3 +-- Cartfile.resolved | 3 +-- Tapglue.podspec | 3 +-- Tapglue.xcodeproj/project.pbxproj | 3 --- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Cartfile b/Cartfile index 3c0666b..fab049d 100755 --- a/Cartfile +++ b/Cartfile @@ -1,3 +1,2 @@ -github "ReactiveX/RxSwift" "3.0.0-beta.1" -github "Alamofire/Alamofire" ~> 4.0.0 +github "ReactiveX/RxSwift" "3.0.0-beta.2" github "Hearst-DD/ObjectMapper" ~> 2.0 diff --git a/Cartfile.resolved b/Cartfile.resolved index 81e6d48..d1e02a2 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,3 @@ -github "Alamofire/Alamofire" "4.0.1" github "Quick/Nimble" "v5.0.0" github "Hearst-DD/ObjectMapper" "2.0.0" -github "ReactiveX/RxSwift" "3.0.0-beta.1" +github "ReactiveX/RxSwift" "3.0.0-beta.2" diff --git a/Tapglue.podspec b/Tapglue.podspec index de99e73..3f682f3 100644 --- a/Tapglue.podspec +++ b/Tapglue.podspec @@ -11,7 +11,6 @@ Pod::Spec.new do |s| s.ios.deployment_target = '9.0' s.source_files = 'Sources/**/*.{swift}' - s.dependency 'RxSwift', '3.0.0-beta.1' - s.dependency 'Alamofire', '4.0.0' + s.dependency 'RxSwift', '3.0.0-beta.2' s.dependency 'ObjectMapper', '2.0.0' end diff --git a/Tapglue.xcodeproj/project.pbxproj b/Tapglue.xcodeproj/project.pbxproj index 3dc4af1..268739a 100644 --- a/Tapglue.xcodeproj/project.pbxproj +++ b/Tapglue.xcodeproj/project.pbxproj @@ -71,7 +71,6 @@ 52D3EFD31D4B5F3A00E1BA77 /* Connections.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52D3EFD21D4B5F3A00E1BA77 /* Connections.swift */; }; 52D3EFD51D4B7D2100E1BA77 /* ConnectionsFeed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52D3EFD41D4B7D2100E1BA77 /* ConnectionsFeed.swift */; }; 52D8AE541D21C4B000AC947E /* RxTapglue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52D8AE531D21C4B000AC947E /* RxTapglue.swift */; }; - 52D8AE561D22978400AC947E /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D8AE551D22978400AC947E /* Alamofire.framework */; }; 52D8AE581D22986300AC947E /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D8AE551D22978400AC947E /* Alamofire.framework */; }; 52D8AE5A1D229A3500AC947E /* Alamofire.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 52D8AE551D22978400AC947E /* Alamofire.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 52D8AE5C1D229A7D00AC947E /* Cartfile in Resources */ = {isa = PBXBuildFile; fileRef = 52D8AE5B1D229A7D00AC947E /* Cartfile */; }; @@ -195,7 +194,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 52D8AE561D22978400AC947E /* Alamofire.framework in Frameworks */, 52D8AE641D2557E200AC947E /* ObjectMapper.framework in Frameworks */, 5202B7671D266A0A0069BA06 /* RxSwift.framework in Frameworks */, ); @@ -477,7 +475,6 @@ files = ( ); inputPaths = ( - "$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework", "$(SRCROOT)/Carthage/Build/iOS/ObjectMapper.framework", "$(SRCROOT)/Carthage/Build/iOS/RxSwift.framework", );