1212//
1313//===----------------------------------------------------------------------===//
1414
15+ import Baggage
1516import Foundation
17+ import Instrumentation
1618import Logging
1719import NIO
1820import NIOConcurrencyHelpers
@@ -71,6 +73,7 @@ public class HTTPClient {
7173 private let stateLock = Lock ( )
7274
7375 internal static let loggingDisabled = Logger ( label: " AHC-do-not-log " , factory: { _ in SwiftLogNoOpLogHandler ( ) } )
76+ internal static let topLevelContextLoggingDisabled = DefaultLoggingContext ( logger: loggingDisabled, baggage: . topLevel)
7477
7578 /// Create an `HTTPClient` with specified `EventLoopGroup` provider and configuration.
7679 ///
@@ -227,7 +230,17 @@ public class HTTPClient {
227230 /// - url: Remote URL.
228231 /// - deadline: Point in time by which the request must complete.
229232 public func get( url: String , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
230- return self . get ( url: url, deadline: deadline, logger: HTTPClient . loggingDisabled)
233+ return self . get ( url: url, context: HTTPClient . topLevelContextLoggingDisabled, deadline: deadline)
234+ }
235+
236+ /// Execute `GET` request using specified URL.
237+ ///
238+ /// - parameters:
239+ /// - url: Remote URL.
240+ /// - context: The logging context carrying a logger and instrumentation metadata.
241+ /// - deadline: Point in time by which the request must complete.
242+ public func get( url: String , context: LoggingContext , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
243+ return self . execute ( . GET, url: url, context: context, deadline: deadline)
231244 }
232245
233246 /// Execute `GET` request using specified URL.
@@ -237,7 +250,7 @@ public class HTTPClient {
237250 /// - deadline: Point in time by which the request must complete.
238251 /// - logger: The logger to use for this request.
239252 public func get( url: String , deadline: NIODeadline ? = nil , logger: Logger ) -> EventLoopFuture < Response > {
240- return self . execute ( . GET , url: url, deadline : deadline , logger: logger)
253+ return self . get ( url: url, context : DefaultLoggingContext ( logger: logger, baggage : . topLevel ) , deadline : deadline )
241254 }
242255
243256 /// Execute `POST` request using specified URL.
@@ -247,7 +260,18 @@ public class HTTPClient {
247260 /// - body: Request body.
248261 /// - deadline: Point in time by which the request must complete.
249262 public func post( url: String , body: Body ? = nil , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
250- return self . post ( url: url, body: body, deadline: deadline, logger: HTTPClient . loggingDisabled)
263+ return self . post ( url: url, context: HTTPClient . topLevelContextLoggingDisabled, body: body, deadline: deadline)
264+ }
265+
266+ /// Execute `POST` request using specified URL.
267+ ///
268+ /// - parameters:
269+ /// - url: Remote URL.
270+ /// - context: The logging context carrying a logger and instrumentation metadata.
271+ /// - body: Request body.
272+ /// - deadline: Point in time by which the request must complete.
273+ public func post( url: String , context: LoggingContext , body: Body ? = nil , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
274+ return self . execute ( . POST, url: url, context: context, body: body, deadline: deadline)
251275 }
252276
253277 /// Execute `POST` request using specified URL.
@@ -258,7 +282,12 @@ public class HTTPClient {
258282 /// - deadline: Point in time by which the request must complete.
259283 /// - logger: The logger to use for this request.
260284 public func post( url: String , body: Body ? = nil , deadline: NIODeadline ? = nil , logger: Logger ) -> EventLoopFuture < Response > {
261- return self . execute ( . POST, url: url, body: body, deadline: deadline, logger: logger)
285+ return self . post (
286+ url: url,
287+ context: DefaultLoggingContext ( logger: logger, baggage: . topLevel) ,
288+ body: body,
289+ deadline: deadline
290+ )
262291 }
263292
264293 /// Execute `PATCH` request using specified URL.
@@ -268,7 +297,18 @@ public class HTTPClient {
268297 /// - body: Request body.
269298 /// - deadline: Point in time by which the request must complete.
270299 public func patch( url: String , body: Body ? = nil , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
271- return self . patch ( url: url, body: body, deadline: deadline, logger: HTTPClient . loggingDisabled)
300+ return self . patch ( url: url, context: HTTPClient . topLevelContextLoggingDisabled, body: body, deadline: deadline)
301+ }
302+
303+ /// Execute `PATCH` request using specified URL.
304+ ///
305+ /// - parameters:
306+ /// - url: Remote URL.
307+ /// - context: The logging context carrying a logger and instrumentation metadata.
308+ /// - body: Request body.
309+ /// - deadline: Point in time by which the request must complete.
310+ public func patch( url: String , context: LoggingContext , body: Body ? = nil , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
311+ return self . execute ( . PATCH, url: url, context: context, body: body, deadline: deadline)
272312 }
273313
274314 /// Execute `PATCH` request using specified URL.
@@ -279,7 +319,12 @@ public class HTTPClient {
279319 /// - deadline: Point in time by which the request must complete.
280320 /// - logger: The logger to use for this request.
281321 public func patch( url: String , body: Body ? = nil , deadline: NIODeadline ? = nil , logger: Logger ) -> EventLoopFuture < Response > {
282- return self . execute ( . PATCH, url: url, body: body, deadline: deadline, logger: logger)
322+ return self . patch (
323+ url: url,
324+ context: DefaultLoggingContext ( logger: logger, baggage: . topLevel) ,
325+ body: body,
326+ deadline: deadline
327+ )
283328 }
284329
285330 /// Execute `PUT` request using specified URL.
@@ -289,7 +334,18 @@ public class HTTPClient {
289334 /// - body: Request body.
290335 /// - deadline: Point in time by which the request must complete.
291336 public func put( url: String , body: Body ? = nil , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
292- return self . put ( url: url, body: body, deadline: deadline, logger: HTTPClient . loggingDisabled)
337+ return self . put ( url: url, context: HTTPClient . topLevelContextLoggingDisabled, body: body, deadline: deadline)
338+ }
339+
340+ /// Execute `PUT` request using specified URL.
341+ ///
342+ /// - parameters:
343+ /// - url: Remote URL.
344+ /// - context: The logging context carrying a logger and instrumentation metadata.
345+ /// - body: Request body.
346+ /// - deadline: Point in time by which the request must complete.
347+ public func put( url: String , context: LoggingContext , body: Body ? = nil , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
348+ return self . execute ( . PUT, url: url, context: context, body: body, deadline: deadline)
293349 }
294350
295351 /// Execute `PUT` request using specified URL.
@@ -300,7 +356,12 @@ public class HTTPClient {
300356 /// - deadline: Point in time by which the request must complete.
301357 /// - logger: The logger to use for this request.
302358 public func put( url: String , body: Body ? = nil , deadline: NIODeadline ? = nil , logger: Logger ) -> EventLoopFuture < Response > {
303- return self . execute ( . PUT, url: url, body: body, deadline: deadline, logger: logger)
359+ return self . put (
360+ url: url,
361+ context: DefaultLoggingContext ( logger: logger, baggage: . topLevel) ,
362+ body: body,
363+ deadline: deadline
364+ )
304365 }
305366
306367 /// Execute `DELETE` request using specified URL.
@@ -309,7 +370,17 @@ public class HTTPClient {
309370 /// - url: Remote URL.
310371 /// - deadline: The time when the request must have been completed by.
311372 public func delete( url: String , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
312- return self . delete ( url: url, deadline: deadline, logger: HTTPClient . loggingDisabled)
373+ return self . delete ( url: url, context: HTTPClient . topLevelContextLoggingDisabled, deadline: deadline)
374+ }
375+
376+ /// Execute `DELETE` request using specified URL.
377+ ///
378+ /// - parameters:
379+ /// - url: Remote URL.
380+ /// - context: The logging context carrying a logger and instrumentation metadata.
381+ /// - deadline: Point in time by which the request must complete.
382+ public func delete( url: String , context: LoggingContext , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
383+ return self . execute ( . DELETE, url: url, context: context, deadline: deadline)
313384 }
314385
315386 /// Execute `DELETE` request using specified URL.
@@ -319,7 +390,24 @@ public class HTTPClient {
319390 /// - deadline: The time when the request must have been completed by.
320391 /// - logger: The logger to use for this request.
321392 public func delete( url: String , deadline: NIODeadline ? = nil , logger: Logger ) -> EventLoopFuture < Response > {
322- return self . execute ( . DELETE, url: url, deadline: deadline, logger: logger)
393+ return self . delete ( url: url, context: DefaultLoggingContext ( logger: logger, baggage: . topLevel) , deadline: deadline)
394+ }
395+
396+ /// Execute arbitrary HTTP request using specified URL.
397+ ///
398+ /// - parameters:
399+ /// - method: Request method.
400+ /// - url: Request url.
401+ /// - body: Request body.
402+ /// - deadline: Point in time by which the request must complete.
403+ /// - context: The logging context carrying a logger and instrumentation metadata.
404+ public func execute( _ method: HTTPMethod = . GET, url: String , context: LoggingContext ? , body: Body ? = nil , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
405+ do {
406+ let request = try Request ( url: url, method: method, body: body)
407+ return self . execute ( request: request, context: context, deadline: deadline)
408+ } catch {
409+ return self . eventLoopGroup. next ( ) . makeFailedFuture ( error)
410+ }
323411 }
324412
325413 /// Execute arbitrary HTTP request using specified URL.
@@ -390,6 +478,17 @@ public class HTTPClient {
390478 return self . execute ( request: request, deadline: deadline, logger: HTTPClient . loggingDisabled)
391479 }
392480
481+ /// Execute arbitrary HTTP request using specified URL.
482+ ///
483+ /// - parameters:
484+ /// - request: HTTP request to execute.
485+ /// - context: The logging context carrying a logger and instrumentation metadata.
486+ /// - deadline: Point in time by which the request must complete.
487+ public func execute( request: Request , context: LoggingContext ? , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
488+ let accumulator = ResponseAccumulator ( request: request)
489+ return self . execute ( request: request, delegate: accumulator, context: context, deadline: deadline) . futureResult
490+ }
491+
393492 /// Execute arbitrary HTTP request using specified URL.
394493 ///
395494 /// - parameters:
@@ -441,6 +540,20 @@ public class HTTPClient {
441540 return self . execute ( request: request, delegate: delegate, deadline: deadline, logger: HTTPClient . loggingDisabled)
442541 }
443542
543+ /// Execute arbitrary HTTP request and handle response processing using provided delegate.
544+ ///
545+ /// - parameters:
546+ /// - request: HTTP request to execute.
547+ /// - delegate: Delegate to process response parts.
548+ /// - deadline: Point in time by which the request must complete.
549+ /// - context: The logging context carrying a logger and instrumentation metadata.
550+ public func execute< Delegate: HTTPClientResponseDelegate > ( request: Request ,
551+ delegate: Delegate ,
552+ context: LoggingContext ? ,
553+ deadline: NIODeadline ? = nil ) -> Task < Delegate . Response > {
554+ return self . execute ( request: request, delegate: delegate, eventLoop: . indifferent, context: context, deadline: deadline)
555+ }
556+
444557 /// Execute arbitrary HTTP request and handle response processing using provided delegate.
445558 ///
446559 /// - parameters:
@@ -474,6 +587,30 @@ public class HTTPClient {
474587 logger: HTTPClient . loggingDisabled)
475588 }
476589
590+ /// Execute arbitrary HTTP request and handle response processing using provided delegate.
591+ ///
592+ /// - parameters:
593+ /// - request: HTTP request to execute.
594+ /// - delegate: Delegate to process response parts.
595+ /// - eventLoop: NIO Event Loop preference.
596+ /// - deadline: Point in time by which the request must complete.
597+ /// - context: The logging context carrying a logger and instrumentation metadata.
598+ public func execute< Delegate: HTTPClientResponseDelegate > ( request: Request ,
599+ delegate: Delegate ,
600+ eventLoop eventLoopPreference: EventLoopPreference ,
601+ context: LoggingContext ? ,
602+ deadline: NIODeadline ? = nil ) -> Task < Delegate . Response > {
603+ var request = request
604+ if let baggage = context? . baggage {
605+ InstrumentationSystem . instrument. inject ( baggage, into: & request. headers, using: HTTPHeadersInjector ( ) )
606+ }
607+ return self . execute ( request: request,
608+ delegate: delegate,
609+ eventLoop: eventLoopPreference,
610+ deadline: deadline,
611+ logger: context? . logger)
612+ }
613+
477614 /// Execute arbitrary HTTP request and handle response processing using provided delegate.
478615 ///
479616 /// - parameters:
0 commit comments