@@ -1071,6 +1071,7 @@ open class LCUser: LCObject {
10711071
10721072 // MARK: Auth Data
10731073
1074+ /// The third party platform
10741075 public enum AuthDataPlatform {
10751076 case qq
10761077 case weibo
@@ -1091,18 +1092,28 @@ open class LCUser: LCObject {
10911092 }
10921093 }
10931094
1095+ /// The options of auth data
10941096 public struct AuthDataOptions : OptionSet {
10951097 public let rawValue : Int
10961098
10971099 public init ( rawValue: Int ) {
10981100 self . rawValue = rawValue
10991101 }
11001102
1103+ /// Using the auth data as main data.
11011104 public static let mainAccount = AuthDataOptions ( rawValue: 1 << 0 )
11021105
1106+ /// If a user with the auth data not exist, then return error.
11031107 public static let failOnNotExist = AuthDataOptions ( rawValue: 1 << 1 )
11041108 }
11051109
1110+ /// Login with third party auth data synchronously.
1111+ /// - Parameters:
1112+ /// - authData: The auth data of third party account.
1113+ /// - platform: The platform of third party account. @see `AuthDataPlatform`.
1114+ /// - unionID: The union ID of the auth data.
1115+ /// - unionIDPlatform: The platform of the `unionID`. @see `AuthDataPlatform`.
1116+ /// - options: @see `AuthDataOptions`.
11061117 public func logIn(
11071118 authData: [ String : Any ] ,
11081119 platform: AuthDataPlatform ,
@@ -1118,18 +1129,29 @@ open class LCUser: LCObject {
11181129 unionID: unionID,
11191130 unionIDPlatform: unionIDPlatform,
11201131 options: options,
1121- completionInBackground: { fulfill ( $0) }
1122- )
1132+ completionInBackground: { result in
1133+ fulfill ( result)
1134+ } )
11231135 }
11241136 }
11251137
1138+ /// Login with third party auth data asynchronously.
1139+ /// - Parameters:
1140+ /// - authData: The auth data of third party account.
1141+ /// - platform: The platform of third party account. @see `AuthDataPlatform`.
1142+ /// - unionID: The union ID of the auth data.
1143+ /// - unionIDPlatform: The platform of the `unionID`. @see `AuthDataPlatform`.
1144+ /// - options: @see `AuthDataOptions`.
1145+ /// - completionQueue: The queue where the `completion` be executed, default is main.
1146+ /// - completion: Result callback.
11261147 @discardableResult
11271148 public func logIn(
11281149 authData: [ String : Any ] ,
11291150 platform: AuthDataPlatform ,
11301151 unionID: String ? = nil ,
11311152 unionIDPlatform: AuthDataPlatform ? = nil ,
11321153 options: AuthDataOptions ? = nil ,
1154+ completionQueue: DispatchQueue = . main,
11331155 completion: @escaping ( LCBooleanResult ) -> Void )
11341156 -> LCRequest
11351157 {
@@ -1139,8 +1161,11 @@ open class LCUser: LCObject {
11391161 unionID: unionID,
11401162 unionIDPlatform: unionIDPlatform,
11411163 options: options,
1142- completionInBackground: { ( result) in mainQueueAsync { completion ( result) } }
1143- )
1164+ completionInBackground: { ( result) in
1165+ completionQueue. async {
1166+ completion ( result)
1167+ }
1168+ } )
11441169 }
11451170
11461171 @discardableResult
@@ -1153,57 +1178,62 @@ open class LCUser: LCObject {
11531178 completionInBackground completion: @escaping ( LCBooleanResult ) -> Void )
11541179 -> LCRequest
11551180 {
1156- var authData : [ String : Any ] = authData
1157- if let unionID: String = unionID {
1181+ var authData = authData
1182+ if let unionID = unionID {
11581183 authData [ " unionid " ] = unionID
11591184 }
1160- if let unionIDPlatform: AuthDataPlatform = unionIDPlatform {
1185+ if let unionIDPlatform = unionIDPlatform {
11611186 authData [ " platform " ] = unionIDPlatform. key
11621187 }
1163- if let options: AuthDataOptions = options, options. contains ( . mainAccount) {
1188+ if let options = options, options. contains ( . mainAccount) {
11641189 authData [ " main_account " ] = true
11651190 }
11661191
1167- var parameters = ( self . dictionary. jsonValue as? [ String : Any ] ) ?? [ : ]
1192+ var parameters = ( self . dictionary. lconValue as? [ String : Any ] ) ?? [ : ]
11681193 parameters [ " authData " ] = [ platform. key: authData]
11691194 parameters. removeValue ( forKey: " __type " )
11701195 parameters. removeValue ( forKey: " className " )
11711196
1172- let path : String
1197+ var path = " users "
11731198 if let options = options, options. contains ( . failOnNotExist) {
1174- path = " users?failOnNotExist=true "
1175- } else {
1176- path = " users "
1199+ path += " ?failOnNotExist=true "
11771200 }
11781201
1179- let request = self . application. httpClient. request ( . post, path, parameters: parameters) { response in
1202+ return self . application. httpClient. request (
1203+ . post, path,
1204+ parameters: parameters)
1205+ { response in
11801206 if let error = LCError ( response: response) {
11811207 completion ( . failure( error: error) )
11821208 } else {
11831209 if let dictionary = response. value as? [ String : Any ] {
1184-
11851210 ObjectProfiler . shared. updateObject ( self , dictionary)
11861211 self . application. currentUser = self
1187-
11881212 completion ( . success)
11891213 } else {
1190- let error = LCError ( code: . invalidType, reason: " invalid response data type. " )
1191- completion ( . failure( error: error) )
1214+ completion ( . failure(
1215+ error: LCError (
1216+ code: . invalidType,
1217+ reason: " invalid response data type. " ) ) )
11921218 }
11931219 }
11941220 }
1195-
1196- return request
11971221 }
11981222
1223+ /// Associate the user with third party auth data synchronously.
1224+ /// - Parameters:
1225+ /// - authData: The auth data of third party account.
1226+ /// - platform: The platform of third party account. @see `AuthDataPlatform`.
1227+ /// - unionID: The union ID of the auth data.
1228+ /// - unionIDPlatform: The platform of the `unionID`. @see `AuthDataPlatform`.
1229+ /// - options: @see `AuthDataOptions`.
11991230 public func associate(
12001231 authData: [ String : Any ] ,
12011232 platform: AuthDataPlatform ,
12021233 unionID: String ? = nil ,
12031234 unionIDPlatform: AuthDataPlatform ? = nil ,
12041235 options: AuthDataOptions ? = nil )
1205- throws
1206- -> LCBooleanResult
1236+ throws -> LCBooleanResult
12071237 {
12081238 return try expect { fulfill in
12091239 try self . associate (
@@ -1212,30 +1242,43 @@ open class LCUser: LCObject {
12121242 unionID: unionID,
12131243 unionIDPlatform: unionIDPlatform,
12141244 options: options,
1215- completionInBackground: { fulfill ( $0) }
1216- )
1245+ completionInBackground: { result in
1246+ fulfill ( result)
1247+ } )
12171248 }
12181249 }
12191250
1251+ /// Associate the user with third party auth data asynchronously.
1252+ /// - Parameters:
1253+ /// - authData: The auth data of third party account.
1254+ /// - platform: The platform of third party account. @see `AuthDataPlatform`.
1255+ /// - unionID: The union ID of the auth data.
1256+ /// - unionIDPlatform: The platform of the `unionID`. @see `AuthDataPlatform`.
1257+ /// - options: @see `AuthDataOptions`.
1258+ /// - completionQueue: The queue where the `completion` be executed, default is main.
1259+ /// - completion: Result callback.
12201260 @discardableResult
12211261 public func associate(
12221262 authData: [ String : Any ] ,
12231263 platform: AuthDataPlatform ,
12241264 unionID: String ? = nil ,
12251265 unionIDPlatform: AuthDataPlatform ? = nil ,
12261266 options: AuthDataOptions ? = nil ,
1267+ completionQueue: DispatchQueue = . main,
12271268 completion: @escaping ( LCBooleanResult ) -> Void )
1228- throws
1229- -> LCRequest
1269+ throws -> LCRequest
12301270 {
12311271 return try self . associate (
12321272 authData: authData,
12331273 platform: platform,
12341274 unionID: unionID,
12351275 unionIDPlatform: unionIDPlatform,
12361276 options: options,
1237- completionInBackground: { result in mainQueueAsync { completion ( result) } }
1238- )
1277+ completionInBackground: { result in
1278+ completionQueue. async {
1279+ completion ( result)
1280+ }
1281+ } )
12391282 }
12401283
12411284 @discardableResult
@@ -1249,105 +1292,141 @@ open class LCUser: LCObject {
12491292 throws
12501293 -> LCRequest
12511294 {
1252- guard let objectID: String = self . objectId? . stringValue else {
1253- throw LCError ( code: . inconsistency, reason: " object id not found. " )
1295+ guard let objectID = self . objectId? . value else {
1296+ throw LCError (
1297+ code: . inconsistency,
1298+ reason: " object id not found. " )
12541299 }
1255- guard let sessionToken: String = self . sessionToken? . stringValue else {
1256- throw LCError ( code: . inconsistency, reason: " session token not found. " )
1300+ guard let sessionToken = self . sessionToken? . value else {
1301+ throw LCError (
1302+ code: . inconsistency,
1303+ reason: " session token not found. " )
12571304 }
12581305
1259- var authData : [ String : Any ] = authData
1260- if let unionID: String = unionID {
1306+ var authData = authData
1307+ if let unionID = unionID {
12611308 authData [ " unionid " ] = unionID
12621309 }
1263- if let unionIDPlatform: AuthDataPlatform = unionIDPlatform {
1310+ if let unionIDPlatform = unionIDPlatform {
12641311 authData [ " platform " ] = unionIDPlatform. key
12651312 }
1266- if let options: AuthDataOptions = options, options. contains ( . mainAccount) {
1313+ if let options = options, options. contains ( . mainAccount) {
12671314 authData [ " main_account " ] = true
12681315 }
12691316
12701317 let path : String = " users/ \( objectID) "
1271- let parameters : [ String : Any ] = [ " authData " : [ platform. key : authData] ]
1318+ let parameters : [ String : Any ] = [ " authData " : [ platform. key: authData] ]
12721319 let headers : [ String : String ] = [ HTTPClient . HeaderFieldName. session: sessionToken]
12731320
1274- let request = self . application. httpClient. request ( . put, path, parameters: parameters, headers: headers) { response in
1321+ return self . application. httpClient. request (
1322+ . put, path,
1323+ parameters: parameters,
1324+ headers: headers)
1325+ { response in
12751326 if let error = LCError ( response: response) {
12761327 completion ( . failure( error: error) )
12771328 } else {
1278- if var dictionary = response. value as? [ String : Any ] {
1279- var originAuthData : [ String : Any ] = ( self . authData? . jsonValue as? [ String : Any ] ) ?? [ : ]
1280- originAuthData [ platform. key] = authData
1281- dictionary [ " authData " ] = originAuthData
1282- ObjectProfiler . shared. updateObject ( self , dictionary)
1283- completion ( . success)
1329+ if let dictionary = response. value as? [ String : Any ] {
1330+ do {
1331+ if let originAuthData = self . authData {
1332+ originAuthData [ platform. key] = try LCDictionary (
1333+ application: self . application,
1334+ unsafeObject: authData)
1335+ } else {
1336+ self . authData = try LCDictionary (
1337+ application: self . application,
1338+ unsafeObject: [ platform. key: authData] )
1339+ }
1340+ ObjectProfiler . shared. updateObject ( self , dictionary)
1341+ completion ( . success)
1342+ } catch {
1343+ completion ( . failure(
1344+ error: LCError (
1345+ error: error) ) )
1346+ }
12841347 } else {
1285- let error = LCError ( code: . invalidType, reason: " invalid response data type. " )
1286- completion ( . failure( error: error) )
1348+ completion ( . failure(
1349+ error: LCError (
1350+ code: . invalidType,
1351+ reason: " invalid response data type. " ) ) )
12871352 }
12881353 }
12891354 }
1290-
1291- return request
12921355 }
12931356
1357+ /// Disassociate the user with third party auth data synchronously.
1358+ /// - platform: The platform of third party account. @see `AuthDataPlatform`.
12941359 public func disassociate( authData platform: AuthDataPlatform ) throws -> LCBooleanResult {
12951360 return try expect { fulfill in
12961361 try self . disassociate (
12971362 authData: platform,
1298- completionInBackground: { fulfill ( $0) }
1299- )
1363+ completionInBackground: { result in
1364+ fulfill ( result)
1365+ } )
13001366 }
13011367 }
13021368
1369+ /// Disassociate the user with third party auth data asynchronously.
1370+ /// - Parameters:
1371+ /// - platform: The platform of third party account. @see `AuthDataPlatform`.
1372+ /// - completionQueue: The queue where the `completion` be executed, default is main.
1373+ /// - completion: Result callback.
13031374 @discardableResult
13041375 public func disassociate(
13051376 authData platform: AuthDataPlatform ,
1377+ completionQueue: DispatchQueue = . main,
13061378 completion: @escaping ( LCBooleanResult ) -> Void )
1307- throws
1308- -> LCRequest
1379+ throws -> LCRequest
13091380 {
13101381 return try self . disassociate (
13111382 authData: platform,
1312- completionInBackground: { result in mainQueueAsync { completion ( result) } }
1313- )
1383+ completionInBackground: { result in
1384+ completionQueue. async {
1385+ completion ( result)
1386+ }
1387+ } )
13141388 }
13151389
13161390 @discardableResult
13171391 private func disassociate(
13181392 authData platform: AuthDataPlatform ,
13191393 completionInBackground completion: @escaping ( LCBooleanResult ) -> Void )
1320- throws
1321- -> LCRequest
1394+ throws -> LCRequest
13221395 {
1323- guard let objectID: String = self . objectId? . stringValue else {
1324- throw LCError ( code: . inconsistency, reason: " object id not found. " )
1396+ guard let objectID = self . objectId? . value else {
1397+ throw LCError (
1398+ code: . inconsistency,
1399+ reason: " object id not found. " )
13251400 }
1326- guard let sessionToken: String = self . sessionToken? . stringValue else {
1327- throw LCError ( code: . inconsistency, reason: " session token not found. " )
1401+ guard let sessionToken = self . sessionToken? . value else {
1402+ throw LCError (
1403+ code: . inconsistency,
1404+ reason: " session token not found. " )
13281405 }
13291406
13301407 let path : String = " users/ \( objectID) "
13311408 let parameters : [ String : Any ] = [ " authData. \( platform. key) " : [ Operation . key: Operation . Name. delete. rawValue] ]
13321409 let headers : [ String : String ] = [ HTTPClient . HeaderFieldName. session: sessionToken]
13331410
1334- let request = self . application. httpClient. request ( . put, path, parameters: parameters, headers: headers) { response in
1411+ return self . application. httpClient. request (
1412+ . put, path,
1413+ parameters: parameters,
1414+ headers: headers)
1415+ { response in
13351416 if let error = LCError ( response: response) {
13361417 completion ( . failure( error: error) )
13371418 } else {
1338- if var dictionary = response. value as? [ String : Any ] {
1339- var originAuthData : [ String : Any ] = ( self . authData? . jsonValue as? [ String : Any ] ) ?? [ : ]
1340- originAuthData. removeValue ( forKey: platform. key)
1341- dictionary [ " authData " ] = originAuthData
1419+ if let dictionary = response. value as? [ String : Any ] {
1420+ self . authData? . removeValue ( forKey: platform. key)
13421421 ObjectProfiler . shared. updateObject ( self , dictionary)
13431422 completion ( . success)
13441423 } else {
1345- let error = LCError ( code: . invalidType, reason: " invalid response data type. " )
1346- completion ( . failure( error: error) )
1424+ completion ( . failure(
1425+ error: LCError (
1426+ code: . invalidType,
1427+ reason: " invalid response data type. " ) ) )
13471428 }
13481429 }
13491430 }
1350-
1351- return request
13521431 }
13531432}
0 commit comments