<commit_before>//
//  Copyright © 2020 ingresse. All rights reserved.
//

import Alamofire

protocol NetworkURLRequest {

    var baseURL: URL? { get }
    var path: String { get }
    var method: HTTPMethod { get }
    var parameters: Encodable? { get }
    var authenticationType: AuthenticationType? { get }
    var headers: [HeaderType]? { get }
}

extension NetworkURLRequest {

    private var encoding: ParameterEncoding {

        switch method {
        case .get, .delete:
            return URLEncoding.default
        default:
            return JSONEncoding.default
        }
    }

    private var headers: HTTPHeaders? {
        var mergedDicts: [String: String]?

        if let authType = authenticationType?.header {
            mergedDicts?.merge(authType) { current, _ in current }
        }

        headers?
            .compactMap({ $0.content })
            .forEach({ content in
                mergedDicts?.merge(content) { current, _ in current }
            })

        guard let headers = mergedDicts else { return nil }
        return HTTPHeaders(headers)
    }

    func asURLRequest() throws -> URLRequestConvertible {

        guard let url = baseURL?.appendingPathComponent(path) else {
            throw NetworkRequestError.invalidURL
        }
        let urlRequest = try URLRequest(url: url, method: method, headers: headers)
        return try encoding.encode(urlRequest, with: parameters?.encoded)
    }
}

enum NetworkRequestError: Error {

    case invalidURL
}<commit_msg>Fix header merge scheme<commit_after>//
//  Copyright © 2020 ingresse. All rights reserved.
//

import Alamofire

protocol NetworkURLRequest {

    var baseURL: URL? { get }
    var path: String { get }
    var method: HTTPMethod { get }
    var parameters: Encodable? { get }
    var authenticationType: AuthenticationType? { get }
    var headers: [HeaderType]? { get }
}

extension NetworkURLRequest {

    private var encoding: ParameterEncoding {

        switch method {
        case .get, .delete:
            return URLEncoding.default
        default:
            return JSONEncoding.default
        }
    }

    private var headers: HTTPHeaders? {
        var headerList = headers?.compactMap({ $0.content })

        if let authType = authenticationType?.header {
            headerList?.append(authType)
        }

        guard let tupleHeader: [(String, String)] = headerList?.flatMap({ $0 }) else {
            return nil
        }

        let mergedDicts = Dictionary(tupleHeader, uniquingKeysWith: +)

        return HTTPHeaders(mergedDicts)
    }

    func asURLRequest() throws -> URLRequestConvertible {

        guard let url = baseURL?.appendingPathComponent(path) else {
            throw NetworkRequestError.invalidURL
        }
        let urlRequest = try URLRequest(url: url, method: method, headers: headers)
        return try encoding.encode(urlRequest, with: parameters?.encoded)
    }
}

enum NetworkRequestError: Error {

    case invalidURL
}
{'fraction_non_alphanumeric': 0.08689839572192513, 'fraction_numerical': 0.003676470588235294, 'mean_word_length': 2.3516237402015676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4429981', 'n_tokens_mistral': 813, 'n_tokens_neox': 775, 'n_words': 231}