<commit_before>//
//  CaveAPI.swift
//  CaveCommentViewer
//
//  Created by 月下 on 2015/12/29.
//  Copyright © 2015年 月下. All rights reserved.
//
/*
以降すべてAPI関連はこのAPIを通して行うようにする
*/
import Foundation
import SwiftyJSON
import KeychainAccess

class CaveAPI {
    private let ud = NSUserDefaults.standardUserDefaults()
    private let keychain = Keychain(service: "net.cavelis.gae")
    
    var devKey: String {
        get {
            return "<KEY>"
        }
    }
    
    private(set) var apiKey: String {
        get {
            return self.ud.stringForKey("auth_api")!
        }
        set(apiKey) {
            self.ud.setObject(apiKey, forKey: "auth_api")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_user: String {
        get {
            return self.ud.stringForKey("auth_user")!
        }
        set(auth_user) {
            self.ud.setObject(auth_user, forKey: "auth_user")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_pass: String {
        get {
            return try! keychain.getString("auth_pass")!
        }
        set(auth_pass) {
            try! keychain.set(auth_pass, key: "auth_pass")
        }
    }
    
    private(set) var cookieKey: [NSHTTPCookie]? {
        get {
            if let data = self.ud.objectForKey("HTTPCookieKey"){
                let cookie = NSKeyedUnarchiver.unarchiveObjectWithData(data as! NSData) as! [NSHTTPCookie]
                return cookie
            }
            return nil
        }
        set(cookieKey) {
            var data:NSData! = nil
            if cookieKey != nil{
                data = NSKeyedArchiver.archivedDataWithRootObject(cookieKey!)
            }
            self.ud.setObject(data, forKey: "HTTPCookieKey")
            self.ud.synchronize()
        }
    }
    
    private(set) var accessKey: String {
        get {
            return self.ud.stringForKey("access_key")!
        }
        set(accessKey) {
            self.ud.setObject(accessKey, forKey: "access_key")
            self.ud.synchronize()
        }
    }
    
    func InitData(){
        self.accessKey = ""
        self.apiKey = ""
        self.auth_user = ""
        self.auth_pass = ""
        self.cookieKey = nil
    }
    
    //    init(){print("APIinit")}
    //
    //    deinit{print("APIdeinit")}
    
    internal func Login(user user:String,pass:String,regist:(Bool)->Void){
        let str = "user_name=\(user)&password=\(<PASSWORD>)"
        let strData = str.dataUsingEncoding(NSUTF8StringEncoding)
        let url:NSURL = NSURL(string: "http://gae.cavelis.net/auth/login")!
        let request = NSMutableURLRequest(URL: url, cachePolicy:NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData,timeoutInterval: 10)
        request.HTTPMethod = "POST"
        request.HTTPBody = strData
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            
            let json = JSON(data: data!)
            if json.count != 0{
                
                let resp = response as? NSHTTPURLResponse
                let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(resp!.allHeaderFields as! [String : String], forURL: response!.URL!)
                NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(cookies, forURL: response!.URL!, mainDocumentURL: nil)
                self.cookieKey = cookies
                self.auth_user = user
                self.auth_pass = <PASSWORD>
                
                regist(true)
            }else{
                
                regist(false)
            }
        }
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func Logout(res:(Bool)->Void){
        let str = "http://gae.cavelis.net/auth/logout"
        let url = NSURL(string:str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)!
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            
            self.cookieKey = nil
            self.apiKey = ""
            self.auth_user = ""
            self.auth_pass = ""
            
            res(true)
        }
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func getAPIKey(success:(String?)->Void){
        
        if self.auth_user == "" || self.auth_pass == ""{
            return
        }
        
        let str = "devkey=\(self.devKey)&user=\(self.auth_user)&pass=\(self.auth_pass)&mode=login"
        let strData = str.dataUsingEncoding(NSUTF8StringEncoding)
        let url:NSURL = NSURL(string: "http://gae.cavelis.net/api/auth")!
        
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.HTTPBody = strData
        
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            
            let json = JSON(data:data!)
            if json["ret"] ==  true {
                self.apiKey = json["apikey"].string!
                success(json["apikey"].string)
            }else{
                success(nil)
            }
        }
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func getAccessKey(){
        let str:String = self.accessKey != "" ? "key=\(self.accessKey)" : "key="
        let url:NSURL = NSURL(string: "http://ws.cavelis.net/accesskey?\(str)")!
        let request = NSMutableURLRequest(URL: url)
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            let json = JSON(data:data!)
            if json == nil{
                print("AccessKey取得失敗")
                return
            }
            if json["accessKey"].stringValue != self.accessKey{
                self.accessKey = json["accessKey"].stringValue
                print("アクセスキーの更新終了")
            }else{
                print("アクセスキーの更新はありません")
            }
        }
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func uploadImage(imgdata imgdata:NSData, session:String, slot:Int?, res:(Bool,String)->Void){
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        let url:NSURL! = NSURL(string:"http://gae.cavelis.net/useredit/getvalidkey")
        let request = NSMutableURLRequest(URL: url)
        
        if self.cookieKey != nil{
            request.allHTTPHeaderFields =  NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookieKey!)
        }else{
            res(false,"認証エラー")
        }
        
        let str = "_session=\(session)"
        let strData = str.dataUsingEncoding(NSUTF8StringEncoding)
        
        request.HTTPMethod = "POST"
        request.HTTPBody = strData
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                res(false,"")
            } else {
                let valid = JSON(data:data!)["valid"].stringValue
                
                self.uploadData(imgdata: imgdata, valid: valid, slot: slot,resp: {resp in
                    if resp{
                        res(true,"画像のアップロードに成功しました")
                    }else{
                        res(false,"画像のアップロードに失敗しました")
                    }
                    
                })
            }
            
        }
        task.resume()
    }
    
    func deleteImage(session session:String,slot:Int?,res:(Bool,String) -> Void){<commit_msg>FavoriteUser関係の追加<commit_after>//
//  CaveAPI.swift
//  CaveCommentViewer
//
//  Created by 月下 on 2015/12/29.
//  Copyright © 2015年 月下. All rights reserved.
//
/*
以降すべてAPI関連はこのAPIを通して行うようにする
*/
import Foundation
import SwiftyJSON
import KeychainAccess

class CaveAPI {
    private let ud = NSUserDefaults.standardUserDefaults()
    private let keychain = Keychain(service: "net.cavelis.gae")
    
    var devKey: String {
        get {
            return "<KEY>"
        }
    }
    
    private(set) var apiKey: String {
        get {
            return self.ud.stringForKey("auth_api")!
        }
        set(apiKey) {
            self.ud.setObject(apiKey, forKey: "auth_api")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_user: String {
        get {
            return self.ud.stringForKey("auth_user")!
        }
        set(auth_user) {
            self.ud.setObject(auth_user, forKey: "auth_user")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_pass: String {
        get {
            return try! keychain.getString("auth_pass")!
        }
        set(auth_pass) {
            try! keychain.set(auth_pass, key: "auth_pass")
        }
    }
    
    private(set) var cookieKey: [NSHTTPCookie]? {
        get {
            if let data = self.ud.objectForKey("HTTPCookieKey"){
                let cookie = NSKeyedUnarchiver.unarchiveObjectWithData(data as! NSData) as! [NSHTTPCookie]
                return cookie
            }
            return nil
        }
        set(cookieKey) {
            var data:NSData! = nil
            if cookieKey != nil{
                data = NSKeyedArchiver.archivedDataWithRootObject(cookieKey!)
            }
            self.ud.setObject(data, forKey: "HTTPCookieKey")
            self.ud.synchronize()
        }
    }
    
    private(set) var accessKey: String {
        get {
            return self.ud.stringForKey("access_key")!
        }
        set(accessKey) {
            self.ud.setObject(accessKey, forKey: "access_key")
            self.ud.synchronize()
        }
    }
    
    private(set) var favUser:[String]{
        get{
            return self.ud.arrayForKey("favUser") as! [String]
        }
        set(favUser){
            self.ud.setObject(favUser, forKey: "favUser")
            self.ud.synchronize()
        }
    }
    
    internal func searchUser(name name:String) -> Bool{
        let users = self.favUser
        
        if users.indexOf(name) != nil {
            return true
        }else{
                return false
        }
    }
    
    internal func addUser(name name:String) -> Void{
        var users = self.favUser
        
        if users.indexOf(name) == nil {
            users.append(name)
            self.favUser = users
        }

        return
    }
    
    internal func deleteUser(name name:String) -> Bool{
        var users = self.favUser
        let index = users.indexOf(name)
        
        if index != nil {
            users.removeAtIndex(index!)
            self.favUser = users
            
            return true
        }
        
        return false
    }
    
    func InitData(){
        self.accessKey = ""
        self.apiKey = ""
        self.auth_user = ""
        self.auth_pass = ""
        self.cookieKey = nil
        self.favUser = []
    }
    
    internal func Login(user user:String,pass:String,regist:(Bool)->Void){
        let str = "user_name=\(user)&password=\(pass)"
        let strData = str.dataUsingEncoding(NSUTF8StringEncoding)
        let url:NSURL = NSURL(string: "http://gae.cavelis.net/auth/login")!
        let request = NSMutableURLRequest(URL: url, cachePolicy:NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData,timeoutInterval: 10)
        request.HTTPMethod = "POST"
        request.HTTPBody = strData
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            
            let json = JSON(data: data!)
            if json.count != 0{
                
                let resp = response as? NSHTTPURLResponse
                let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(resp!.allHeaderFields as! [String : String], forURL: response!.URL!)
                NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(cookies, forURL: response!.URL!, mainDocumentURL: nil)
                self.cookieKey = cookies
                self.auth_user = user
                self.auth_pass = <PASSWORD>
                
                regist(true)
            }else{
                
                regist(false)
            }
        }
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func Logout(res:(Bool)->Void){
        let str = "http://gae.cavelis.net/auth/logout"
        let url = NSURL(string:str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)!
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            
            self.cookieKey = nil
            self.apiKey = ""
            self.auth_user = ""
            self.auth_pass = ""
            
            res(true)
        }
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func getAPIKey(success:(String?)->Void){
        
        if self.auth_user == "" || self.auth_pass == ""{
            return
        }
        
        let str = "devkey=\(self.devKey)&user=\(self.auth_user)&pass=\(self.<PASSWORD>)&mode=login"
        let strData = str.dataUsingEncoding(NSUTF8StringEncoding)
        let url:NSURL = NSURL(string: "http://gae.cavelis.net/api/auth")!
        
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.HTTPBody = strData
        
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            
            let json = JSON(data:data!)
            if json["ret"] ==  true {
                self.apiKey = json["apikey"].string!
                success(json["apikey"].string)
            }else{
                success(nil)
            }
        }
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func getAccessKey(){
        let str:String = self.accessKey != "" ? "key=\(self.accessKey)" : "key="
        let url:NSURL = NSURL(string: "http://ws.cavelis.net/accesskey?\(str)")!
        let request = NSMutableURLRequest(URL: url)
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            if error != nil {
                errorStatus.offlineError(error: error!)
                return
            }
            let json = JSON(data:data!)
            if json == nil{
                print("AccessKey取得失敗")
                return
            }
            if json["accessKey"].stringValue != self.accessKey{
                self.accessKey = json["accessKey"].stringValue
                print("アクセスキーの更新終了")
            }else{
                print("アクセスキーの更新はありません")
            }
        }
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        task.resume()
    }
    
    internal func uploadImage(imgdata imgdata:NSData, session:String, slot:Int?, res:(Bool,String)->Void){
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        let url:NSURL! = NSURL(string:"http://gae.cavelis.net/useredit/getvalidkey")
        let request = NSMutableURLRequest(URL: url)
        
        if self.cookieKey != nil{
            request.allHTTPHeaderFields =  NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookieKey!)
        }else{
            res(false,"認証エラー")
        }
        
        let str = "_session=\(session)"
        let strData = str.dataUsingEncoding(NSUTF8StringEncoding)
        
        request.HTTPMethod = "POST"
        request.HTTPBody = strData
        let task : NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            
            if error != nil {
                res(false,"")
            } else {
                let valid = JSON(data:data!)["valid"].stringValue
                
                self.uploadData(imgdata: imgdata, valid: valid, slot: slot,resp: {resp in
                    if resp{
                        res(true,"画像のアップロードに成功しました")
                    }else{
                        res(false,"画像のアップロードに失敗しました")
                    }
                    
                })
            }
            
        }
        task.resume()
    }
    
    internal func deleteImage(session session:String,slot:Int?,res:(Bool,String) -> Void){
{'fraction_non_alphanumeric': 0.10358692201883399, 'fraction_numerical': 0.0019045603639826474, 'mean_word_length': 1.7499272621472215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 32, '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': '5262504', 'n_tokens_mistral': 5389, 'n_tokens_neox': 5254, 'n_words': 920}