-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathAuthenticationClientMiddleware.swift
42 lines (38 loc) · 1.52 KB
/
AuthenticationClientMiddleware.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIRuntime
import Foundation
import HTTPTypes
/// A client middleware that injects a value into the `Authorization` header field of the request.
package struct AuthenticationMiddleware {
/// The value for the `Authorization` header field.
private let value: String
/// Creates a new middleware.
/// - Parameter value: The value for the `Authorization` header field.
package init(authorizationHeaderFieldValue value: String) { self.value = value }
}
extension AuthenticationMiddleware: ClientMiddleware {
package func intercept(
_ request: HTTPRequest,
body: HTTPBody?,
baseURL: URL,
operationID: String,
next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
var request = request
// Adds the `Authorization` header field with the provided value.
request.headerFields[.authorization] = value
return try await next(request, body, baseURL)
}
}