File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -113,7 +113,52 @@ struct GitHubRestAPIIssuesExtension {
113113}
114114```
115115</details >
116+
117+ <details >
118+ <summary >Example of code for the `GITHUB_TOKEN` to authenticate.</summary >
116119
120+ ``` swift
121+ import Foundation
122+ import GitHubRestAPIUsers
123+ import OpenAPIRuntime
124+ import OpenAPIURLSession
125+ import HTTPTypes
126+
127+ /// Example: ProcessInfo.processInfo.environment["GITHUB_TOKEN"] ?? ""
128+ let token: String = " ***"
129+
130+ let client = Client (
131+ serverURL : try Servers.server1 (),
132+ transport : URLSessionTransport (),
133+ middlewares : [AuthenticationMiddleware (token : token)]
134+ )
135+
136+ /// Injects an authorization header to every request.
137+ struct AuthenticationMiddleware : ClientMiddleware {
138+
139+ private let token: String
140+
141+ init (token : String ) {
142+ self .token = token
143+ }
144+ private var header: [String : String ] { [" Authorization" : " Bearer \( token ) " ] }
145+
146+ func intercept (
147+ _ request : HTTPRequest,
148+ body : HTTPBody? ,
149+ baseURL : URL,
150+ operationID : String ,
151+ next : @Sendable (HTTPRequest, HTTPBody? , URL) async throws -> (HTTPResponse, HTTPBody? )
152+ ) async throws -> (HTTPResponse, HTTPBody? ) {
153+ var request = request
154+ request.headerFields .append (HTTPField (name : .authorization , value : " Bearer \( token ) " ))
155+ return try await next (request, body, baseURL)
156+ }
157+
158+ }
159+ ```
160+ </details >
161+
117162## Installation
118163
119164### Swift Package Manager
You can’t perform that action at this time.
0 commit comments