@@ -59,8 +59,10 @@ public enum Allow2Response {
5959 case CheckResult( Allow2CheckResult )
6060 case Request( Bool )
6161
62- static func parseFromJSON( response : JSON ) -> Allow2Response {
63- guard response [ " error " ] != " invalid pairId " else {
62+ static func parseFromJSON( _ json : JSON , response: HTTPURLResponse ? = nil ) -> Allow2Response {
63+ guard json [ " error " ] != " invalid pairId " ,
64+ json [ " error " ] != " invalid pairToken " ,
65+ response? . statusCode ?? 200 != 401 else {
6466 // special case, no longer controlled
6567 Allow2 . shared. userId = nil
6668 Allow2 . shared. pairId = nil
@@ -76,17 +78,17 @@ public enum Allow2Response {
7678 ) )
7779 }
7880
79- if let requestSent = response [ " requestSent " ] . bool {
81+ if let requestSent = json [ " requestSent " ] . bool {
8082 return . Request( requestSent)
8183 }
8284
83- guard let allowed = response [ " allowed " ] . bool else {
85+ guard let allowed = json [ " allowed " ] . bool else {
8486 return . Error( Allow2Error . InvalidResponse)
8587 }
86- let activities = response [ " activities " ]
87- let dayTypes = response [ " dayTypes " ]
88- let children = response [ " children " ]
89- let allDayTypes = response [ " allDayTypes " ]
88+ let activities = json [ " activities " ]
89+ let dayTypes = json [ " dayTypes " ]
90+ let children = json [ " children " ]
91+ let allDayTypes = json [ " allDayTypes " ]
9092 Allow2 . shared. _children = children. arrayValue. map { ( child) -> Allow2Child in
9193 return Allow2Child ( id: child [ " id " ] . uInt64Value,
9294 name: child [ " name " ] . stringValue,
@@ -417,9 +419,7 @@ public class Allow2 {
417419 public func check( childId: String ! , activities: [ Allow2Activity ] ! , log: Bool = true , completion: ( ( Allow2Response ) -> Void ) ? = nil ) {
418420
419421 guard self . isPaired else {
420- if completion != nil {
421- completion!( Allow2Response . Error ( Allow2Error . NotPaired ) )
422- }
422+ completion ? ( Allow2Response . Error ( Allow2Error . NotPaired ) )
423423 return
424424 }
425425
@@ -441,9 +441,7 @@ public class Allow2 {
441441
442442 if checkResult. expires. timeIntervalSinceNow. sign != . minus {
443443 // not expired yet, use cached value
444- if completion != nil {
445- completion!( Allow2Response . CheckResult ( checkResult) )
446- }
444+ completion ? ( Allow2Response . CheckResult ( checkResult) )
447445 return
448446 }
449447
@@ -475,6 +473,31 @@ public class Allow2 {
475473
476474 // interpret the result
477475 // todo: 403 is disconnected, clear everything out
476+ // this doesn't work here, it's handled by parseFromJSON below
477+ // if let httpResponse = response as? HTTPURLResponse,
478+ // httpResponse.statusCode == 401 {
479+ // // device released
480+ // self.userId = nil
481+ // self.pairId = nil
482+ // self.childId = nil
483+ // self._children = []
484+ // self._dayTypes = []
485+ //
486+ // // notify everyone
487+ // NotificationCenter.default.post(
488+ // name: .allow2CheckResultNotification,
489+ // object: nil,
490+ // userInfo: [ "result" : [
491+ // "allowed": true,
492+ // "activities": [],
493+ // "dayTypes": [],
494+ // "allDayTypes": [],
495+ // "children": []
496+ // ]]
497+ // )
498+ // completion?(Allow2Response.Error( Allow2Error.NotPaired ))
499+ // return
500+ // }
478501
479502 // handle other errors
480503
@@ -485,7 +508,7 @@ public class Allow2 {
485508 return ;
486509 }
487510
488- let result = Allow2Response . parseFromJSON ( response: json )
511+ let result = Allow2Response . parseFromJSON ( json , response: response as? HTTPURLResponse )
489512
490513 switch result {
491514 case let . CheckResult( checkResult) :
@@ -549,9 +572,7 @@ extension Allow2 {
549572 public func request( dayTypeId: UInt64 ? , lift: [ UInt64 ] ? , message: String ? , completion: ( ( Allow2Response ) -> Void ) ? = nil ) {
550573
551574 guard self . isPaired else {
552- if completion != nil {
553- completion!( Allow2Response . Error ( Allow2Error . NotPaired ) )
554- }
575+ completion ? ( Allow2Response . Error ( Allow2Error . NotPaired ) )
555576 return
556577 }
557578
@@ -598,34 +619,20 @@ extension Allow2 {
598619
599620 // attempt to handle valid response
600621 // todo: better error handling on data -> JSON
601- guard let json = try ? JSON ( data: data!) else {
602- completion ? ( Allow2Response . Error ( Allow2Error . InvalidResponse ) )
622+ // guard let json = try? JSON(data: data!) else {
623+ // completion?(Allow2Response.Error( Allow2Error.InvalidResponse ))
624+ // return;
625+ // }
626+
627+ guard ( response as? HTTPURLResponse ) ? . statusCode ?? 500 == 200 else {
628+ completion ? ( Allow2Response . Error ( Allow2Error . Other ( message: " Something went wrong " ) ) )
603629 return ;
604630 }
605631
606- let result = Allow2Response . parseFromJSON ( response : json)
632+ // let result = Allow2Response.parseFromJSON(json)
607633
608- switch result {
609- case let . CheckResult( checkResult) :
610-
611- // good response, cache the result first
612- self . resultCache [ key] = checkResult
613-
614- // notify everyone
615- NotificationCenter . default. post (
616- name: . allow2CheckResultNotification,
617- object: nil ,
618- userInfo: [ " result " : checkResult ]
619- )
620-
621- break
622- default :
623- completion ? ( result)
624- return
625- }
626-
627- // now return the result
628- completion ? ( result)
634+ completion ? ( Allow2Response . Request ( true ) )
635+
629636 }
630637 task. resume ( )
631638
0 commit comments