<commit_before>//
//  ClientUtils.swift
//  Skugga
//
//  Created by <NAME> on 23/09/2017.
//  Copyright © 2017 NamelessDev. All rights reserved.
//

import Foundation

enum APIClientError: String, Error, LocalizedError {
    case unknown = "Unknown"
    case badURL = "Bad URL"
    case jsonParserError = "JSON Parsing error"
    
    var errorDescription: String {
        return self.rawValue
    }
    
    var code: Int {
        switch(self) {
        case .unknown: return -1
        case .badURL: return 1
        case .jsonParserError: return 2
        }
    }
    
    var nsError: NSError {
        return NSError(domain: "skugga.apiclient.error", code: self.code, userInfo: [NSLocalizedDescriptionKey: self.rawValue.description])
    }
}

extension URL {
    init?(route: Route?) {
        self.init(string: Configuration.endpoint)
        self.appendPathComponent("1.0")
        if let route = route {
            self.appendPathComponent(route.rawValue)
        }
    }
}

extension URLRequest {
    init(route: Route?) throws {
        let url = URL(route: route)
        
        if let url = url {
            self.init(url: url)
            
            let secret = Configuration.secret
            if !secret.isEmpty {
                self.setValue(secret, forHTTPHeaderField: ClientConsts.SECRET_KEY_HEADER)
            }
        } else {
            throw APIClientError.badURL
        }
    }
    
    mutating func addSecret() {
        let secret = Configuration.secret
        if !secret.isEmpty {
            self.setValue(secret, forHTTPHeaderField: ClientConsts.SECRET_KEY_HEADER)
        }
    }
}<commit_msg>Add helper for http error codes<commit_after>//
//  ClientUtils.swift
//  Skugga
//
//  Created by <NAME> on 23/09/2017.
//  Copyright © 2017 NamelessDev. All rights reserved.
//

import Foundation

enum APIClientError: String, Error, LocalizedError {
    case unknown = "Unknown"
    case badURL = "Bad URL"
    case jsonParserError = "JSON Parsing error"
    
    var errorDescription: String {
        return self.rawValue
    }
    
    var code: Int {
        switch(self) {
        case .unknown: return -1
        case .badURL: return 1
        case .jsonParserError: return 2
        }
    }
    
    var nsError: NSError {
        return NSError(domain: "skugga.apiclient.error", code: self.code, userInfo: [NSLocalizedDescriptionKey: self.rawValue.description])
    }
}

extension URL {
    init?(route: Route?) {
        self.init(string: Configuration.endpoint)
        self.appendPathComponent("1.0")
        if let route = route {
            self.appendPathComponent(route.rawValue)
        }
    }
}

extension URLRequest {
    init(route: Route?) throws {
        let url = URL(route: route)
        
        if let url = url {
            self.init(url: url)
            
            let secret = Configuration.secret
            if !secret.isEmpty {
                self.setValue(secret, forHTTPHeaderField: ClientConsts.SECRET_KEY_HEADER)
            }
        } else {
            throw APIClientError.badURL
        }
    }
    
    mutating func addSecret() {
        let secret = Configuration.secret
        if !secret.isEmpty {
            self.setValue(secret, forHTTPHeaderField: ClientConsts.SECRET_KEY_HEADER)
        }
    }
}

extension URLResponse {
    func isHTTPSuccessful() -> Bool {
        if let statusCode = (self as? HTTPURLResponse)?.statusCode {
            return statusCode >= 200 && statusCode <= 299
        }
        return false
    }
}
{'fraction_non_alphanumeric': 0.09418439716312056, 'fraction_numerical': 0.011347517730496455, 'mean_word_length': 1.9432387312186978, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, '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': '5545296', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1032, 'n_words': 253}