@@ -172,6 +172,17 @@ public class HTTPClient {
172172 return self . execute ( request: request, delegate: accumulator, deadline: deadline) . futureResult
173173 }
174174
175+ /// Execute arbitrary HTTP request using specified URL.
176+ ///
177+ /// - parameters:
178+ /// - request: HTTP request to execute.
179+ /// - eventLoop: NIO Event Loop preference.
180+ /// - deadline: Point in time by which the request must complete.
181+ public func execute( request: Request , eventLoop: EventLoopPreference , deadline: NIODeadline ? = nil ) -> EventLoopFuture < Response > {
182+ let accumulator = ResponseAccumulator ( request: request)
183+ return self . execute ( request: request, delegate: accumulator, eventLoop: eventLoop, deadline: deadline) . futureResult
184+ }
185+
175186 /// Execute arbitrary HTTP request and handle response processing using provided delegate.
176187 ///
177188 /// - parameters:
@@ -180,7 +191,27 @@ public class HTTPClient {
180191 /// - deadline: Point in time by which the request must complete.
181192 public func execute< T: HTTPClientResponseDelegate > ( request: Request , delegate: T , deadline: NIODeadline ? = nil ) -> Task < T . Response > {
182193 let eventLoop = self . eventLoopGroup. next ( )
194+ return self . execute ( request: request, delegate: delegate, eventLoop: eventLoop, deadline: deadline)
195+ }
196+
197+ /// Execute arbitrary HTTP request and handle response processing using provided delegate.
198+ ///
199+ /// - parameters:
200+ /// - request: HTTP request to execute.
201+ /// - delegate: Delegate to process response parts.
202+ /// - eventLoop: NIO Event Loop preference.
203+ /// - deadline: Point in time by which the request must complete.
204+ public func execute< T: HTTPClientResponseDelegate > ( request: Request , delegate: T , eventLoop: EventLoopPreference , deadline: NIODeadline ? = nil ) -> Task < T . Response > {
205+ switch eventLoop. preference {
206+ case . indifferent:
207+ return self . execute ( request: request, delegate: delegate, eventLoop: self . eventLoopGroup. next ( ) , deadline: deadline)
208+ case . prefers( let preferred) :
209+ precondition ( self . eventLoopGroup. makeIterator ( ) . contains { $0 === preferred } , " Provided EventLoop must be part of clients EventLoopGroup. " )
210+ return self . execute ( request: request, delegate: delegate, eventLoop: preferred, deadline: deadline)
211+ }
212+ }
183213
214+ private func execute< T: HTTPClientResponseDelegate > ( request: Request , delegate: T , eventLoop: EventLoop , deadline: NIODeadline ? = nil ) -> Task < T . Response > {
184215 let redirectHandler : RedirectHandler < T . Response > ?
185216 if self . configuration. followRedirects {
186217 redirectHandler = RedirectHandler < T . Response > ( request: request) { newRequest in
@@ -312,6 +343,29 @@ public class HTTPClient {
312343 case createNew
313344 }
314345
346+ /// Specifies how the library will treat event loop passed by the user.
347+ public struct EventLoopPreference {
348+ enum Preference {
349+ /// Event Loop will be selected by the library.
350+ case indifferent
351+ /// Library will try to use provided event loop if possible.
352+ case prefers( EventLoop )
353+ }
354+
355+ var preference : Preference
356+
357+ init ( _ preference: Preference ) {
358+ self . preference = preference
359+ }
360+
361+ /// Event Loop will be selected by the library.
362+ public static let indifferent = EventLoopPreference ( . indifferent)
363+ /// Library will try to use provided event loop if possible.
364+ public static func prefers( _ eventLoop: EventLoop ) -> EventLoopPreference {
365+ return EventLoopPreference ( . prefers( eventLoop) )
366+ }
367+ }
368+
315369 /// Timeout configuration
316370 public struct Timeout {
317371 /// Specifies connect timeout.
0 commit comments