<commit_before>//
//  DownloadHelper.swift
//  Pods
//
//  Created by <NAME> on 2017/7/28.
//
//

import Foundation

public class DownloadRequest: Request {
    
    public var method: String = "GET"
    private var _configuration: RequestConfiguration?
    public var configuration: RequestConfiguration {
        get {
            guard let config = _configuration else {
                return BaseRequestConfiguration.create(from: client.configuration, request: self)
            }
            return config
        }
        set {
            _configuration = newValue
        }
    }
    public var client: Client = BaseClient.shared
    public private(set) var uri: String
    
    public init(url: URL, savePath: String) {
        self.uri = url.relativeString
        self.configuration.baseURLString = url.baseURL?.absoluteString ?? ""
        self.configuration.decoder = HTTPResponseFileDecoder(filePath: savePath)
    }
    
    public private(set) var content: RequestContent?
    
    @discardableResult
    public func send(completion: @escaping (DownloadResponse) -> Void, errorHandler: @escaping (HIError) -> Void) -> RequestFuture<DownloadResponse> {
        let future: RequestFuture<DownloadResponse> = client.send(self)
        future.responseHandler = completion
        future.errorHandler = errorHandler
        return future
    }
    
    @discardableResult
    public func send(rawResponseHandler: @escaping (HTTPResponse) -> Void, errorHandler: @escaping (HIError) -> Void) -> RequestFuture<HTTPResponse> {
        let future = client.send(self)
        future.responseHandler = rawResponseHandler
        future.errorHandler = errorHandler
        return future
    }
}

public struct DownloadResponse: Response {
    
    public let body: HTTPFile?
    public let rawResponse: HTTPResponse
    public init(content: ResponseContent?, rawResponse: HTTPResponse) throws {
        self.rawResponse = rawResponse
        guard let content = content else {
            self.body = nil
            return
        }
        self.body = HTTPFile(content: content)
    }
}

extension String {
    public func download(toPath savePath: String, responseHandler: @escaping (HTTPResponse) -> Void, errorHandler: @escaping (HIError) -> Void) {
        let url = URL(string: self)!
        let req = DownloadRequest(url: url, savePath: savePath)
        req.send(rawResponseHandler: responseHandler, errorHandler: errorHandler)
    }
}

extension URL {
    public func download(toPath savePath: String, responseHandler: @escaping (HTTPResponse) -> Void, errorHandler: @escaping (HIError) -> Void) {
        let req = DownloadRequest(url: self, savePath: savePath)
        req.send(rawResponseHandler: responseHandler, errorHandler: errorHandler)
    }
}
<commit_msg>[ADD] download request entry<commit_after>//
//  DownloadHelper.swift
//  Pods
//
//  Created by <NAME> on 2017/7/28.
//
//

import Foundation

public class DownloadRequest: Request {
    
    public var method: String = "GET"
    private var _configuration: RequestConfiguration?
    public var configuration: RequestConfiguration {
        get {
            guard let config = _configuration else {
                return BaseRequestConfiguration.create(from: client.configuration, request: self)
            }
            return config
        }
        set {
            _configuration = newValue
        }
    }
    public var client: Client = BaseClient.shared
    public private(set) var uri: String
    
    public init(url: URL, savePath: String) {
        self.uri = url.relativeString
        self.configuration.baseURLString = url.baseURL?.absoluteString ?? ""
        self.configuration.decoder = HTTPResponseFileDecoder(filePath: savePath)
    }
    
    public private(set) var content: RequestContent?
    
    @discardableResult
    public func send(completion: @escaping (DownloadResponse) -> Void, errorHandler: @escaping (HIError) -> Void) -> RequestFuture<DownloadResponse> {
        let future: RequestFuture<DownloadResponse> = client.send(self)
        future.responseHandler = completion
        future.errorHandler = errorHandler
        return future
    }
    
    @discardableResult
    public func send(rawResponseHandler: @escaping (HTTPResponse) -> Void, errorHandler: @escaping (HIError) -> Void) -> RequestFuture<HTTPResponse> {
        let future = client.send(self)
        future.responseHandler = rawResponseHandler
        future.errorHandler = errorHandler
        return future
    }
}

public struct DownloadResponse: Response {
    
    public let body: HTTPFile?
    public let rawResponse: HTTPResponse
    public init(content: ResponseContent?, rawResponse: HTTPResponse) throws {
        self.rawResponse = rawResponse
        guard let content = content else {
            self.body = nil
            return
        }
        self.body = HTTPFile(content: content)
    }
}

extension String {
    public func download(toPath savePath: String, responseHandler: @escaping (HTTPResponse) -> Void, errorHandler: @escaping (HIError) -> Void) {
        let req = downloadRequest(toPath: savePath)
        req.send(rawResponseHandler: responseHandler, errorHandler: errorHandler)
    }
    public func downloadRequest(toPath savePath: String) -> DownloadRequest {
        let url = URL(string: self)!
        return DownloadRequest(url: url, savePath: savePath)
    }
}

extension URL {
    public func download(toPath savePath: String, responseHandler: @escaping (HTTPResponse) -> Void, errorHandler: @escaping (HIError) -> Void) {
        let req = downloadRequest(toPath: savePath)
        req.send(rawResponseHandler: responseHandler, errorHandler: errorHandler)
    }
    public func downloadRequest(toPath savePath: String) -> DownloadRequest {
        return DownloadRequest(url: self, savePath: savePath)
    }
}

{'fraction_non_alphanumeric': 0.0879685093274003, 'fraction_numerical': 0.002396029436933082, 'mean_word_length': 2.85997357992074, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6327246', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1498, 'n_words': 417}