<commit_before>//
//  GetRequestViewController.swift
//  WTKit
//
//  Created by SongWentong on 4/21/16.
//  Copyright © 2016 SongWentong. All rights reserved.
//

import UIKit
import Foundation
class GetRequestViewController: UIViewController,POSTParamatersVCDelegate {
    
    @IBOutlet weak var requestButton: UIButton!
    @IBOutlet weak var urlTextField: UITextField!
    @IBOutlet weak var resultTextView: UITextView!
    @IBOutlet weak var methodSegment: UISegmentedControl!
    @IBOutlet weak var rightItem: UIBarButtonItem!
    var parameters:[String:String]?
    
    override func viewDidLoad() {
        //        requestButton.enabled = false
        
        checkTextLength()
        super.viewDidLoad()
//        self.navigationItem.hidesBackButton = true
    }
    
    @IBAction func EditChanged(sender: AnyObject) {
        
        checkTextLength()
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
//        WTPrint(self.navigationItem.leftBarButtonItem)
    }
    
    //根据当时文本框的内容来控制按钮是否可用
    func checkTextLength(){
        let string = urlTextField.text
        let count = string?.characters.count
        //如果长度不对就不让按按钮
        if (string == nil || count==0) {
            requestButton.enabled = false;
        }else{
            requestButton.enabled = true;
        }
    }
    @IBAction func methodChanged(sender: AnyObject) {
//        var enabled = true
//        if methodSegment.selectedSegmentIndex==0 {
//            enabled = false
//        }
//        rightItem.enabled = enabled
    }
    @IBAction func rightItemPressed(sender: AnyObject) {
        self.performSegueWithIdentifier("parameters", sender: nil)
    }
    
    @IBAction func requestButtonPressed(sender: AnyObject) {
        let string = urlTextField.text
        if string != nil {
//            let request = NSMutableURLRequest.request(string!)
//            let queue = NSURLSession.sharedSession().delegateQueue
//            WTPrint(queue)
            
            self.showLoadingView()
            requestButton.enabled = false
            var method = "GET"
            if methodSegment.selectedSegmentIndex == 1 {
                method = "POST"
            }
            NSURLSession.dataTaskWith(string!,method:method,parameters:parameters, completionHandler: { (data, response, error) in
                self.hideLoadingView()
                self.requestButton.enabled = true
                
                if error == nil{
                    <commit_msg>url 可储存<commit_after>//
//  GetRequestViewController.swift
//  WTKit
//
//  Created by SongWentong on 4/21/16.
//  Copyright © 2016 SongWentong. All rights reserved.
//

import UIKit
import Foundation
class GetRequestViewController: UIViewController,POSTParamatersVCDelegate {
    
    @IBOutlet weak var requestButton: UIButton!
    @IBOutlet weak var urlTextField: UITextField!
    @IBOutlet weak var resultTextView: UITextView!
    @IBOutlet weak var methodSegment: UISegmentedControl!
    @IBOutlet weak var rightItem: UIBarButtonItem!
    var parameters:[String:String]?
    
    
    let lastURLKey = "lastURLKey"
    
    override func viewDidLoad() {
        //        requestButton.enabled = false
        
        var url = NSUserDefaults.standardUserDefaults().stringForKey(lastURLKey)
        if url == nil {
            url = "http://www.baidu.com"
        }
        urlTextField.text = url
        
        checkTextLength()
        super.viewDidLoad()
        
//        self.navigationItem.hidesBackButton = true
    }
    
    @IBAction func EditChanged(sender: AnyObject) {
        
        checkTextLength()
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
//        WTPrint(self.navigationItem.leftBarButtonItem)
    }
    
    //根据当时文本框的内容来控制按钮是否可用
    func checkTextLength(){
        let string = urlTextField.text
        let count = string?.characters.count
        //如果长度不对就不让按按钮
        if (string == nil || count==0) {
            requestButton.enabled = false;
        }else{
            requestButton.enabled = true;
        }
    }
    @IBAction func methodChanged(sender: AnyObject) {
//        var enabled = true
//        if methodSegment.selectedSegmentIndex==0 {
//            enabled = false
//        }
//        rightItem.enabled = enabled
    }
    @IBAction func rightItemPressed(sender: AnyObject) {
        self.performSegueWithIdentifier("parameters", sender: nil)
    }
    
    @IBAction func requestButtonPressed(sender: AnyObject) {
        let string = urlTextField.text
        if string != nil {
//            let request = NSMutableURLRequest.request(string!)
//            let queue = NSURLSession.sharedSession().delegateQueue
//            WTPrint(queue)
            
            self.showLoadingView()
            requestButton.enabled = false
            var method = "GET"
            if methodSegment.selectedSegmentIndex == 1 {
                method = "POST"
            }
            NSUserDefaults.standardUserDefaults().setObject(urlTextField.text, forKey: lastURLKey)
            NSURLSession.dataTaskWith(string!,method:method,parameters:parameters, completionHandler: { (data, response, error) in
                self.hideLoadingView()
                self.requestButton.enabled = true
                
                if error == nil{
                    
{'fraction_non_alphanumeric': 0.08846790509472136, 'fraction_numerical': 0.004414199006805224, 'mean_word_length': 1.9994484280198566, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '6338735', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1510, 'n_words': 313}