Skip to content

Commit

Permalink
Merge pull request #152 from tapglue/ios8-support
Browse files Browse the repository at this point in the history
Ios8 support
  • Loading branch information
nilsen340 authored Oct 7, 2016
2 parents 9e06f6c + dce148d commit 9462028
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 105 deletions.
3 changes: 1 addition & 2 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
5202B76B1D266A920069BA06 /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = "<group>"; };
5215FB681D35152700142FCE /* RxBlocking.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxBlocking.framework; path = Carthage/Build/iOS/RxBlocking.framework; sourceTree = "<group>"; };
5215FB6B1D3516EE00142FCE /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/iOS/Nimble.framework; sourceTree = "<group>"; };
5256925A1DA7D56900E71DB9 /* Example.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Example.entitlements; sourceTree = "<group>"; };
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 = "<group>"; };
526B17751D2567DE0086EF95 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -162,6 +163,7 @@
526B17721D2567DE0086EF95 /* Example */ = {
isa = PBXGroup;
children = (
5256925A1DA7D56900E71DB9 /* Example.entitlements */,
526B17731D2567DE0086EF95 /* AppDelegate.swift */,
526B17751D2567DE0086EF95 /* ViewController.swift */,
526B17771D2567DE0086EF95 /* Main.storyboard */,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions Example/Example/Example.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>
11 changes: 4 additions & 7 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Entities/TapglueError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
18 changes: 0 additions & 18 deletions Sources/Internal/Alamofire+Logging.swift

This file was deleted.

94 changes: 59 additions & 35 deletions Sources/Internal/Http.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,92 @@

import Foundation
import RxSwift
import Alamofire
import ObjectMapper

class Http {
private var successCodes: Range<Int> = 200..<299

func execute<T:Mappable>(_ request: URLRequestConvertible) -> Observable<T> {
func execute<T:Mappable>(_ request: URLRequest) -> Observable<T> {
return Observable.create {observer in
Alamofire.request(request)
.validate()
.debugLog()
.responseJSON { (response:DataResponse<Any>) in
log(response.response?.statusCode)
log(response.result.value)
switch(response.result) {
case .success(let value):
log(value)
if let object = Mapper<T>().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<T>().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<Void> {
func execute(_ request:URLRequest) -> Observable<Void> {

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<T>(_ data: Data?, onObserver observer: AnyObserver<T>,
withDefaultError error: Error) {
withDefaultError error: Error?) {
if let data = data {
let json = String(data: data, encoding: String.Encoding.utf8)!
if let errorFeed = Mapper<ErrorFeed>().map(JSONString: json) {
log(errorFeed.toJSONString())
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
}
}
1 change: 0 additions & 1 deletion Sources/Internal/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright © 2016 Tapglue. All rights reserved.
//

import Alamofire
import RxSwift

class Network {
Expand Down
24 changes: 4 additions & 20 deletions Sources/Internal/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 Tapglue. All rights reserved.
//

import Alamofire
import Foundation

class Router {

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/RxCompositePage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import RxSwift
import Alamofire

open class RxCompositePage<T: DefaultInstanceEntity> {
open var data: T {
Expand Down Expand Up @@ -38,7 +37,7 @@ open class RxCompositePage<T: DefaultInstanceEntity> {
var request: URLRequest
request = Router.getOnURL(prevPointer)

return Http().execute(request as URLRequestConvertible).map { (feed: CompositeFlattenableFeed<T>) in
return Http().execute(request).map { (feed: CompositeFlattenableFeed<T>) in
let page = RxCompositePage<T>(feed: feed, previousPointer: feed.page?.before)
return page
}
Expand Down
1 change: 0 additions & 1 deletion Sources/RxTapglue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import Alamofire
import RxSwift

/// Provides a RxSwift interface to the tapglue sdk
Expand Down
26 changes: 21 additions & 5 deletions Sources/Sims/DefaultSimsApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import Alamofire

class DefaultSimsApi: SimsApi {

Expand All @@ -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
}
}
}
3 changes: 1 addition & 2 deletions Tapglue.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 9462028

Please sign in to comment.