<commit_before>import Foundation
import Inquiline
import Requests

public class HTTPSessionConfiguration {
  public var HTTPAdditionalHeaders = [String:String]()

  public class func defaultSessionConfiguration() -> HTTPSessionConfiguration {
    return HTTPSessionConfiguration()
  }
}

public typealias HTTPCompletionFunc = ((NSData?, HTTPURLResponse?, NSError?) -> Void)

public class HTTPSessionDataTask {
  let completion: HTTPCompletionFunc
  let configuration: HTTPSessionConfiguration
  let URL: NSURL

  private init(configuration: HTTPSessionConfiguration, URL: NSURL, completion: HTTPCompletionFunc) {
    self.completion = completion
    self.configuration = configuration
    self.URL = URL
  }

  private func makeError(message: String? = nil) -> NSError {
    #if os(Linux)
    var userInfo = [String:Any]()
    #else
    var userInfo = [String:String]()
    #endif

    if let message = message {
      userInfo[NSLocalizedDescriptionKey] = message
    }
    return NSError(domain:"org.vu0.RequestSession", code: 23, userInfo: userInfo)
  }

  private func perform() {
#if os(Linux)
    let urlString = URL.absoluteString!
#else
    let urlString = URL.absoluteString
#endif

    let headers = configuration.HTTPAdditionalHeaders.map { ($0.0, $0.1) }
    var response: Response?

    do {
      response = try get(urlString, headers: headers)
    } catch {
      completion(nil, nil, makeError(String(error)))
      return
    }

    if let response = response, body = response.body {
      let urlResponse = HTTPURLResponse(URL: URL, statusCode: 200,
        HTTPVersion: "1.1", headerFields: nil)
      completion(body.dataUsingEncoding(NSUTF8StringEncoding), urlResponse, nil)
    } else {
      completion(nil, nil, makeError("Empty HTTP response body."))
    }
  }

  public func resume() {
    async { self.perform() }
  }
}

public class HTTPSession {
  let configuration: HTTPSessionConfiguration

  public init(configuration: HTTPSessionConfiguration) {
    self.configuration = configuration
  }

  public func dataTaskWithURL(url: NSURL, completion: HTTPCompletionFunc) -> HTTPSessionDataTask {
    return HTTPSessionDataTask(configuration: configuration, URL: url, completion: completion)
  }
}

public class HTTPURLResponse {
  let HTTPVersion: String

  public let allHeaderFields: [NSObject:AnyObject]
  public let statusCode: Int
  public let URL: NSURL

  public init(URL: NSURL, statusCode: Int, HTTPVersion: String, headerFields: [NSObject:AnyObject]?) {
    self.URL = URL
    self.statusCode = statusCode
    self.HTTPVersion = HTTPVersion
    self.allHeaderFields = headerFields ?? [NSObject:AnyObject]()
  }
}
<commit_msg>Increase robustness of `NSData` conversion<commit_after>import Foundation
import Inquiline
import Requests

public class HTTPSessionConfiguration {
  public var HTTPAdditionalHeaders = [String:String]()

  public class func defaultSessionConfiguration() -> HTTPSessionConfiguration {
    return HTTPSessionConfiguration()
  }
}

public typealias HTTPCompletionFunc = ((NSData?, HTTPURLResponse?, NSError?) -> Void)

public class HTTPSessionDataTask {
  let completion: HTTPCompletionFunc
  let configuration: HTTPSessionConfiguration
  let URL: NSURL

  private init(configuration: HTTPSessionConfiguration, URL: NSURL, completion: HTTPCompletionFunc) {
    self.completion = completion
    self.configuration = configuration
    self.URL = URL
  }

  private func makeError(message: String? = nil) -> NSError {
    #if os(Linux)
    var userInfo = [String:Any]()
    #else
    var userInfo = [String:String]()
    #endif

    if let message = message {
      userInfo[NSLocalizedDescriptionKey] = message
    }
    return NSError(domain:"org.vu0.RequestSession", code: 23, userInfo: userInfo)
  }

  private func perform() {
#if os(Linux)
    let urlString = URL.absoluteString!
#else
    let urlString = URL.absoluteString
#endif

    let headers = configuration.HTTPAdditionalHeaders.map { ($0.0, $0.1) }
    var response: Response?

    do {
      response = try get(urlString, headers: headers)
    } catch {
      completion(nil, nil, makeError(String(error)))
      return
    }

    if let response = response, body = response.body {
      let urlResponse = HTTPURLResponse(URL: URL, statusCode: 200,
        HTTPVersion: "1.1", headerFields: nil)
      if let body = NSString(string: body).dataUsingEncoding(NSUTF8StringEncoding) {
        completion(body, urlResponse, nil)
      } else {
        completion(nil, urlResponse, makeError("NSData conversion failed: \(body)"))
      }
    } else {
      completion(nil, nil, makeError("Empty HTTP response body."))
    }
  }

  public func resume() {
    async { self.perform() }
  }
}

public class HTTPSession {
  let configuration: HTTPSessionConfiguration

  public init(configuration: HTTPSessionConfiguration) {
    self.configuration = configuration
  }

  public func dataTaskWithURL(url: NSURL, completion: HTTPCompletionFunc) -> HTTPSessionDataTask {
    return HTTPSessionDataTask(configuration: configuration, URL: url, completion: completion)
  }
}

public class HTTPURLResponse {
  let HTTPVersion: String

  public let allHeaderFields: [NSObject:AnyObject]
  public let statusCode: Int
  public let URL: NSURL

  public init(URL: NSURL, statusCode: Int, HTTPVersion: String, headerFields: [NSObject:AnyObject]?) {
    self.URL = URL
    self.statusCode = statusCode
    self.HTTPVersion = HTTPVersion
    self.allHeaderFields = headerFields ?? [NSObject:AnyObject]()
  }
}

{'fraction_non_alphanumeric': 0.09480566654558663, 'fraction_numerical': 0.00472212132219397, 'mean_word_length': 3.974706413730804, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544010', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1538, 'n_words': 424}