<commit_before>//
//  API.swift
//
//  Created by <NAME> on 19/03/16.
//

import SwiftyJSON

/// Protocol for request quer params
public protocol Queryable {
    func queryString(forKey key: String) -> [URLQueryItem]
}

/// Possibble response errors
public enum APIError: Error {
    case unknown
    case notFound
    case unouthorized
    case forbidden
    case serverError
    case multipleChoice
    case badRequest
    case notImplemented
    case gatewayTimeout
    //TODO: - expand
    
    init?(withResponse response: URLResponse?) {
        if let statusCode = (response as? HTTPURLResponse)?.statusCode {
            switch statusCode {
            case 200...299:
                return nil
            case 300:
                self = .multipleChoice
                break
            case 400:
                self = .badRequest
                break
            case 401:
                self = .unouthorized
                break
            case 403:
                self = .forbidden
                break
            case 404:
                self = .notFound
                break
            case 501:
                self = .notImplemented
                break
            case 504:
                self = .gatewayTimeout
                break
            case 500...599:
                self = .serverError
                break
            default:
                self = .unknown
            }
        }
        else {
            return nil
        }
    }
}

/// Possibble authentivation types for the request
public enum AuthenticationType {
    case none
    case httpHeader
    case urlParameter //a.k.a. Query
}

/// Authentication manager type
open class RequestAuthenticator {
    open var type: AuthenticationType = .none
    open var accessToken: String?
    open var tokenKey: String?
    
    public init() { }
    
    func authenticateURLRequest(_ req: URLRequest) -> URLRequest {
        var req = req
        switch self.type {
        case .httpHeader where accessToken != nil && tokenKey != nil:<commit_msg>- unknown error changed to other with status code in it<commit_after>//
//  API.swift
//
//  Created by <NAME> on 19/03/16.
//

import SwiftyJSON

/// Protocol for request quer params
public protocol Queryable {
    func queryString(forKey key: String) -> [URLQueryItem]
}

/// Possibble response errors
public enum APIError: Error {
    case other(Int)
    case notFound
    case unouthorized
    case forbidden
    case serverError
    case multipleChoice
    case badRequest
    case notImplemented
    case gatewayTimeout
    //TODO: - expand
    
    init?(withResponse response: URLResponse?) {
        if let statusCode = (response as? HTTPURLResponse)?.statusCode {
            switch statusCode {
            case 200...299:
                return nil
            case 300:
                self = .multipleChoice
                break
            case 400:
                self = .badRequest
                break
            case 401:
                self = .unouthorized
                break
            case 403:
                self = .forbidden
                break
            case 404:
                self = .notFound
                break
            case 501:
                self = .notImplemented
                break
            case 504:
                self = .gatewayTimeout
                break
            case 500...599:
                self = .serverError
                break
            default:
                self = .other(statusCode)
            }
        }
        else {
            return nil
        }
    }
}

/// Possibble authentivation types for the request
public enum AuthenticationType {
    case none
    case httpHeader
    case urlParameter //a.k.a. Query
}

/// Authentication manager type
open class RequestAuthenticator {
    open var type: AuthenticationType = .none
    open var accessToken: String?
    open var tokenKey: String?
    
    public init() { }
    
    func authenticateURLRequest(_ req: URLRequest) -> URLRequest {
        var req = req
        switch self.type {
        case .httpHeader where accessToken != nil && tokenKey != nil:
{'fraction_non_alphanumeric': 0.06660254868958884, 'fraction_numerical': 0.018754508295263286, 'mean_word_length': 1.3744292237442923, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1826938', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1022, 'n_words': 339}