<commit_before>//
//  CompresJsonRequest.swift
//  CompresJSON
//
//  Created by <NAME> on 09/05/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

public class CompresJsonRequest: JsonRequest {
   
    override public class func create< T : JsonRequest >(urlString:String, parameters:Dictionary<String, AnyObject>?, method:Alamofire.Method) -> T {
        
        return CompresJsonRequest(urlString: urlString, parameters: parameters, method: method) as! T
    }
    
    internal override func exec() {
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        
        if let params = self.parameters {
            
            var err: NSError?
            var json: String = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)!.toString()
            
            //NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted, error: <#NSErrorPointer#>)
            //json = json.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
            json = json.replaceString("\"", withString: "#")
            //println("json: \(json)")
            json = CompresJSON.encryptAndCompressAsNecessary(json)
            
            self.parameters = Dictionary<String, AnyObject>()
            self.parameters!["data"] = json
        }
        
        //println(self.parameters!["data"])
        //println(self.urlString)
        //println(CompresJSON.decryptAndDecompressAsNecessary(self.parameters!["data"] as! String))
        
        self.almofireRequest = request(self.method, self.urlString, parameters: self.parameters, encoding: ParameterEncoding.URL)
            .response{ (request, response, data, error) in
                
                if let e = error {
                    
                    println(e.localizedDescription)
                    
                    var alert = self.alertControllerForError(e, completion: { (retry) -> () in
                        
                        if retry {
                            
                            self.cancel()
                            self.exec()
                        }
                    })
                    
                    self.failDownload(e, alert: alert)
                }
                    
                else{
                    let json = JSON(data: data! as! NSData)
                    
                    let encryptedJson = json["data"].stringValue
                    let unencryptedJson = CompresJSON.decryptAndDecompressAsNecessary(encryptedJson).replaceString("#", withString: "\"")
                    
                    if let dataFromString = unencryptedJson.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false){
                        
                        let unpackedJson = JSON(data: dataFromString)
                        
                        self.succeedDownload(unpackedJson)
                    }
                    else {
                        println("got nil - retrying")
                        self.cancel()
                        self.exec()
                    }
                    
                    
                }
                
                self.finishDownload()
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        }
    }
    
}<commit_msg>removed \ #<commit_after>//
//  CompresJsonRequest.swift
//  CompresJSON
//
//  Created by <NAME> on 09/05/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

public class CompresJsonRequest: JsonRequest {
   
    override public class func create< T : JsonRequest >(urlString:String, parameters:Dictionary<String, AnyObject>?, method:Alamofire.Method) -> T {
        
        return CompresJsonRequest(urlString: urlString, parameters: parameters, method: method) as! T
    }
    
    internal override func exec() {
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        
        if let params = self.parameters {
            
            var err: NSError?
            var json: String = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)!.toString()
            
            //NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted, error: <#NSErrorPointer#>)
            //json = json.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
            //json = json.replaceString("\"", withString: "#")
            //println("json: \(json)")
            json = CompresJSON.encryptAndCompressAsNecessary(json)
            
            self.parameters = Dictionary<String, AnyObject>()
            self.parameters!["data"] = json
        }
        
        //println(self.parameters!["data"])
        //println(self.urlString)
        //println(CompresJSON.decryptAndDecompressAsNecessary(self.parameters!["data"] as! String))
        
        self.almofireRequest = request(self.method, self.urlString, parameters: self.parameters, encoding: ParameterEncoding.URL)
            .response{ (request, response, data, error) in
                
                if let e = error {
                    
                    println(e.localizedDescription)
                    
                    var alert = self.alertControllerForError(e, completion: { (retry) -> () in
                        
                        if retry {
                            
                            self.cancel()
                            self.exec()
                        }
                    })
                    
                    self.failDownload(e, alert: alert)
                }
                    
                else{
                    let json = JSON(data: data! as! NSData)
                    
                    let encryptedJson = json["data"].stringValue
                    let unencryptedJson = CompresJSON.decryptAndDecompressAsNecessary(encryptedJson) //.replaceString("#", withString: "\"")
                    
                    if let dataFromString = unencryptedJson.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false){
                        
                        let unpackedJson = JSON(data: dataFromString)
                        
                        self.succeedDownload(unpackedJson)
                    }
                    else {
                        println("got nil - retrying")
                        self.cancel()
                        self.exec()
                    }
                    
                    
                }
                
                self.finishDownload()
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        }
    }
    
}
{'fraction_non_alphanumeric': 0.08785454545454545, 'fraction_numerical': 0.004072727272727273, 'mean_word_length': 1.3833622183708838, 'pattern_counts': {'":': 0, '<': 15, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4433645', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1573, 'n_words': 314}