<commit_before>//
//  HttpRequestable.swift
//  Restivus
//
//  Created by <NAME> on 2017-08-21.
//  Copyright © 2017 <EMAIL>. All rights governed under the Apache 2 License Agreement
//

import Foundation

/// The type to be used when no custom Decodable result is required.
/// Useful for when you don't know, or care, what the response is (or if one is even provided),
/// such as a `Deletable` with no response body, or a `Gettable` which returns HTML.
public typealias Raw = Data

/// Represents the result of an asynchronous operation.
///
/// - success: The operation was a success, and contains the `Success` output from that operation.
/// - failure: The operation failed, and contains the `Error` from that operation.
public enum Result<Success> {
    case success(Success)
    case failure(HTTPError)
}

/// A wrapper which will contain the decoded `T` instance, if one was successfully decoded; otherwise `nil`.
/// `OptionalResponseType` is useful for when you have a request which may or may not return the expected
/// JSON structure. For example, fetching a mythical `UserDetails` JSON for a user who does not exist,
/// but for reason - unbeknownst to us - the server will return no JSON instead of a 404.
///
/// Example:
/// ---
///
///     struct UserDetails {
///         var mothersMaidenName: String
///         var lovesTheDentist: Bool
///     }
///
///     struct UserDetailsRequest: Codable {
///         var userId: Int
///     }
///
///     extension UserDetailsRequest: Authenticating, Gettable {
///         typealias ResponseType = OptionalResponseType<UserDetails>
///         var path: String = "/some/api/path"
///     }
///
///     UserDetailsRequest(userId: 42).submit() { result in
///         if case let Result.success(optionalUserDetails) = result {
///             let userDetails = optionalUserDetails.response
///
///         }
///     }
/// ---
struct OptionalResponseType<T>: Decodable where T: Decodable {
    var response: T? = nil
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        self.response = try? container.decode(T.self)
    }
}


// The function used as a completion handler in all Restables.
public typealias HttpSubmittableCompletionHandler<ResponseType> = (Result<ResponseType>) -> Void

/// The base protocol for method-specific protocols.
/// `Restable` defaults the following implementations:
///    - *baseURL*: `""`
///    - *path*: `""`
///    - *resultEncoding*: `.json`
///    - *submit(...) throws -> URLSessionDataTask*: 
public protocol Restable {
    associatedtype ResponseType: Decodable<commit_msg>that ol' `public` keyword gets ya every time.<commit_after>//
//  HttpRequestable.swift
//  Restivus
//
//  Created by <NAME> on 2017-08-21.
//  Copyright © 2017 <EMAIL>. All rights governed under the Apache 2 License Agreement
//

import Foundation

/// The type to be used when no custom Decodable result is required.
/// Useful for when you don't know, or care, what the response is (or if one is even provided),
/// such as a `Deletable` with no response body, or a `Gettable` which returns HTML.
public typealias Raw = Data

/// Represents the result of an asynchronous operation.
///
/// - success: The operation was a success, and contains the `Success` output from that operation.
/// - failure: The operation failed, and contains the `Error` from that operation.
public enum Result<Success> {
    case success(Success)
    case failure(HTTPError)
}

/// A wrapper which will contain the decoded `T` instance, if one was successfully decoded; otherwise `nil`.
/// `OptionalResponseType` is useful for when you have a request which may or may not return the expected
/// JSON structure. For example, fetching a mythical `UserDetails` JSON for a user who does not exist,
/// but for reason - unbeknownst to us - the server will return no JSON instead of a 404.
///
/// Example:
/// ---
///
///     struct UserDetails {
///         var mothersMaidenName: String
///         var lovesTheDentist: Bool
///     }
///
///     struct UserDetailsRequest: Codable {
///         var userId: Int
///     }
///
///     extension UserDetailsRequest: Authenticating, Gettable {
///         typealias ResponseType = OptionalResponseType<UserDetails>
///         var path: String = "/some/api/path"
///     }
///
///     UserDetailsRequest(userId: 42).submit() { result in
///         if case let Result.success(optionalUserDetails) = result {
///             let userDetails = optionalUserDetails.response
///
///         }
///     }
/// ---
public struct OptionalResponseType<T>: Decodable where T: Decodable {
    var response: T? = nil
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        self.response = try? container.decode(T.self)
    }
}


// The function used as a completion handler in all Restables.
public typealias HttpSubmittableCompletionHandler<ResponseType> = (Result<ResponseType>) -> Void

/// The base protocol for method-specific protocols.
/// `Restable` defaults the following implementations:
///    - *baseURL*: `""`
///    - *path*: `""`
///    - *resultEncoding*: `.json`
///    - *submit(...) throws -> URLSessionDataTask*: 
public protocol Restable {
    associatedtype ResponseType: Decodable
{'fraction_non_alphanumeric': 0.12431719721228103, 'fraction_numerical': 0.0067809380297607835, 'mean_word_length': 2.672199170124481, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '6237987', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1411, 'n_words': 553}