- 📙 Focusing on Swift & iOS
- 🔨 Creator of applications and frameworks
- 🐏 Founder of ObjCCN
- 🍖 Meat lover
componentnetworking's Introduction
componentnetworking's People
Forkers
linsyorozuya xaosbear nick6969 donaldlwu littlema0404 bruce-pac zealforbeing evan3rd hayasilin xiahaozhejose areckkimo wuchiwei pei1133 pinyuanchencomponentnetworking's Issues
疑問
let task = session.dataTask(with: urlRequest) {
data, response, error in
guard let data = data else {
handler(.failure(error ?? ResponseError.nilData))
return
}
guard let response = response as? HTTPURLResponse else {
handler(.failure(ResponseError.nonHTTPResponse))
return
}
self.handleDecision(
request,
data: data,
response: response,
decisions: decisions ?? request.decisions,
handler: handler
)
}
1.閼於data和response檢查,兩都一定都要有嗎,才能進到handleDecision嗎
舉例:https://docs.microsoft.com/en-us/rest/api/cognitiveservices/qnamakerruntime/runtime/train
該api就是沒有data,但卻有response,response code 204,所以照目前的邏輯,是執行不到ParseResultDecision
是不是就不要先檢查有沒有data
struct RefreshTokenDecision: Decision {
func shouldApply<Req: Request>(request: Req, data: Data, response: HTTPURLResponse) -> Bool {
return response.statusCode == 403
}
func apply<Req: Request>(
request: Req,
data: Data,
response: HTTPURLResponse,
done closure: @escaping (DecisionAction<Req>) -> Void)
{
let refreshTokenRequest = RefreshTokenRequest(refreshToken: "abc123")
client.send(refreshTokenRequest) { result in
switch result {
case .success(_):
let decisionsWithoutRefresh = request.decisions.removing(self)
closure(.restartWith(decisionsWithoutRefresh))
case .failure(let error): closure(.errored(error))
}
}
}
}
- 當token過期、無效或revoked,返回的statusCode是401還是403,因為我在某些oAuth2 api得到的是401,但我去翻rfc6749,沒有說明該返回的status code是多少
struct RetryDecision: Decision {
let leftCount: Int
func shouldApply<Req: Request>(request: Req, data: Data, response: HTTPURLResponse) -> Bool {
let isStatusCodeValid = (200..<300).contains(response.statusCode)
return !isStatusCodeValid && leftCount > 0
}
func apply<Req: Request>(
request: Req,
data: Data,
response: HTTPURLResponse,
done closure: @escaping (DecisionAction<Req>) -> Void)
{
let retryDecision = RetryDecision(leftCount: leftCount - 1)
let newDecisions = request.decisions.replacing(self, with: retryDecision)
closure(.restartWith(newDecisions))
}
}
1.我想到一種可能,當user輸入是錯誤密碼,api返回status code 非2xx,但依目前的邏輯,就會重試,而不會直接到BadResponseStatusCodeDecision
2.let newDecisions = request.decisions.replacing(self, with: retryDecision)
因為是從request取出default decisions,除了要取代原本的RetryDecision,還要移除前面的RefreshTokenDecision,這樣是不是乾脆在handleDecision func內,把移掉後的decisions傳入apply func(新增一個decisions參數)
struct BadResponseStatusCodeDecision: Decision {
func shouldApply<Req: Request>(request: Req, data: Data, response: HTTPURLResponse) -> Bool {
return !(200..<300).contains(response.statusCode)
}
func apply<Req: Request>(
request: Req,
data: Data,
response: HTTPURLResponse,
done closure: @escaping (DecisionAction<Req>) -> Void)
{
do {
let value = try decoder.decode(APIError.self, from: data)
closure(.errored(ResponseError.apiError(error: value, statusCode: response.statusCode)))
} catch {
closure(.errored(error))
}
}
}
- status code 非2xx時,data還是會返回有內容嗎? 因為我遇過api是401時,data是沒有內容,這樣是不是不符合規範?
有refreshtoken實作但卻沒有取得refresh token部份,以OAuth2為例,第一次會跳出授權視窗,當user按同意後,就會取得access token和refresh token,這部份要如何把它擴充到目前的框架
另外除了oauth認証機制外,還有apikey、jwt,我的想法是定義一個auth enum,列出所有的auth方式,再來extension一個adapter再adapted,還是有什麼更合適的做法
以上,謝謝
Please provide license
I would like to use this demo code as a starter to refactor my app. Please provide a open source license to make sure people can use them legally. Many thanks!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.