<commit_before>//
//  Response.swift
//  WatsonCore
//
//  Scaled down version of Alamofire response object
//
//  Created by <NAME> on 10/12/15.
//  Copyright © 2015 MIL. All rights reserved.
//

import SwiftyJSON
import ObjectMapper
import Foundation

/**
*  The Main response back for Watson Core.  It will contain the status, status info
and the status code for the http response.
*/
public struct CoreResponse: Mappable, CustomStringConvertible {
    public var data:AnyObject?
    public var code:Int?
    public var info:String?
    public var help:String?
    
    //Alchemy
    public var totalTransactions:Int?
    public var usage:String?
    public var status:String?
    
    //Concept Expansion
    public var developer:String?
    
    //NSError
    public var domain:String?
    
    public var error:NSError {
        let errDomain = domain == nil ? WatsonCoreConstants.defaultErrorDomain : domain!
        let errCode = code == nil ? 0 : code!
        let errUserInfo = info == nil ? [:] : [WatsonCoreConstants.descriptionKey:info!]

        return NSError(domain: errDomain, code: errCode, userInfo: errUserInfo)
    }
    
    public var description: String {
        var desc = ""
        if let code = code {
            desc += "\nCode: \(code)"
        }
        if let status = status {
            desc += "\nStatus: \(status)"
        }
        if let domain = domain {
            desc += "\nDomain: \(domain)"
        }
        if let info = info {
            desc += "\nInfo: \(info)"
        }
        if let developer = developer {
            desc += "\nDeveloper: \(developer)"
        }
        if let help = help {
            desc += "\nHelp: \(help)"
        }
        return desc
    }

    public init?(_ map: Map) {}
    
    public mutating func mapping(map: Map) {
        //Maps most verbose information last, so that it takes precedence in case of overlapping information

        //NSError
        code                <-  map["errorCode"]
        domain              <-  map["errorDomain"]
        info                <-  map["errorLocalizedDescription"]

        //NSURLHTTPResponse
        code                <-  map["responseStatusCode"]
        info                <-  map["responseInfo"]
        
        //Standard
        data                <-  map["data"]
        code                <- (map["data.code"], Transformation.stringToInt)
        info                <-  map["data.error"]
        info                <-  map["data.error_message"]
        
        //Personality Insights
        help                <-  map["data.help"]
        info                <-  map["data.description"]
        
        //Concept Expansion
        code                <-  map["data.error.error_code"]
        info                <-  map["data.message"]
        info                <-  map["data.error.user_message"]
        developer           <-  map["data.error.dev_message"]
        help                <-  map["data.error.doc"]

        //Alchemy
        totalTransactions   <- (map["data.totalTransactions"], Transformation.stringToInt)
        status              <-  map["data.status"]
        usage               <-  map["data.usage"]
        info                <-  map["data.statusInfo"]
        
    }
}<commit_msg>Add ability for CoreResponse to return and instance of itself<commit_after>//
//  Response.swift
//  WatsonCore
//
//  Scaled down version of Alamofire response object
//
//  Created by <NAME> on 10/12/15.
//  Copyright © 2015 MIL. All rights reserved.
//

import ObjectMapper
import Foundation
import Alamofire

/**
*  The Main response back for Watson Core.  It will contain the status, status info
and the status code for the http response.
*/
public struct CoreResponse: Mappable, CustomStringConvertible {
    public var data:AnyObject?
    public var code:Int?
    public var info:String?
    public var help:String?
    
    //Alchemy
    public var totalTransactions:Int?
    public var usage:String?
    public var status:String?
    
    //Concept Expansion
    public var developer:String?
    
    //NSError
    public var domain:String?
    
    public var error:NSError? {
        let errDomain = domain == nil ? WatsonCoreConstants.defaultErrorDomain : domain!
        let errCode = code == nil ? 0 : code!
        let errUserInfo = info == nil ? [:] : [WatsonCoreConstants.descriptionKey:info!]

        return NSError(domain: errDomain, code: errCode, userInfo: errUserInfo)
    }
    
    public var description: String {
        var desc = ""
        if let code = code {
            desc += "\nCode: \(code)"
        }
        if let status = status {
            desc += "\nStatus: \(status)"
        }
        if let domain = domain {
            desc += "\nDomain: \(domain)"
        }
        if let info = info {
            desc += "\nInfo: \(info)"
        }
        if let developer = developer {
            desc += "\nDeveloper: \(developer)"
        }
        if let help = help {
            desc += "\nHelp: \(help)"
        }
        return desc
    }

    public init?(_ map: Map) {}

    public mutating func mapping(map: Map) {
        //Maps most verbose information last, so that it takes precedence in case of overlapping information

        //NSError
        code                <-  map["errorCode"]
        domain              <-  map["errorDomain"]
        info                <-  map["errorLocalizedDescription"]

        //NSURLHTTPResponse
        code                <-  map["responseStatusCode"]
        info                <-  map["responseInfo"]
        
        //Standard
        data                <-  map["data"]
        code                <- (map["data.code"], Transformation.stringToInt)
        info                <-  map["data.error"]
        info                <-  map["data.error_message"]
        
        //Personality Insights
        help                <-  map["data.help"]
        info                <-  map["data.description"]
        
        //Concept Expansion
        code                <-  map["data.error.error_code"]
        info                <-  map["data.message"]
        info                <-  map["data.error.user_message"]
        developer           <-  map["data.error.dev_message"]
        help                <-  map["data.error.doc"]

        //Alchemy
        totalTransactions   <- (map["data.totalTransactions"], Transformation.stringToInt)
        status              <-  map["data.status"]
        usage               <-  map["data.usage"]
        info                <-  map["data.statusInfo"]
    }
  
  /**
   Given an AlamoFire response object, returns a Watson response object (CoreResponse) with standardized fields for errors and info
   
   - parameter response: AlamoFire Response
   
   - returns: A Watson CoreResponse
   */
  static func getCoreResponse<T>(response: Response<T,NSError>)->CoreResponse {
    var coreResponseDictionary: Dictionary<String,AnyObject> = Dictionary()
    
    if let data = response.data where data.length > 0 {
      do {
        if let jsonData = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves) as? [String: AnyObject] {
          coreResponseDictionary.updateValue(jsonData, forKey: "data")
        }
      } catch {
        Log.sharedLogger.error("Could not convert response data object to JSON")
      }
    }
    if let error = response.result.error {
      coreResponseDictionary.updateValue(error.code, forKey: "errorCode")
      coreResponseDictionary.updateValue(error.localizedDescription, forKey: "errorLocalizedDescription")
      coreResponseDictionary.updateValue(error.domain, forKey: "errorDomain")
    }
    if let response = response.response {
      coreResponseDictionary.updateValue(response.statusCodeEnum.rawValue, forKey: "responseStatusCode")
      coreResponseDictionary.updateValue(response.statusCodeEnum.localizedReasonPhrase, forKey: "responseInfo")
    }
    let coreResponse = Mapper<CoreResponse>().map(coreResponseDictionary)!
    Log.sharedLogger.info("\(coreResponse)")
    return coreResponse
  }

}
{'fraction_non_alphanumeric': 0.10418984056358917, 'fraction_numerical': 0.002842664689160796, 'mean_word_length': 1.7874612469858766, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3130180', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2106, 'n_words': 576}