<commit_before>/*
* Copyright (c) 2015, Tidepool Project
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the associated License, which is identical to the BSD 2-Clause
* License as published by the Open Source Initiative at opensource.org.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the License for more details.
*
* You should have received a copy of the License along with this program; if
* not, you can obtain one from Tidepool Project at tidepool.org.
*/

import Foundation
import UIKit
import Alamofire
import SwiftyJSON
import CoreData

/*
    Most of the service interface is exposed via the data classes. However, for login/logout, some set of service interface functions are needed. Much of the needed login code should be portable from Urchin.
*/

class APIConnector {
    // MARK: Properties
    
    
    private var _currentUser: User?
    var currentUser: User? {
        get {
            if _currentUser == nil {
                let ad = UIApplication.sharedApplication().delegate as! AppDelegate
                if let user = DatabaseUtils.getUser(ad.managedObjectContext) {
                    _currentUser = user
                }
            }
            return _currentUser
        }
        set(newUser) {
            if newUser != _currentUser {
                DatabaseUtils.updateUser(_currentUser, newUser: newUser)
                _currentUser = newUser
                if newUser != nil {
                    NSLog("Set currentUser, name: \(newUser!.username), id: \(newUser!.userid)")
                } else {
                    NSLog("Cleared currentUser!")
                }
            }
        }
    }
    
    // MARK: - Constants
    
    static let kSessionTokenDefaultKey = "SToken"
    static let kCurrentServiceDefaultKey = "SCurrentService"
    static let kSessionIdHeader = "x-tidepool-session-token"

    // Error domain and codes
    static let kNutshellErrorDomain = "NutshellErrorDomain"
    static let kNoSessionTokenErrorCode = -1
    
    // Session token, acquired on login and saved in NSUserDefaults
    private var _rememberToken = true
    private var _sessionToken: String?
    var sessionToken: String? {
        set(newToken) {
            if ( newToken != nil  && _rememberToken) {
                NSUserDefaults.standardUserDefaults().setValue(newToken, forKey: APIConnector.kSessionTokenDefaultKey)
            } else {
                NSUserDefaults.standardUserDefaults().removeObjectForKey(APIConnector.kSessionTokenDefaultKey)
            }
            _sessionToken = newToken
        }
        get {
            return _sessionToken
        }
    }
    
    // Dictionary of servers and their base URLs
    static let kServers = [
        "Production" :   "https://api.tidepool.org",
        "Staging" :      "https://staging-api.tidepool.org",
        "Development" :  "https://devel-api.tidepool.io",
    ]
    static let kDefaultServerName = "Production"

    static var _currentService: String?
    static var currentService: String? {
        set(newService) {
            if newService == nil {
                NSUserDefaults.standardUserDefaults().removeObjectForKey(APIConnector.kCurrentServiceDefaultKey)
                _currentService = nil
            } else {
                if APIConnector.kServers[newService!] != nil {
                    NSUserDefaults.standardUserDefaults().setValue(newService, forKey: APIConnector.kCurrentServiceDefaultKey)
                    _currentService = newService
                }
            }
        }<commit_msg>Change service urls per Darin.<commit_after>/*
* Copyright (c) 2015, Tidepool Project
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the associated License, which is identical to the BSD 2-Clause
* License as published by the Open Source Initiative at opensource.org.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the License for more details.
*
* You should have received a copy of the License along with this program; if
* not, you can obtain one from Tidepool Project at tidepool.org.
*/

import Foundation
import UIKit
import Alamofire
import SwiftyJSON
import CoreData

/*
    Most of the service interface is exposed via the data classes. However, for login/logout, some set of service interface functions are needed. Much of the needed login code should be portable from Urchin.
*/

class APIConnector {
    // MARK: Properties
    
    
    private var _currentUser: User?
    var currentUser: User? {
        get {
            if _currentUser == nil {
                let ad = UIApplication.sharedApplication().delegate as! AppDelegate
                if let user = DatabaseUtils.getUser(ad.managedObjectContext) {
                    _currentUser = user
                }
            }
            return _currentUser
        }
        set(newUser) {
            if newUser != _currentUser {
                DatabaseUtils.updateUser(_currentUser, newUser: newUser)
                _currentUser = newUser
                if newUser != nil {
                    NSLog("Set currentUser, name: \(newUser!.username), id: \(newUser!.userid)")
                } else {
                    NSLog("Cleared currentUser!")
                }
            }
        }
    }
    
    // MARK: - Constants
    
    static let kSessionTokenDefaultKey = "SToken"
    static let kCurrentServiceDefaultKey = "SCurrentService"
    static let kSessionIdHeader = "x-tidepool-session-token"

    // Error domain and codes
    static let kNutshellErrorDomain = "NutshellErrorDomain"
    static let kNoSessionTokenErrorCode = -1
    
    // Session token, acquired on login and saved in NSUserDefaults
    private var _rememberToken = true
    private var _sessionToken: String?
    var sessionToken: String? {
        set(newToken) {
            if ( newToken != nil  && _rememberToken) {
                NSUserDefaults.standardUserDefaults().setValue(newToken, forKey: APIConnector.kSessionTokenDefaultKey)
            } else {
                NSUserDefaults.standardUserDefaults().removeObjectForKey(APIConnector.kSessionTokenDefaultKey)
            }
            _sessionToken = newToken
        }
        get {
            return _sessionToken
        }
    }
    
    // Dictionary of servers and their base URLs
    static let kServers = [
        "Production" :   "https://api.tidepool.org",
        "Staging" :      "https://stg-api.tidepool.org",
        "Development" :  "https://dev-api.tidepool.org",
    ]
    static let kDefaultServerName = "Production"

    static var _currentService: String?
    static var currentService: String? {
        set(newService) {
            if newService == nil {
                NSUserDefaults.standardUserDefaults().removeObjectForKey(APIConnector.kCurrentServiceDefaultKey)
                _currentService = nil
            } else {
                if APIConnector.kServers[newService!] != nil {
                    NSUserDefaults.standardUserDefaults().setValue(newService, forKey: APIConnector.kCurrentServiceDefaultKey)
                    _currentService = newService
                }
            }
        }
{'fraction_non_alphanumeric': 0.07209737827715355, 'fraction_numerical': 0.0016051364365971107, 'mean_word_length': 2.1898464163822524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, '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': '2127906', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1879, 'n_words': 600}