<commit_before>//
//  Networker.swift
//  Open Souq Assignment
//
//  Created by <NAME> on 11/4/15.
//  Copyright © 2015 Mamouneyya. All rights reserved.
//

import UIKit
import Alamofire
import AlamofireImage

class Networker {

    // MARK: - Public Vars
    
    /// Singleton's class shared instance
    static let sharedInstance = Networker()

    /// Internet connection reachability
    static var isReachable: Bool {
        if let reachability = Networker.sharedInstance.reachability {
            return reachability.isReachable
        } else {
            print("Unable to create Reachability")
            return false
        }
    }

    // MARK: - Private Vars
    
    /// Internal Alamofire's reachability manager
    private let reachability = NetworkReachabilityManager()
    /// Internal Alamofire's request manager
    private let manager = Alamofire.Manager()
    /// Internal Alamofire's image downloader
    private let imageDownloader = ImageDownloader(
        configuration: ImageDownloader.defaultURLSessionConfiguration(),
        downloadPrioritization: .LIFO,
        maximumActiveDownloads: 4,
        imageCache: AutoPurgingImageCache()
    )

    // MARK: - Lifecycle
    
    private init() {
        
    }
    
    // MARK: - Request Methods

    /**
        Creates a request for the specified URL request.
        
        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
        
        - parameter URLRequest: The URL request
        
        - returns: The created request.
    */
    class func request(URLRequest: URLRequestConvertible) -> Request {
        return Networker.sharedInstance.manager.request(URLRequest)
    }
    
    /**
         Creates a request for the specified request configurations.
         
         If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
         
         - parameter requestConfigurations: The request configurations
         
         - returns: The created request.
     */
    class func request(requestConfigurations: RequestConfigurations) -> Request {
        return Networker.sharedInstance.manager.request(Router(requestConfigurations))
    }
    
    /**
         Creates a request for the specified method, URL string, parameters, parameter encoding and headers.
         
         - parameter method:     The HTTP method.
         - parameter URLString:  The URL string.
         - parameter parameters: The parameters. `nil` by default.
         - parameter encoding:   The parameter encoding. `.URL` by default.
         - parameter headers:    The HTTP headers. `nil` by default.
         
         - returns: The created request.
     */
    class func request(
        method: Alamofire.Method,
        _ URLString: URLStringConvertible,
        parameters: [String: AnyObject]? = nil,
        encoding: ParameterEncoding = .JSON,
        headers: [String: String]? = nil)
        -> Request
    {
        return Networker.sharedInstance.manager.request(
            method,
            URLString,
            parameters: parameters,
            encoding: encoding,
            headers: headers
            )
    }
    
    /**<commit_msg>Add automatic validation for all Networker's requests.<commit_after>//
//  Networker.swift
//  Open Souq Assignment
//
//  Created by <NAME> on 11/4/15.
//  Copyright © 2015 Mamouneyya. All rights reserved.
//

import UIKit

class Networker {

    // MARK: - Public Vars
    
    /// Singleton's class shared instance
    static let sharedInstance = Networker()

    /// Internet connection reachability
    static var isReachable: Bool {
        if let reachability = Networker.sharedInstance.reachability {
            return reachability.isReachable
        } else {
            print("Unable to create Reachability")
            return false
        }
    }

    // MARK: - Private Vars
    
    /// Internal Alamofire's reachability manager
    private let reachability = NetworkReachabilityManager()
    /// Internal Alamofire's request manager
    private let manager = Alamofire.Manager()
    /// Internal Alamofire's image downloader
    private let imageDownloader = ImageDownloader(
        configuration: ImageDownloader.defaultURLSessionConfiguration(),
        downloadPrioritization: .LIFO,
        maximumActiveDownloads: 4,
        imageCache: AutoPurgingImageCache()
    )

    // MARK: - Lifecycle
    
    private init() {
        
    }
    
    // MARK: - Requests

    /**
        Creates a request for the specified URL request.
        
        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
        
        - parameter URLRequest: The URL request
        
        - returns: The created request.
    */
    class func request(URLRequest: URLRequestConvertible) -> Request {
        return Networker.sharedInstance.manager
            .request(URLRequest)
            .validate()
    }
    
    /**
         Creates a request for the specified request configurations.
         
         If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
         
         - parameter requestConfigurations: The request configurations
         
         - returns: The created request.
     */
    class func request(requestConfigurations: RequestConfigurations) -> Request {
        return Networker.sharedInstance.manager
            .request(Router(requestConfigurations))
            .validate()
    }
    
    /**
         Creates a request for the specified method, URL string, parameters, parameter encoding and headers.
         
         - parameter method:     The HTTP method.
         - parameter URLString:  The URL string.
         - parameter parameters: The parameters. `nil` by default.
         - parameter encoding:   The parameter encoding. `.URL` by default.
         - parameter headers:    The HTTP headers. `nil` by default.
         
         - returns: The created request.
     */
    class func request(
        method: Alamofire.Method,
        _ URLString: URLStringConvertible,
        parameters: [String: AnyObject]? = nil,
        encoding: ParameterEncoding = .JSON,
        headers: [String: String]? = nil)
        -> Request
    {
        return Networker.sharedInstance.manager.request(
            method,
            URLString,
            parameters: parameters,
            encoding: encoding,
            headers: headers)
            .validate()
        
    }
    
    // MARK: - Downloads
    
    /**
{'fraction_non_alphanumeric': 0.07934109112891038, 'fraction_numerical': 0.003022517757291824, 'mean_word_length': 2.0150341685649202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3623401', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1626, 'n_words': 512}