swift-server / swift-server/async-http-client
support middlewares
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 1.1k
- Forks
- 156
- PR merge metrics
- No merged PRs in 30d
Description
[initial suggestion from @Lukasa]
AHC should support a "middleware" concept (ideally once it's a "3-tier library") where a user can supply some middlewares which do common jobs like following redirects, authenticating requests, etc.
Very rough API sketch
final class AuthenticationMiddleware {
// rough sketch, in reality returning just one modified request isn't good enough because with redirects we may
// need more.
func willPerformRequest(_ request: Request, http: HTTPService) -> EventLoopFuture<Request> {
http.client().get("auth/produce/me/an/auth/token").map { token in
var request = request
request.headers[.authorization] = "bearer \(token)"
return request
}
}
}
func makeTwoAuthenticatedHTTPRequests(http: HTTPService, eventLoop: EventLoop, logger: Logger) {
let client = http.client(tlsConfiguration: ...,
eventLoop: eventLoop,
logger: logger,
middlewares: [AuthenticationMiddleware.self])
return client.get("https://foo/bar").flatMap { first in
client.get("https://bar/buz").flatMap { second in
return (first, second)
}
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the linked three-tier library proposal in issue #392 and the existing HTTPService and client APIs. Use the authentication and redirect examples to clarify the middleware lifecycle and request-flow requirements. Done means an agreed middleware design that supports these use cases, rather than only the rough API sketch shown here.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100