<commit_before>//
//  ContactsController.swift
//  HarvestKit
//
//  Created by <NAME> on 16/01/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

#if os(iOS)
    import ThunderRequest
#elseif os(tvOS)
    import ThunderRequestTV
#elseif os (OSX)
    import ThunderRequestMac
#endif

/**
The contacts controller is responsible for managing contacts in the Harvest API. Contacts can be assosciated with clients
*/
public class ContactsController {
    
    /**
    The request controller used to load contact information. This is shared with other controllers
    */
    let requestController: TSCRequestController
    
    /**
    Initialises a new controller.
    - parameter requestController: The request controller to use when loading contact information. This must be passed down from HarvestController so that authentication may be shared
    */
    internal init(requestController: TSCRequestController) {
        
        self.requestController = requestController
    }
    
    /**
    Gets all contacts for the account of the authenticated user.
     
    - parameter completionHandler: The completion handler to return contacts and errors to
     
    - Note: The user must have access to the contacts on this account
    */
    public func getContacts(completionHandler: (contacts: [Contact]?, requestError: NSError?) -> ()) {
        
        requestController.get("contacts") { (response: TSCRequestResponse?, requestError: NSError?) -> Void in
            
            if let error = requestError {
                completionHandler(contacts: nil, requestError: error)
                return;
            }
            
            if let contactsArray = response?.array as? [[String: AnyObject]] {
                
                let contacts = contactsArray.map({Contact(dictionary: $0)}).filter { $0 != nil }.map { $0! }
                
                completionHandler(contacts: contacts, requestError: nil)
                return
            }
            
            let error = NSError(domain: "co.uk.mattcheetham.harvestkit", code: 500, userInfo: [NSLocalizedDescriptionKey: "The server did not return a valid response"])
            completionHandler(contacts: nil, requestError: error)
            
        }
        
    }
    
    /**
     Gets all contacts assosciated with a client by their identifier
     
     - parameter clientIdentifier: The unique identifier of the client to search for contacts for
     - parameter completionHandler: The completion handler to return contacts and errors to
     
     - Note: The user must have access to the contacts on this account
     */
    public func getContacts(clientIdentifier: Int, completionHandler: (contacts: [Contact]?, requestError: NSError?) -> ()) {
        
        requestController.get("clients/\(clientIdentifier)/contacts") { (response: TSCRequestResponse?, requestError: NSError?) -> Void in
            
            if let error = requestError {
                completionHandler(contacts: nil, requestError: error)
                return;
            }
            
            if let contactsArray = response?.array as? [[String: AnyObject]] {
                
                let contacts = contactsArray.map({Contact(dictionary: $0)}).filter { $0 != nil }.map { $0! }
                
                completionHandler(contacts: contacts, requestError: nil)
                return
            }
            
            let error = NSError(domain: "co.uk.mattcheetham.harvestkit", code: 500, userInfo: [NSLocalizedDescriptionKey: "The server did not return a valid response"])
            completionHandler(contacts: nil, requestError: error)
            
        }
        
    }
    
    /**
     Gets all contacts assosciated with a client
     
     - parameter clientIdentifier: The unique identifier of the client to search for contacts for
     - parameter completionHandler: The completion handler to return contacts and errors to
     
     - Note: The user must have access to the contacts on this account
     */
    public func getContacts(client: Client, completionHandler: (contacts: [Contact]?, requestError: NSError?) -> ()) {
        
        guard let clientIdentifier = client.identifier else {
            
            let error = NSError(domain: "co.uk.mattcheetham.harvestkit", code: 400, userInfo: [NSLocalizedDescriptionKey: "The client specified does not have an identifier"])
            completionHandler(contacts: nil, requestError: error)
            return
        }
        
        getContacts(clientIdentifier, completionHandler: completionHandler)
        
    }
    
}<commit_msg>Adds ability to retrieve a single contact by identifier. Closes #17<commit_after>//
//  ContactsController.swift
//  HarvestKit
//
//  Created by <NAME> on 16/01/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

#if os(iOS)
    import ThunderRequest
#elseif os(tvOS)
    import ThunderRequestTV
#elseif os (OSX)
    import ThunderRequestMac
#endif

/**
The contacts controller is responsible for managing contacts in the Harvest API. Contacts can be assosciated with clients
*/
public class ContactsController {
    
    /**
    The request controller used to load contact information. This is shared with other controllers
    */
    let requestController: TSCRequestController
    
    /**
    Initialises a new controller.
    - parameter requestController: The request controller to use when loading contact information. This must be passed down from HarvestController so that authentication may be shared
    */
    internal init(requestController: TSCRequestController) {
        
        self.requestController = requestController
    }
    
    //MARK: - Retrieving Contacts
    
    /**
    Gets all contacts for the account of the authenticated user.
     
    - parameter completionHandler: The completion handler to return contacts and errors to
     
    - Note: The user must have access to the contacts on this account
    */
    public func getContacts(completionHandler: (contacts: [Contact]?, requestError: NSError?) -> ()) {
        
        requestController.get("contacts") { (response: TSCRequestResponse?, requestError: NSError?) -> Void in
            
            if let error = requestError {
                completionHandler(contacts: nil, requestError: error)
                return;
            }
            
            if let contactsArray = response?.array as? [[String: AnyObject]] {
                
                let contacts = contactsArray.map({Contact(dictionary: $0)}).filter { $0 != nil }.map { $0! }
                
                completionHandler(contacts: contacts, requestError: nil)
                return
            }
            
            let error = NSError(domain: "co.uk.mattcheetham.harvestkit", code: 500, userInfo: [NSLocalizedDescriptionKey: "The server did not return a valid response"])
            completionHandler(contacts: nil, requestError: error)
            
        }
        
    }
    
    /**
     Gets all contacts assosciated with a client by their identifier
     
     - parameter clientIdentifier: The unique identifier of the client to search for contacts for
     - parameter completionHandler: The completion handler to return contacts and errors to
     
     - Note: The user must have access to the contacts on this account
     */
    public func getContacts(clientIdentifier: Int, completionHandler: (contacts: [Contact]?, requestError: NSError?) -> ()) {
        
        requestController.get("clients/\(clientIdentifier)/contacts") { (response: TSCRequestResponse?, requestError: NSError?) -> Void in
            
            if let error = requestError {
                completionHandler(contacts: nil, requestError: error)
                return;
            }
            
            if let contactsArray = response?.array as? [[String: AnyObject]] {
                
                let contacts = contactsArray.map({Contact(dictionary: $0)}).filter { $0 != nil }.map { $0! }
                
                completionHandler(contacts: contacts, requestError: nil)
                return
            }
            
            let error = NSError(domain: "co.uk.mattcheetham.harvestkit", code: 500, userInfo: [NSLocalizedDescriptionKey: "The server did not return a valid response"])
            completionHandler(contacts: nil, requestError: error)
            
        }
        
    }
    
    /**
     Gets all contacts assosciated with a client
     
     - parameter clientIdentifier: The unique identifier of the client to search for contacts for
     - parameter completionHandler: The completion handler to return contacts and errors to
     
     - Note: The user must have access to the contacts on this account
     */
    public func getContacts(client: Client, completionHandler: (contacts: [Contact]?, requestError: NSError?) -> ()) {
        
        guard let clientIdentifier = client.identifier else {
            
            let error = NSError(domain: "co.uk.mattcheetham.harvestkit", code: 400, userInfo: [NSLocalizedDescriptionKey: "The client specified does not have an identifier"])
            completionHandler(contacts: nil, requestError: error)
            return
        }
        
        getContacts(clientIdentifier, completionHandler: completionHandler)
        
    }
    
    /**
     Gets a contact for a specific ID
     
     - parameter identifier: The identifier for a contact
     - parameter completionHandler: The completion handler to return the contact and errors to
     */
    public func getContact(identifier: Int, completionHandler: (contact: Contact?, requestError: NSError?) -> ()) {
        
        requestController.get("contacts/\(identifier)") { (response: TSCRequestResponse?, requestError: NSError?) -> Void in
            
            if let error = requestError {
                completionHandler(contact: nil, requestError: error)
                return
            }
            
            if let contactResponse = response {
                
                if let contactResponseDictionary = contactResponse.dictionary as? [String: AnyObject] {
                    
                    let foundContact = Contact(dictionary: contactResponseDictionary)
                    completionHandler(contact: foundContact, requestError: nil)
                    return
                    
                }
                
            }
            
            let error = NSError(domain: "co.uk.mattcheetham.harvestkit", code: 500, userInfo: [NSLocalizedDescriptionKey: "The server did not return a valid contact object"])
            completionHandler(contact: nil, requestError: error)
            
        }
        
    }
    
}
{'fraction_non_alphanumeric': 0.07741576162628794, 'fraction_numerical': 0.005476654599461617, 'mean_word_length': 2.1539812646370025, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '4429071', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2572, 'n_words': 885}