<commit_before>//
//  Service.swift
//  Pods
//
//  Created by <NAME> on 3/10/16.
//
//

import Foundation
import BrightFutures
import Freddy
import Result

public protocol ServiceEnvironment {
    func baseUrl() -> String
    func fullURLWithResourcePath(path:String) -> String
}

public class Service {
    
    public static var environment:ServiceEnvironment? = nil
    
    private class func fullURLWithPath(path:String) -> String {
        var fullUrl = path
        
        if let e = environment {
            fullUrl = e.fullURLWithResourcePath(path)
        }
        
        return fullUrl
    }
    
    // MARK: Get JSON Object
    
    public class func getJSONObject(path: String, headers: [String : String] = [:], dotPath: String? = nil) -> Future<JSON, NSError> {
        return ServiceUtil.get(fullURLWithPath(path), headers: headers).flatMap { response -> Result<JSON, NSError> in
            do {
                let json = try response.bodyJSON()
                
                if let dotPath = dotPath, let jsonDotPathObject = json[dotPath] {
                    return Result.Success(jsonDotPathObject)
                } else {
                    return Result.Success(json)
                }
            } catch {
                return Result.Failure(NSError(domain: "", code: 0, userInfo: nil))
            }
        }
    }
    
    // MARK: Get Object
    
    public class func getObject<T: JSONDecodable>(path: String, headers: [String : String] = [:], dotPath: String? = nil) -> Future<T, NSError> {
        return getJSONObject(path, headers: headers, dotPath: dotPath).flatMap { json -> Result<T, NSError> in
            do {
                let object = try T(json: json)
                return Result.Success(object)
            } catch {
                return Result.Failure(NSError(domain: "", code: 0, userInfo: nil))
            }
        }
    }
    
    // MARK: Get Objects
    
    public class func getObjects<T: JSONDecodable>(path: String, headers: [String : String] = [:], dotPath: String? = nil) -> Future<[T], NSError> {
        return getJSONObject(path, headers: headers).flatMap { json -> Result<[T], NSError> in
            do {
                if case .Array(let jsonObjects) = json {
                    var objects = [T]()
                    
                    for jsonObject in jsonObjects {
                        objects.append(try T(json: jsonObject))
                    }
                    
                    return Result.Success(objects)
                }
            } catch {
                
            }
            
            return Result.Failure(NSError(domain: "", code: 0, userInfo: nil))
        }<commit_msg>Now passing in the type for the get calls in Service.<commit_after>//
//  Service.swift
//  Pods
//
//  Created by <NAME> on 3/10/16.
//
//

import Foundation
import BrightFutures
import Freddy
import Result

public protocol ServiceEnvironment {
    func baseUrl() -> String
    func fullURLWithResourcePath(path:String) -> String
}

public class Service {
    
    public static var environment:ServiceEnvironment? = nil
    
    private class func fullURLWithPath(path:String) -> String {
        var fullUrl = path
        
        if let e = environment {
            fullUrl = e.fullURLWithResourcePath(path)
        }
        
        return fullUrl
    }
    
    // MARK: Get JSON Object
    
    public class func getJSONObject(path: String, headers: [String : String] = [:], dotPath: String? = nil) -> Future<JSON, NSError> {
        return ServiceUtil.get(fullURLWithPath(path), headers: headers).flatMap { response -> Result<JSON, NSError> in
            do {
                let json = try response.bodyJSON()
                
                if let dotPath = dotPath, let jsonDotPathObject = json[dotPath] {
                    return Result.Success(jsonDotPathObject)
                } else {
                    return Result.Success(json)
                }
            } catch {
                return Result.Failure(NSError(domain: "", code: 0, userInfo: nil))
            }
        }
    }
    
    // MARK: Get Object
    
    public class func getObject<T: JSONDecodable>(type: T.Type, path: String, headers: [String : String] = [:], dotPath: String? = nil) -> Future<T, NSError> {
        return getJSONObject(path, headers: headers, dotPath: dotPath).flatMap { json -> Result<T, NSError> in
            do {
                let object = try T(json: json)
                return Result.Success(object)
            } catch {
                return Result.Failure(NSError(domain: "", code: 0, userInfo: nil))
            }
        }
    }
    
    // MARK: Get Objects
    
    public class func getObjects<T: JSONDecodable>(type: T.Type, path: String, headers: [String : String] = [:], dotPath: String? = nil) -> Future<[T], NSError> {
        return getJSONObject(path, headers: headers).flatMap { json -> Result<[T], NSError> in
            do {
                if case .Array(let jsonObjects) = json {
                    var objects = [T]()
                    
                    for jsonObject in jsonObjects {
                        objects.append(try T(json: jsonObject))
                    }
                    
                    return Result.Success(objects)
                }
            } catch {
                
            }
            
            return Result.Failure(NSError(domain: "", code: 0, userInfo: nil))
        }
{'fraction_non_alphanumeric': 0.10255941499085923, 'fraction_numerical': 0.0029250457038391227, 'mean_word_length': 1.610209923664122, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 39, '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': '5143012', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1451, 'n_words': 394}