<commit_before>//
//  Networking-Alamofire.swift
//  Siesta
//
//  Created by Paul on 2015/6/26.
//  Copyright © 2016 Bust Out Solutions. All rights reserved.
//

import Foundation
import Siesta
import Alamofire

/**
  Uses [Alamofire](https://github.com/Alamofire/Alamofire) for networking.

  You can create instances of this provider with a custom
  [Alamofire.Manager](http://cocoadocs.org/docsets/Alamofire/1.3.0/Classes/Manager.html)
  in order to control caching, certificate validation rules, etc. For example, here is a `Service` that will
  use a URLCache and will not use the cell network:

      class MyAPI: Service {
          init() {
              let configuration = URLSessionConfiguration.defaultSessionConfiguration()
              configuration.allowsCellularAccess = false
              super.init(
                  baseURL: "http://foo.bar/v1",
                  networking: AlamofireProvider(configuration: configuration))
          }
      }
*/
public struct AlamofireProvider: NetworkingProvider
    {
    public let manager: Alamofire.SessionManager

    public init(manager: Alamofire.SessionManager = SessionManager.default)
        { self.manager = manager }

    public init(configuration: URLSessionConfiguration)
        { self.init(manager: Alamofire.SessionManager(configuration: configuration)) }

    public func startRequest(
            _ request: URLRequest,
            completion: @escaping RequestNetworkingCompletionCallback)
        -> RequestNetworking
        {
        return AlamofireRequestNetworking(
            manager.request(request)
                .response { completion($0.response, $0.data, $0.error) })
        }
    }

internal struct AlamofireRequestNetworking: RequestNetworking, SessionTaskContainer
    {
    internal var alamofireRequest: Alamofire.Request

    init(_ alamofireRequest: Alamofire.Request)
        {
        self.alamofireRequest = alamofireRequest
        if case .suspended = alamofireRequest.task?.state ?? .suspended
            {
            alamofireRequest.resume()   // in case manager.startRequestsImmediately is false
            }
        }

    var task: URLSessionTask
        {
        return alamofireRequest.task!
        }

    func cancel()
        { alamofireRequest.cancel() }
    }

extension Alamofire.SessionManager: NetworkingProviderConvertible
    {
    /// You can pass an `AlamoFire.Manager` when creating a `Service`.
    public var siestaNetworkingProvider: NetworkingProvider
        { return AlamofireProvider(manager: self) }
    }<commit_msg>if task is nil, resume won't help :)<commit_after>//
//  Networking-Alamofire.swift
//  Siesta
//
//  Created by Paul on 2015/6/26.
//  Copyright © 2016 Bust Out Solutions. All rights reserved.
//

import Foundation
import Siesta
import Alamofire

/**
  Uses [Alamofire](https://github.com/Alamofire/Alamofire) for networking.

  You can create instances of this provider with a custom
  [Alamofire.Manager](http://cocoadocs.org/docsets/Alamofire/1.3.0/Classes/Manager.html)
  in order to control caching, certificate validation rules, etc. For example, here is a `Service` that will
  use a URLCache and will not use the cell network:

      class MyAPI: Service {
          init() {
              let configuration = URLSessionConfiguration.defaultSessionConfiguration()
              configuration.allowsCellularAccess = false
              super.init(
                  baseURL: "http://foo.bar/v1",
                  networking: AlamofireProvider(configuration: configuration))
          }
      }
*/
public struct AlamofireProvider: NetworkingProvider
    {
    public let manager: Alamofire.SessionManager

    public init(manager: Alamofire.SessionManager = SessionManager.default)
        { self.manager = manager }

    public init(configuration: URLSessionConfiguration)
        { self.init(manager: Alamofire.SessionManager(configuration: configuration)) }

    public func startRequest(
            _ request: URLRequest,
            completion: @escaping RequestNetworkingCompletionCallback)
        -> RequestNetworking
        {
        return AlamofireRequestNetworking(
            manager.request(request)
                .response { completion($0.response, $0.data, $0.error) })
        }
    }

internal struct AlamofireRequestNetworking: RequestNetworking, SessionTaskContainer
    {
    internal var alamofireRequest: Alamofire.Request

    init(_ alamofireRequest: Alamofire.Request)
        {
        self.alamofireRequest = alamofireRequest
        if let requestTask = alamofireRequest.task, case .suspended = requestTask.state
            {
            alamofireRequest.resume()   // in case manager.startRequestsImmediately is false
            }
        }

    var task: URLSessionTask
        {
        return alamofireRequest.task!
        }

    func cancel()
        { alamofireRequest.cancel() }
    }

extension Alamofire.SessionManager: NetworkingProviderConvertible
    {
    /// You can pass an `AlamoFire.Manager` when creating a `Service`.
    public var siestaNetworkingProvider: NetworkingProvider
        { return AlamofireProvider(manager: self) }
    }
{'fraction_non_alphanumeric': 0.08221834399844871, 'fraction_numerical': 0.006980802792321117, 'mean_word_length': 2.6400846859562455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2394143', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1387, 'n_words': 345}