<commit_before>import Foundation
import RequestKit
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

public let githubBaseURL = "https://api.github.com"
public let githubWebURL = "https://github.com"

public let OctoKitErrorDomain = "com.nerdishbynature.octokit"

public struct TokenConfiguration: Configuration {
    public var apiEndpoint: String
    public var accessToken: String?
    public let errorDomain = OctoKitErrorDomain
    public let authorizationHeader: String? = "Basic"

    /// Custom `Accept` header for API previews.
    ///
    /// Used for preview support of new APIs, for instance Reaction API.
    /// see: https://developer.github.com/changes/2016-05-12-reactions-api-preview/
    private var previewCustomHeaders: Array<HTTPHeader>?

    public var customHeaders: Array<HTTPHeader>? {
        // More (non-preview) headers can be appended if needed in the future
        return previewCustomHeaders
    }

    public init(_ token: String? = nil, url: String = githubBaseURL, previewHeaders: [PreviewHeader] = []) {
        apiEndpoint = url
        accessToken = token?.data(using: .utf8)!.base64EncodedString()
        previewCustomHeaders = previewHeaders.map(\.header)
    }
}

public struct OAuthConfiguration: Configuration {
    public var apiEndpoint: String
    public var accessToken: String?
    public let token: String
    public let secret: String
    public let scopes: [String]
    public let webEndpoint: String
    public let errorDomain = OctoKitErrorDomain

    /// Custom `Accept` header for API previews.
    ///
    /// Used for preview support of new APIs, for instance Reaction API.
    /// see: https://developer.github.com/changes/2016-05-12-reactions-api-preview/
    private var previewCustomHeaders: Array<HTTPHeader>?

    public var customHeaders: Array<HTTPHeader>? {
        // More (non-preview) headers can be appended if needed in the future
        return previewCustomHeaders
    }

    public init(_ url: String = githubBaseURL, webURL: String = githubWebURL,
        token: String, secret: String, scopes: [String],  previewHeaders: [PreviewHeader] = []) {
            apiEndpoint = url
            webEndpoint = webURL
            self.token = token
            self.secret = secret
            self.scopes = scopes
            previewCustomHeaders = previewHeaders.map(\.header)
    }

    public func authenticate() -> URL? {
        return OAuthRouter.authorize(self).URLRequest?.url
    }

    public func authorize(_ session: RequestKitURLSession = URLSession.shared, code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
        let request = OAuthRouter.accessToken(self, code).URLRequest
        if let request = request {
            let task = session.dataTask(with: request) { data, response, err in
                if let response = response as? HTTPURLResponse {<commit_msg>Use Swift version < 5.2 code for retro compatibility<commit_after>import Foundation
import RequestKit
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

public let githubBaseURL = "https://api.github.com"
public let githubWebURL = "https://github.com"

public let OctoKitErrorDomain = "com.nerdishbynature.octokit"

public struct TokenConfiguration: Configuration {
    public var apiEndpoint: String
    public var accessToken: String?
    public let errorDomain = OctoKitErrorDomain
    public let authorizationHeader: String? = "Basic"

    /// Custom `Accept` header for API previews.
    ///
    /// Used for preview support of new APIs, for instance Reaction API.
    /// see: https://developer.github.com/changes/2016-05-12-reactions-api-preview/
    private var previewCustomHeaders: Array<HTTPHeader>?

    public var customHeaders: Array<HTTPHeader>? {
        // More (non-preview) headers can be appended if needed in the future
        return previewCustomHeaders
    }

    public init(_ token: String? = nil, url: String = githubBaseURL, previewHeaders: [PreviewHeader] = []) {
        apiEndpoint = url
        accessToken = token?.data(using: .utf8)!.base64EncodedString()
        previewCustomHeaders = previewHeaders.map { $0.header }
    }
}

public struct OAuthConfiguration: Configuration {
    public var apiEndpoint: String
    public var accessToken: String?
    public let token: String
    public let secret: String
    public let scopes: [String]
    public let webEndpoint: String
    public let errorDomain = OctoKitErrorDomain

    /// Custom `Accept` header for API previews.
    ///
    /// Used for preview support of new APIs, for instance Reaction API.
    /// see: https://developer.github.com/changes/2016-05-12-reactions-api-preview/
    private var previewCustomHeaders: Array<HTTPHeader>?

    public var customHeaders: Array<HTTPHeader>? {
        // More (non-preview) headers can be appended if needed in the future
        return previewCustomHeaders
    }

    public init(_ url: String = githubBaseURL, webURL: String = githubWebURL,
        token: String, secret: String, scopes: [String],  previewHeaders: [PreviewHeader] = []) {
            apiEndpoint = url
            webEndpoint = webURL
            self.token = token
            self.secret = secret
            self.scopes = scopes
            previewCustomHeaders = previewHeaders.map { $0.header }
    }

    public func authenticate() -> URL? {
        return OAuthRouter.authorize(self).URLRequest?.url
    }

    public func authorize(_ session: RequestKitURLSession = URLSession.shared, code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
        let request = OAuthRouter.accessToken(self, code).URLRequest
        if let request = request {
            let task = session.dataTask(with: request) { data, response, err in
                if let response = response as? HTTPURLResponse {
{'fraction_non_alphanumeric': 0.09064477509834103, 'fraction_numerical': 0.007183170856849667, 'mean_word_length': 3.055478502080444, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 15, 'https://': 8, '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': '4303136', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1510, 'n_words': 489}