Skip to content

Commit 2415a9c

Browse files
Merge pull request #42 from ScalaInc/feature/xcode-8.3
Feature/xcode 8.3
2 parents a2ee53a + 6d8a4ca commit 2415a9c

18 files changed

+218
-36
lines changed

ExpSwift.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Pod::Spec.new do |s|
3737
# s.frameworks = 'UIKit', 'MapKit'
3838
# s.dependency 'AFNetworking', '~> 2.3'
3939
s.dependency 'PromiseKit','4.0.4'
40-
s.dependency 'Alamofire','4.0.1'
41-
s.dependency 'Socket.IO-Client-Swift','8.0.2'
40+
s.dependency 'Alamofire','4.4.0'
41+
s.dependency 'Socket.IO-Client-Swift','8.3.3'
4242
s.dependency 'JSONWebToken','2.0.1'
4343
end

ExpSwift/Classes/Auth.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
import Foundation
1010
public final class Auth: Model,ResponseObject {
1111

12-
required public init?(response: HTTPURLResponse, representation: Any) {
13-
let representation = representation as? [String: AnyObject]
14-
super.init(response: response, representation: representation!)
12+
required public init?(response: HTTPURLResponse?, representation: Any?) {
13+
super.init(response: response, representation: representation)
1514
}
1615

1716

17+
public func getToken() -> String {
18+
return get("token") as! String
19+
}
20+
21+
22+
23+
1824
}

ExpSwift/Classes/Content.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class Content: Model,ResponseObject,ResponseCollection {
2424
case UNKNOWN = ""
2525
}
2626

27-
required public init?(response: HTTPURLResponse, representation: Any) {
27+
required public init?(response: HTTPURLResponse?, representation: Any?) {
2828
if let representation = representation as? [String: AnyObject] {
2929
self.uuid = representation["uuid"] as! String
3030
self.subtype = CONTENT_TYPES(rawValue: representation["subtype"] as! String)!

ExpSwift/Classes/ContentNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public final class ContentNode: Model,ResponseObject,ResponseCollection {
2525
case UNKNOWN = ""
2626
}
2727

28-
required public init?(response: HTTPURLResponse, representation: Any) {
28+
required public init?(response: HTTPURLResponse?, representation: Any?) {
2929
if let representation = representation as? [String: AnyObject] {
3030
self.uuid = representation["uuid"] as! String
3131
self.subtype = CONTENT_TYPES(rawValue: representation["subtype"] as! String)!

ExpSwift/Classes/Data.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public final class Data: Model,ResponseObject,ResponseCollection,ModelProtocol {
1515
public let group: String
1616
public let key: String
1717

18-
required public init?(response: HTTPURLResponse, representation: Any) {
18+
required public init?(response: HTTPURLResponse?, representation: Any?) {
1919
if let representation = representation as? [String: AnyObject] {
2020
self.group = representation["group"] as! String
2121
self.key = representation["key"] as! String

ExpSwift/Classes/Device.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public final class Device: Model,ResponseObject,ResponseCollection,ModelProtocol
1818
fileprivate var experience:Experience?
1919

2020

21-
required public init?(response: HTTPURLResponse, representation: Any) {
21+
required public init?(response: HTTPURLResponse?, representation: Any?) {
2222
let representation = representation as? [String: AnyObject]
2323
self.uuid = representation?["uuid"] as! String
2424
if let dic = representation?["location"] as? NSDictionary {

ExpSwift/Classes/Exp.swift

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var hostSocket: String = ""
1919
public var auth:Auth?
2020
var socketManager = SocketManager()
2121
var runtime = Runtime()
22+
let timeoutLogin = 10
2223
public typealias CallBackTypeConnection = (String) -> Void
2324
var authConnection = [String: CallBackTypeConnection]()
2425

@@ -63,6 +64,8 @@ enum Router: URLRequestConvertible {
6364
case refreshToken()
6465
case broadcast([String: Any],String)
6566
case respond([String: Any])
67+
case getCurrentUser()
68+
case getToken([String: Any])
6669
//create
6770
case createDevice([String:Any])
6871
case createData(String,String,[String:Any])
@@ -165,6 +168,10 @@ enum Router: URLRequestConvertible {
165168
return .delete
166169
case .deleteThing:
167170
return .delete
171+
case .getCurrentUser:
172+
return .get
173+
case .getToken:
174+
return .post
168175
}
169176
}
170177

@@ -248,7 +255,10 @@ enum Router: URLRequestConvertible {
248255
return "/api/locations/\(uuid)"
249256
case .deleteThing(let uuid):
250257
return "/api/things/\(uuid)"
251-
258+
case .getCurrentUser:
259+
return "/api/users/current"
260+
case .getToken:
261+
return "/api/auth/token"
252262
}
253263
}
254264

@@ -334,6 +344,9 @@ enum Router: URLRequestConvertible {
334344
case .saveThing(_,let parameters):
335345
urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
336346
expLogging("EXP Http Request saveThing: \(urlRequest)")
347+
case .getToken(let parameters):
348+
urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
349+
expLogging("EXP Http Request getToken: \(urlRequest)")
337350
default:
338351
break
339352
}
@@ -366,10 +379,19 @@ Initialize the SDK and connect to EXP.
366379
@param options.
367380
@return Promise<Bool>.
368381
*/
369-
public func start(_ options:[String:String]) -> Promise<Bool> {
382+
public func start(_ options:[String:Any]) -> Promise<Bool> {
370383
return runtime.start(options)
371384
}
372385

386+
/**
387+
Initialize the SDK and connect to EXP.
388+
@param options.
389+
@return Promise<Bool>.
390+
*/
391+
public func start(_ host:String, auth:Auth) -> Promise<Bool> {
392+
return runtime.start(host,auth: auth)
393+
}
394+
373395
/**
374396
Get list of devices
375397
@param dictionary of search params
@@ -843,9 +865,9 @@ public func deleteFeed(_ uuid:String) -> Promise<Void>{
843865
@param options.
844866
@return Promise<Auth>.
845867
*/
846-
func login(_ options:[String:String]) ->Promise<Auth>{
868+
func login(_ options:[String:Any?]) ->Promise<Auth>{
847869
return Promise { fulfill, reject in
848-
Alamofire.request(Router.login(options as [String : AnyObject])).validate()
870+
Alamofire.request(Router.login(options)).validate()
849871
.responseObject { (response: DataResponse<Auth>) in
850872
switch response.result{
851873
case .success(let data):
@@ -1015,6 +1037,80 @@ public func respond(_ params:[String:Any]) -> Promise<Message>{
10151037
}
10161038
}
10171039

1040+
/**
1041+
Get Current User
1042+
@param params.
1043+
@return Promise<User>.
1044+
*/
1045+
public func getCurrentUser() -> Promise<User>{
1046+
return Promise { fulfill, reject in
1047+
Alamofire.request(Router.getCurrentUser()).validate()
1048+
.responseObject { (response: DataResponse<User>) in
1049+
switch response.result{
1050+
case .success(let data):
1051+
fulfill(data)
1052+
case .failure(let error):
1053+
return reject(error)
1054+
}
1055+
}
1056+
}
1057+
}
1058+
1059+
/**
1060+
Get Current User
1061+
@param params.
1062+
@return Promise<User>.
1063+
*/
1064+
public func getToken(_ options:[String:Any]) -> Promise<Auth>{
1065+
return Promise { fulfill, reject in
1066+
Alamofire.request(Router.getToken(options)).validate()
1067+
.responseObject { (response: DataResponse<Auth>) in
1068+
switch response.result{
1069+
case .success(let data):
1070+
fulfill(data)
1071+
case .failure(let error):
1072+
return reject(error)
1073+
}
1074+
}
1075+
}
1076+
}
1077+
1078+
1079+
/**
1080+
Get Current User
1081+
@param params.
1082+
@return Promise<User>.
1083+
*/
1084+
public func getUser(_ host:String, token:String) -> Promise<User>{
1085+
expLogging("EXP GET USER : \(token)")
1086+
tokenSDK = token
1087+
hostUrl = host
1088+
return Promise { fulfill, reject in
1089+
Alamofire.request(Router.getCurrentUser()).validate()
1090+
.responseObject { (response: DataResponse<User>) in
1091+
switch response.result{
1092+
case .success(let data):
1093+
fulfill(data)
1094+
case .failure(let error):
1095+
return reject(error)
1096+
}
1097+
}
1098+
}
1099+
}
1100+
1101+
/**
1102+
Starth with Auth
1103+
@param options.
1104+
@return Promise<Auth>.
1105+
*/
1106+
func startAuth(_ data:Auth) {
1107+
//set auth to context
1108+
auth = data
1109+
setTokenSDK(data)
1110+
refreshAuthToken(data)
1111+
}
1112+
1113+
10181114

10191115
/**
10201116
Connection Socket
@@ -1046,7 +1142,7 @@ public func stop(){
10461142

10471143

10481144
/**
1049-
Refresh Auth Token Recursive with Timeout
1145+
Refresh Auth Token Recursive with Auth
10501146
*/
10511147
private func refreshAuthToken(_ result:Auth){
10521148
after(interval: getTimeout(result)).then{ result -> Void in
@@ -1057,14 +1153,39 @@ private func refreshAuthToken(_ result:Auth){
10571153
refreshAuthToken(result)
10581154
}
10591155
}
1156+
}
10601157

1158+
/**
1159+
Refresh Auth Token Recursive with Timeout
1160+
*/
1161+
private func refreshAuthToken(_ timeout:String){
1162+
after(interval: getTimeout(Double(timeout)!)).then{ result -> Void in
1163+
refreshToken().then{ result -> Void in
1164+
setTokenSDK(result)
1165+
expLogging("EXP refreshAuthToken: \(result.document)")
1166+
socketManager.refreshConnection()
1167+
refreshAuthToken(result)
1168+
}
1169+
}
10611170
}
10621171

10631172
/**
10641173
Get Time Out
10651174
*/
10661175
private func getTimeout(_ data:Auth) -> TimeInterval{
1067-
let expiration = data.get("expiration") as! Double
1176+
var timeoutVal:TimeInterval = TimeInterval(timeoutLogin);
1177+
if let expiration = data.get("expiration") as? Double{
1178+
let startDate = Date(timeIntervalSince1970: expiration/1000)
1179+
var timeout = startDate.timeIntervalSince(Date())
1180+
timeoutVal = TimeInterval(Int64(timeout))
1181+
}
1182+
return timeoutVal
1183+
}
1184+
1185+
/**
1186+
Get Time Out
1187+
*/
1188+
private func getTimeout(_ expiration:Double) -> TimeInterval{
10681189
let startDate = Date(timeIntervalSince1970: expiration/1000)
10691190
let timeout = startDate.timeIntervalSince(Date())
10701191
return TimeInterval(Int64(timeout))

ExpSwift/Classes/Experience.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public final class Experience: Model,ResponseObject,ResponseCollection,ModelProt
1515

1616
public let uuid: String
1717

18-
required public init?(response: HTTPURLResponse, representation: Any) {
18+
required public init?(response: HTTPURLResponse?, representation: Any?) {
1919
if let representation = representation as? [String: AnyObject] {
2020
self.uuid = representation["uuid"] as! String
2121
} else {

ExpSwift/Classes/Feed.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class Feed: Model,ResponseObject,ResponseCollection,ModelProtocol {
1414

1515
public let uuid: String
1616

17-
required public init?(response: HTTPURLResponse, representation: Any) {
17+
required public init?(response: HTTPURLResponse?, representation: Any?) {
1818
if let representation = representation as? [String: AnyObject] {
1919
self.uuid = representation["uuid"] as! String
2020
} else {

ExpSwift/Classes/Location.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public final class Location: Model,ResponseObject,ResponseCollection,ModelProtoc
1717
public var zones: [Zone] = []
1818

1919

20-
required public init?(response: HTTPURLResponse, representation: Any) {
20+
required public init?(response: HTTPURLResponse?, representation: Any?) {
2121
if let representation = representation as? [String: AnyObject] {
2222
self.uuid = representation["uuid"] as! String
2323
} else {

0 commit comments

Comments
 (0)