PromiseKit - 重新加载第一个承诺的可选承诺

时间:2018-02-09 22:17:17

标签: promisekit

我阅读了所有文档,但我没有成功回答我的问题。 我有这个场景

  • 我打电话给经过身份验证的api点
    1. 如果回复是200到299我满足
    2. 如果响应为401,我会调用api来刷新令牌

如果我刷新了API令牌,我需要重新调用第一个api点(用于恢复所需的数据)。 你能帮我解决这个问题吗? 感谢

1 个答案:

答案 0 :(得分:0)

根据需要使用递归。使用PromiseKit你可以做很棒的事情。请参阅下面的刷新令牌请求。我希望下面的代码是自我记录的。

// Define here your errors 
enum MyErrors: Swift.Error {
    case unauthorized
    case unknown
}

// Here is your primary function for retrieving remote data    
func fetchRemoteData() {
    self.primaryRequest()
        .then { data in
            return self.parsePrimaryRequest(response: data)
        }
        .done { parsedResult in
            // do something here if all chaining is success
        }
        .recover { primaryRequestError in // this is your case for refresh token
            if case MyErrors.unauthorized = primaryRequestError { // if not authorized
                self.refreshTokenRequest()
                    .then { data in
                        return self.parseRefreshToken(data: data)
                    }
                    .done { token in
                        // do someting with token, save it in secure store (keychain)
                        self.fetchRemoteData()
                    }
                    .catch { refreshTokenError in
                    // refresh token request has failed
                }
            }
            else {
                throw primaryRequestError
            }
        }
        .catch { primaryRequestError in
            // here if error during request
    }
}

func primaryRequest() -> Promise<Data> {
    return Promise { seal in
        // here you execure the request
        // ...
        // ...
        let responseData = Data() // here you get data from URLSessionTask etc
        let yourRequestStatusCode = 401 // take status code


        let successCodes = Array(200...299) // define success codes
        let authorizationErrorCode = 401 // define status code which requires performing refresh token request
        if successCodes.contains(yourRequestStatusCode) {
            return seal.fulfill(responseData)
        } else if yourRequestStatusCode == authorizationErrorCode {
            throw MyErrors.unauthorized
        } else {
            throw MyErrors.unknown
        }
    }
}

func parsePrimaryRequest(response: Data) -> Promise<String> {
    return Promise { seal in
        // here you parse response
        // ...
        // ...

        let parsedObject = "Awesome result"
        seal.fulfill(parsedObject)
    }
}

func refreshTokenRequest() -> Promise<Data> {
    return Promise { seal in
        // here you execure the request to refresh token
        // ...
        // ...
        let responseData = Data() // here you get data from URLSessionTask etc
        let yourRequestStatusCode = 401 // take status code


        let successCodes = Array(200...299) // define success codes for refresh token request
        if successCodes.contains(yourRequestStatusCode) {
            return seal.fulfill(responseData)
        } else {
            throw MyErrors.unknown
        }
    }
}

func parseRefreshToken(data: Data) -> Promise<String> {
    return Promise { seal in
        // here you parse response
        // ...
        // ...

        let parsedObject = "00000000-00000000-00000000-00000000"
        seal.fulfill(parsedObject)
    }
}