<commit_before>//
//  API.swift
//  Endpoint
//
//  Created by <NAME> on 10/10/2016.
//  Copyright © 2016 Tailored Apps. All rights reserved.
//

import Foundation

public enum APIError: Error {
    case unacceptableStatus(code: Int, description: String)
    case parsingError(description: String)
    case serverError(description: String)
}

open class API {
    public let baseURL: URL
    public var debugAll = false
    
    public init(baseURL: URL) {
        self.baseURL = baseURL
    }
    
    public func request<E: Endpoint, R: RequestEncoder>(for endpoint: E, with data: R?=nil) -> URLRequest where E.RequestType == R {
        var url = baseURL
        
        if let path = endpoint.path {
            guard let urlWithPath = URL(string: path, relativeTo: baseURL) else {
                fatalError("invalid path \(path)")
            }
            url = urlWithPath
        }
        
        var request = URLRequest(url: url)
        request.httpMethod = endpoint.method.rawValue
        
        if let data = data ?? endpoint as? R {
            request = data.encode(request: request)
        }
        
        return request
    }
    <commit_msg>API: generic simplification<commit_after>//
//  API.swift
//  Endpoint
//
//  Created by <NAME> on 10/10/2016.
//  Copyright © 2016 Tailored Apps. All rights reserved.
//

import Foundation

public enum APIError: Error {
    case unacceptableStatus(code: Int, description: String)
    case parsingError(description: String)
    case serverError(description: String)
}

open class API {
    public let baseURL: URL
    public var debugAll = false
    
    public init(baseURL: URL) {
        self.baseURL = baseURL
    }
    
    public func request<E: Endpoint>(for endpoint: E, with data: E.RequestType?=nil) -> URLRequest {
        var url = baseURL
        
        if let path = endpoint.path {
            guard let urlWithPath = URL(string: path, relativeTo: baseURL) else {
                fatalError("invalid path \(path)")
            }
            url = urlWithPath
        }
        
        var request = URLRequest(url: url)
        request.httpMethod = endpoint.method.rawValue
        
        if let data = data ?? endpoint as? E.RequestType {
            request = data.encode(request: request)
        }
        
        return request
    }
    
{'fraction_non_alphanumeric': 0.09254498714652956, 'fraction_numerical': 0.010282776349614395, 'mean_word_length': 1.985933503836317, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6157222', 'n_tokens_mistral': 681, 'n_tokens_neox': 646, 'n_words': 201}