Skip to content

support middlewares #393

Open
Open
@weissi

Description

@weissi

[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)
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/enhancementImprovements to existing feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions