<commit_before>//
//  SRPuppyClient.swift
//  Swifty Recipes
//
//  Created by <NAME> on 12/02/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public class SRPuppyClient {
    
    public typealias CompletionHandler = (result: [SRRecipe]?, error: NSError?) -> Void
    
    // MARK: Properties
    
    public let imageCache = SRImageCache()
    public var includesRecipesWithoutThumbnails: Bool {
        get {
            return onlyImages == 0
        }
        set {
            if newValue == true {
                onlyImages = 0
            } else {
                onlyImages = 1
            }
        }
    }

    private let session: NSURLSession!
    private var onlyImages: Int = 1
    
    // MARK: Life Cycle
    
    private init() {
        session = NSURLSession.sharedSession()
    }
    
    class func sharedClient() -> SRPuppyClient {
        
        struct Singleton {
            static let sharedClient = SRPuppyClient()
        }
        
        return Singleton.sharedClient
    }
    
    // MARK: Convenience Methods
    
    /**
    Utilizes a NSURLSessionDataTask to retrieve recipes using the Recipe Puppy API.
    - parameter ingredients: An optional array of ingredient names represented by a [String].
    - parameter query: An optional search query represented by a String.
    - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
    - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error.
    */
    public func fetchRecipes(ingredients: [String]?, query: String?, page: Int = 1, completionHandler: CompletionHandler)  {
        
        taskWithParameters(ingredients, query: query, page: page) { result, error in
            
            if let error = error {
                completionHandler(result: nil, error: error)
            } else {
                completionHandler(result: result, error: error)
            }
        }
    }
    
    /**
     Utilizes a NSURLSessionDataTask to retrieve recipes using the Recipe Puppy API.
     - parameter ingredients: An optional array of ingredient names represented by a [String].
     - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
     - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error.
     */
    public func fetchRecipesWithIngredients(ingredients: [String]?, page: Int = 1, completionHandler: CompletionHandler) -> Void {
        
        taskWithParameters(ingredients, query: nil, page: page) { result, error in
            
            if let error = error {
                completionHandler(result: nil, error: error)
            } else {
                completionHandler(result: result, error: error)
            }
        }
    }
    
    /**
     Utilizes a NSURLSessionDataTask to retrieve recipes using the Recipe Puppy API.
     - parameter query: An optional search query represented by a String.
     - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
     - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error.
     */
    public func fetchRecipesWithQuery(query: String?, page: Int = 1, completionHandler: CompletionHandler) -> Void {
        
        taskWithParameters(nil, query: query, page: page) { result, error in
            
            if let error = error {
                completionHandler(result: nil, error: error)
            } else {
                completionHandler(result: result, error: error)
            }
        }
    }
    
    // MARK: Task Method
    
    /**
    Creates a NSURLSessionDataTask for a Recipe Puppy API request given a set of parameters.
    - parameter ingredients: An optional array of ingredient names represented by a [String].
    - parameter query: An optional search query represented by a String.
    - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
    - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error. If the recipe array and errors are both nil, this indicates correct response with no result.<commit_msg>Fetch convenience methods now properly return the underlying task<commit_after>//
//  SRPuppyClient.swift
//  Swifty Recipes
//
//  Created by <NAME> on 12/02/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public class SRPuppyClient {
    
    public typealias CompletionHandler = (result: [SRRecipe]?, error: NSError?) -> Void
    
    // MARK: Properties
    
    public let imageCache = SRImageCache()
    public var includesRecipesWithoutThumbnails: Bool {
        get {
            return onlyImages == 0
        }
        set {
            if newValue == true {
                onlyImages = 0
            } else {
                onlyImages = 1
            }
        }
    }

    private let session: NSURLSession!
    private var onlyImages: Int = 1
    
    // MARK: Life Cycle
    
    private init() {
        session = NSURLSession.sharedSession()
    }
    
    class func sharedClient() -> SRPuppyClient {
        
        struct Singleton {
            static let sharedClient = SRPuppyClient()
        }
        
        return Singleton.sharedClient
    }
    
    // MARK: Convenience Methods
    
    /**
    Utilizes a NSURLSessionDataTask to retrieve recipes using the Recipe Puppy API.
    - parameter ingredients: An optional array of ingredient names represented by a [String].
    - parameter query: An optional search query represented by a String.
    - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
    - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error.
    */
    public func fetchRecipes(ingredients: [String]?, query: String?, page: Int = 1, completionHandler: CompletionHandler) -> NSURLSessionDataTask? {
        
        return taskWithParameters(ingredients, query: query, page: page) { result, error in
            if let error = error {
                completionHandler(result: nil, error: error)
            } else {
                completionHandler(result: result, error: error)
            }
        }
    }
    
    /**
     Utilizes a NSURLSessionDataTask to retrieve recipes using the Recipe Puppy API.
     - parameter ingredients: An optional array of ingredient names represented by a [String].
     - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
     - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error.
     */
    public func fetchRecipesWithIngredients(ingredients: [String]?, page: Int = 1, completionHandler: CompletionHandler) -> NSURLSessionDataTask? {
        
        return taskWithParameters(ingredients, query: nil, page: page) { result, error in
            if let error = error {
                completionHandler(result: nil, error: error)
            } else {
                completionHandler(result: result, error: error)
            }
        }
    }
    
    /**
     Utilizes a NSURLSessionDataTask to retrieve recipes using the Recipe Puppy API.
     - parameter query: An optional search query represented by a String.
     - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
     - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error.
     */
    public func fetchRecipesWithQuery(query: String?, page: Int = 1, completionHandler: CompletionHandler) -> NSURLSessionDataTask? {
        
        return taskWithParameters(nil, query: query, page: page) { result, error in
            if let error = error {
                completionHandler(result: nil, error: error)
            } else {
                completionHandler(result: result, error: error)
            }
        }
    }
    
    // MARK: Task Method
    
    /**
    Creates a NSURLSessionDataTask for a Recipe Puppy API request given a set of parameters.
    - parameter ingredients: An optional array of ingredient names represented by a [String].
    - parameter query: An optional search query represented by a String.
    - parameter page: An Int representing the page number of the request. The Recipe Puppy API returns at most 10 items per page.
    - parameter completionHandler: A completion handler for the asynchroneous data request. The result is provided as an optional [SRRecipe] array and an optional NSError indicating error. If the recipe array and errors are both nil, this indicates correct response with no result.
{'fraction_non_alphanumeric': 0.0635203013813311, 'fraction_numerical': 0.005232314776056928, 'mean_word_length': 2.3974404550302166, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '4130669', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2359, 'n_words': 1005}