@@ -220,13 +220,16 @@ class HTTPClient {
220220 headers: HTTPHeaders ,
221221 encoding: ParameterEncoding ,
222222 parameters: [ String : Any ] ? ,
223- completionDispatchQueue : DispatchQueue ,
223+ completionQueue : DispatchQueue ,
224224 completionHandler: @escaping ( LCResponse ) -> Void )
225225 {
226226 do {
227- var request = try URLRequest ( url: url, method: method, headers: headers)
227+ var request = try URLRequest (
228+ url: url,
229+ method: method,
230+ headers: headers)
228231 request = try encoding. encode ( request, with: parameters)
229- completionDispatchQueue . sync {
232+ completionQueue . sync {
230233 guard let cachedResponse = self . urlCache? . cachedResponse ( for: request) ,
231234 let httpResponse = cachedResponse. response as? HTTPURLResponse else {
232235 completionHandler ( self . response (
@@ -236,7 +239,7 @@ class HTTPClient {
236239 return
237240 }
238241 let result = Result {
239- try JSONResponseSerializer ( options : . allowFragments )
242+ try JSONResponseSerializer ( )
240243 . serialize (
241244 request: request,
242245 response: httpResponse,
@@ -255,9 +258,10 @@ class HTTPClient {
255258 completionHandler ( response)
256259 }
257260 } catch {
258- completionDispatchQueue . async {
261+ completionQueue . async {
259262 completionHandler ( self . response (
260- with: LCError ( error: error) ) )
263+ with: LCError (
264+ error: error) ) )
261265 }
262266 }
263267 }
@@ -268,23 +272,21 @@ class HTTPClient {
268272 parameters: [ String : Any ] ? = nil ,
269273 headers: [ String : String ] ? = nil ,
270274 cachePolicy: LCQuery . CachePolicy = . onlyNetwork,
271- completionDispatchQueue : DispatchQueue ? = nil ,
275+ completionQueue : DispatchQueue ? = nil ,
272276 completionHandler: @escaping ( LCResponse ) -> Void )
273277 -> LCRequest
274278 {
275- let completionDispatchQueue = completionDispatchQueue
279+ let completionQueue = completionQueue
276280 ?? self . defaultCompletionConcurrentQueue
277-
278281 guard let url = self . application. appRouter. route ( path: path) else {
279- completionDispatchQueue . sync {
282+ completionQueue . sync {
280283 completionHandler ( self . response (
281284 with: LCError (
282285 code: . notFound,
283286 reason: " URL not found. " ) ) )
284287 }
285288 return LCSingleRequest ( request: nil )
286289 }
287-
288290 let method = method. alamofireMethod
289291 let headers = HTTPHeaders ( mergeCommonHeaders ( headers) )
290292 let encoding : ParameterEncoding
@@ -294,23 +296,25 @@ class HTTPClient {
294296 default :
295297 encoding = JSONEncoding . default
296298 }
297-
298299 let requestCachedResponse : ( ) -> Void = {
299300 self . requestCache (
300301 url: url,
301302 method: method,
302303 headers: headers,
303304 encoding: encoding,
304305 parameters: parameters,
305- completionDispatchQueue : completionDispatchQueue ,
306+ completionQueue : completionQueue ,
306307 completionHandler: completionHandler)
307308 }
308-
309309 switch cachePolicy {
310310 case . onlyNetwork, . networkElseCache:
311- let request = session. request ( url, method: method, parameters: parameters, encoding: encoding, headers: headers) . validate ( )
311+ let request = self . session. request (
312+ url, method: method,
313+ parameters: parameters,
314+ encoding: encoding,
315+ headers: headers) . validate ( )
312316 request. lcDebugDescription ( )
313- request. responseJSON ( queue: completionDispatchQueue ) { afResponse in
317+ request. responseJSON ( queue: completionQueue ) { afResponse in
314318 afResponse. lcDebugDescription ( request: request)
315319 let response = LCResponse (
316320 application: self . application,
@@ -331,82 +335,67 @@ class HTTPClient {
331335 return LCSingleRequest ( request: nil )
332336 }
333337 }
334-
335- /**
336- Creates a request to REST API and sends it asynchronously.
337-
338- - parameter url: The absolute URL.
339- - parameter method: The HTTP Method.
340- - parameter parameters: The request parameters.
341- - parameter headers: The request headers.
342- - parameter completionDispatchQueue: The dispatch queue in which the completion handler will be called. By default, it's a concurrent queue.
343- - parameter completionHandler: The completion callback closure.
344-
345- - returns: A request object.
346- */
338+
347339 func request(
348340 url: URL ,
349341 method: Method ,
350342 parameters: [ String : Any ] ? = nil ,
351343 headers: [ String : String ] ? = nil ,
352- completionDispatchQueue : DispatchQueue ? = nil ,
344+ completionQueue : DispatchQueue ? = nil ,
353345 completionHandler: @escaping ( LCResponse ) -> Void )
354346 -> LCRequest
355347 {
356- let method = method. alamofireMethod
357- let headers = HTTPHeaders ( mergeCommonHeaders ( headers) )
358- var encoding : ParameterEncoding !
359-
348+ let method = method. alamofireMethod
349+ let headers = HTTPHeaders ( mergeCommonHeaders ( headers) )
350+ let encoding : ParameterEncoding
360351 switch method {
361- case . get: encoding = URLEncoding . default
362- default : encoding = JSONEncoding . default
352+ case . get:
353+ encoding = URLEncoding . default
354+ default :
355+ encoding = JSONEncoding . default
363356 }
364-
365- let request = session. request ( url, method: method, parameters: parameters, encoding: encoding, headers: headers) . validate ( )
366-
367- let completionDispatchQueue = completionDispatchQueue
368- ?? self . defaultCompletionConcurrentQueue
369-
357+ let request = self . session. request (
358+ url, method: method,
359+ parameters: parameters,
360+ encoding: encoding,
361+ headers: headers) . validate ( )
370362 request. lcDebugDescription ( )
371- request. responseJSON ( queue: completionDispatchQueue) { afResponse in
363+ request. responseJSON (
364+ queue: completionQueue ?? self . defaultCompletionConcurrentQueue)
365+ { afResponse in
372366 afResponse. lcDebugDescription ( request: request)
373- completionHandler ( LCResponse ( application: self . application, afDataResponse: afResponse) )
367+ completionHandler ( LCResponse (
368+ application: self . application,
369+ afDataResponse: afResponse) )
374370 }
375-
376371 return LCSingleRequest ( request: request)
377372 }
378-
379- /**
380- Create request for error.
381-
382- - parameter error: The error object.
383- - parameter completionDispatchQueue: The dispatch queue in which the completion handler will be called. By default, it's a concurrent queue.
384- - parameter completionHandler: The completion callback closure.
385-
386- - returns: A request object.
387- */
373+
388374 func request< T: LCResultType > (
389375 error: Error ,
390- completionDispatchQueue: DispatchQueue ? = nil ,
391- completionHandler: @escaping ( T ) -> Void ) -> LCRequest
376+ completionQueue: DispatchQueue ? = nil ,
377+ completionHandler: @escaping ( T ) -> Void )
378+ -> LCRequest
392379 {
393- return request ( object: error, completionDispatchQueue: completionDispatchQueue) { error in
394- completionHandler ( T ( error: LCError ( error: error) ) )
380+ return self . request (
381+ object: error,
382+ completionQueue: completionQueue)
383+ { error in
384+ completionHandler ( T (
385+ error: LCError (
386+ error: error) ) )
395387 }
396388 }
397-
389+
398390 func request< T> (
399391 object: T ,
400- completionDispatchQueue: DispatchQueue ? = nil ,
401- completionHandler: @escaping ( T ) -> Void ) -> LCRequest
392+ completionQueue: DispatchQueue ? = nil ,
393+ completionHandler: @escaping ( T ) -> Void )
394+ -> LCRequest
402395 {
403- let completionDispatchQueue = completionDispatchQueue
404- ?? self . defaultCompletionConcurrentQueue
405-
406- completionDispatchQueue. async {
396+ ( completionQueue ?? self . defaultCompletionConcurrentQueue) . async {
407397 completionHandler ( object)
408398 }
409-
410399 return LCSingleRequest ( request: nil )
411400 }
412401}
0 commit comments