<commit_before>import Foundation
import Swish

private let baseUrl = NSURL(string: "https://passport.thegrid.io/")!

func createUrl(path: String, _ queryItems: [String: String] = [:]) -> NSURL? {
    guard let url = NSURL(string: path, relativeToURL: baseUrl),
        components = NSURLComponents(URL: url, resolvingAgainstBaseURL: true)
    else {
        return .None
    }
    
    let queryItems = queryItems
        .map { NSURLQueryItem(name: $0, value: $1) }
        .sort { $0.name < $1.name }
    
    components.queryItems = queryItems.isEmpty ? nil : queryItems
    return components.URL
}

func createRequest(method: RequestMethod, _ path: String, token: String) -> NSMutableURLRequest {
    let request = createRequest(method, path)
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    return request
}

func createRequest(method: RequestMethod, _ path: String, body: [String: AnyObject] = [:]) -> NSMutableURLRequest {
    let url = createUrl(path)!
    let request = NSMutableURLRequest(URL: url)
    
    request.HTTPMethod = method.rawValue
    request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions())
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("gzip;q=1.0,compress;q=0.5", forHTTPHeaderField: "Accept-Encoding")
    
    return request
}<commit_msg>Don't set empty JSON body to prevent "connection was lost" error. * Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."<commit_after>import Foundation
import Swish

private let baseUrl = NSURL(string: "https://passport.thegrid.io/")!

func createUrl(path: String, _ queryItems: [String: String] = [:]) -> NSURL? {
    guard let url = NSURL(string: path, relativeToURL: baseUrl),
        components = NSURLComponents(URL: url, resolvingAgainstBaseURL: true)
    else {
        return .None
    }
    
    let queryItems = queryItems
        .map { NSURLQueryItem(name: $0, value: $1) }
        .sort { $0.name < $1.name }
    
    components.queryItems = queryItems.isEmpty ? nil : queryItems
    return components.URL
}

func createRequest(method: RequestMethod, _ path: String, token: String) -> NSMutableURLRequest {
    let request = createRequest(method, path)
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    return request
}

func createRequest(method: RequestMethod, _ path: String, body: [String: AnyObject]? = nil) -> NSMutableURLRequest {
    let url = createUrl(path)!
    let request = NSMutableURLRequest(URL: url)
    
    if let body = body {
        request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions())
    }
    
    request.HTTPMethod = method.rawValue
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("gzip;q=1.0,compress;q=0.5", forHTTPHeaderField: "Accept-Encoding")
    
    return request
}
{'fraction_non_alphanumeric': 0.11851851851851852, 'fraction_numerical': 0.00644122383252818, 'mean_word_length': 3.9696, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5269798', 'n_tokens_mistral': 937, 'n_tokens_neox': 919, 'n_words': 223}