我刚刚开始使用 Swift(一种非常好的语言)进行编码,并且我正在尝试制作一个需要用户使用第三方登录服务登录的应用程序。
身份验证流程的基本内容如下所示: 1. 用户输入 ssn(瑞典人编号)并按 Enter。
2. POST 到返回 json blob 的 url:
{
"transactionId": "a transaction id",
"expires": "date sting in some iso format",
"autostartToken": "irrelevant for my usage"
}
3. 轮询使用transactionId
来自步骤 2的 url。
此 url 返回一个 json blob:
{
"state": "OUTSTANDING_TRANSACTION",
// other stuff that's uninteresting
}
一旦用户使用移动身份验证应用程序授予访问权限,此 url 将返回更复杂的 json blob。然后state
将更改为“已完成”。4. 从可以从步骤 3 中的 blob 获得的最终 url 接收身份验证令牌(一旦状态为“COMPLETED”
。5.???
6.获利!
所以我的“问题”是我无法真正弄清楚(以我有限的快速知识)如何执行第 3 步。轮询 url 直到状态为“COMPLETED”(或者第 2 步的过期时间已通过,它应该失败)。
我在 javascript 中做了一个 hacky 尝试来尝试该服务,它看起来像这样:
this.postMethodThatReturnsAPromise(url, data).then(response => {
let {transactionId} = response.body;
let self = this,
max = 10,
num = 0;
return new Promise(function (resolve, reject) {
(function poll() {
self._get(`baseurl/${transactionId}`).then(res => {
let {state} = res.body;
if (state !== 'COMPLETE' && num < max) {
setTimeout(poll, 2000);
} else if (state === 'COMPLETE') {
return resolve(res);
}
});
num++;
})();
});
})
我如何在 swift 3 中使用 Alamofire 和 Promisekit 做到这一点?
return Alamofire.request(url, method: .post, /* rest is omitted */).responseJSON().then { response -> String in
let d = res as! Dictionary<String, Any>
return d["transactionId"]
}.then { transactionId -> [String: Any] in
// TODO: The polling until blob contains "state" with value "COMPLETED"
// Return the final json blob as dict to the next promise handler
}.then { data in
}