<commit_before>//
//  FSSession.swift
//  Quadrat
//
//  Created by <NAME> on 26/10/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

/** A handler used for authorization. */
public typealias AuthorizationHandler = (Bool, NSError?) -> Void

/** A nandler used by all endpoints. */
public typealias ResponseClosure = (result: Result) -> Void

/** A nandler for downloading images. */
public typealias DownloadImageClosure = (imageData: NSData?, error: NSError?) -> Void

/** Typealias for parameters dictionary. */
public typealias Parameters = [String:String]

/**
    Posted when session have access token, but server returns response with 401 HTTP code.
    Guaranteed to be posted an main thread.
*/
public let QuadratSessionDidBecomeUnauthorizedNotification = "QuadratSessionDidBecomeUnauthorizedNotification"

private var _sharedSession : Session?

public class Session {
    
    /** The coniguration. */
    let configuration       : Configuration
    
    /** The session which perform all network requests. */
    let URLSession          : NSURLSession
    
    /** The current authorizer. */
    var authorizer          : Authorizer?
    
    /** Used as image cache for downloaded files. */
    let dataCache           : DataCache
    
    /** The queue on which tasks have to call completion handlers. */
    let completionQueue     : NSOperationQueue
   
    /** The keychain. */
    let keychain            : Keychain
    
    /** Manages network activity indicator. */
    var networkActivityController : NetworkActivityIndicatorController?

    /**
        One can create custom logger to process all errors and responses in one place.
        Main purpose is to debug or to track all the errors accured in framework via some analytic tool.
    */
    public var logger       : Logger?
    
    public lazy var users : Users = {
        return Users(session: self)
        }()
    
    public lazy var venues : Venues = {
        return Venues(session: self)
        }()
    
    public lazy var venueGroups : VenueGroups = {
        return VenueGroups(session: self)
        }()
    
    public lazy var checkins : Checkins = {
        return Checkins(session: self)
        }()
    
    public lazy var tips : Tips = {
        return Tips(session: self)
        }()
    
    public lazy var lists : Lists = {
        return Lists(session: self)
        }()
    
    public lazy var updates : Updates = {
        return Updates(session: self)
        }()
    
    public lazy var photos : Photos = {
        return Photos(session: self)
        }()
    
    public lazy var settings : Settings = {
        return Settings(session: self)
        }()
    
    public lazy var specials : Specials = {
        return Specials(session: self)
        }()
    
    public lazy var events : Events = {
        return Events(session: self)
        }()
    
    public lazy var pages : Pages = {
        return Pages(session: self)
        }()
    
    public lazy var pageUpdates : PageUpdates = {
        return PageUpdates(session: self)
        }()
    
    public lazy var multi : Multi = {
        return Multi(session: self)
        }()
    
    public init(configuration: Configuration, completionQueue: NSOperationQueue) {
        if configuration.shouldControllNetworkActivityIndicator {
            networkActivityController = NetworkActivityIndicatorController()
        }
        self.configuration = configuration
        self.completionQueue = completionQueue
        let URLConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
        URLConfiguration.timeoutIntervalForRequest = configuration.timeoutInterval
        let delegateQueue = NSOperationQueue()
        delegateQueue.maxConcurrentOperationCount = 1
        self.URLSession = NSURLSession(configuration: URLConfiguration, delegate: nil, delegateQueue: delegateQueue)
        dataCache = DataCache(name: configuration.userTag)
        if configuration.debugEnabled {
            self.logger = ConsoleLogger()
        }
        keychain = Keychain(configuration: self.configuration)
        keychain.logger = self.logger
    }
    
    public convenience init(configuration: Configuration) {
        self.init(configuration:configuration, completionQueue: NSOperationQueue.mainQueue())
    }
    
    public class func setupSharedSessionWithConfiguration(configuration: Configuration) {
        if _sharedSession == nil {
            _sharedSession = Session(configuration: configuration)
        } else {
            fatalError("You shouldn't call call setupSharedSessionWithConfiguration twice!")
        }
    }
    
    public class func sharedSession() -> Session {
        if _sharedSession == nil {
            fatalError("You must call setupSharedInstanceWithConfiguration before!")
        }
        return _sharedSession!
    }
    
    /** Whether session is authorized or not. */
    public func isAuthorized() -> Bool {
        do {
            let accessToken = try keychain.accessToken()
            return accessToken != nil
        } catch {
            return false
        }
    }
    
    /** 
        Removes access token from keychain.
        This method doesn't post `QuadratSessionDidBecomeUnauthorizedNotification`.
    */
    public func deauthorize() {
        do {
            try keychain.deleteAccessToken()<commit_msg>Allow for setting up shared session to use a different completion queue<commit_after>//
//  FSSession.swift
//  Quadrat
//
//  Created by <NAME> on 26/10/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

/** A handler used for authorization. */
public typealias AuthorizationHandler = (Bool, NSError?) -> Void

/** A nandler used by all endpoints. */
public typealias ResponseClosure = (result: Result) -> Void

/** A nandler for downloading images. */
public typealias DownloadImageClosure = (imageData: NSData?, error: NSError?) -> Void

/** Typealias for parameters dictionary. */
public typealias Parameters = [String:String]

/**
    Posted when session have access token, but server returns response with 401 HTTP code.
    Guaranteed to be posted an main thread.
*/
public let QuadratSessionDidBecomeUnauthorizedNotification = "QuadratSessionDidBecomeUnauthorizedNotification"

private var _sharedSession : Session?

public class Session {
    
    /** The coniguration. */
    let configuration       : Configuration
    
    /** The session which perform all network requests. */
    let URLSession          : NSURLSession
    
    /** The current authorizer. */
    var authorizer          : Authorizer?
    
    /** Used as image cache for downloaded files. */
    let dataCache           : DataCache
    
    /** The queue on which tasks have to call completion handlers. */
    let completionQueue     : NSOperationQueue
   
    /** The keychain. */
    let keychain            : Keychain
    
    /** Manages network activity indicator. */
    var networkActivityController : NetworkActivityIndicatorController?

    /**
        One can create custom logger to process all errors and responses in one place.
        Main purpose is to debug or to track all the errors accured in framework via some analytic tool.
    */
    public var logger       : Logger?
    
    public lazy var users : Users = {
        return Users(session: self)
        }()
    
    public lazy var venues : Venues = {
        return Venues(session: self)
        }()
    
    public lazy var venueGroups : VenueGroups = {
        return VenueGroups(session: self)
        }()
    
    public lazy var checkins : Checkins = {
        return Checkins(session: self)
        }()
    
    public lazy var tips : Tips = {
        return Tips(session: self)
        }()
    
    public lazy var lists : Lists = {
        return Lists(session: self)
        }()
    
    public lazy var updates : Updates = {
        return Updates(session: self)
        }()
    
    public lazy var photos : Photos = {
        return Photos(session: self)
        }()
    
    public lazy var settings : Settings = {
        return Settings(session: self)
        }()
    
    public lazy var specials : Specials = {
        return Specials(session: self)
        }()
    
    public lazy var events : Events = {
        return Events(session: self)
        }()
    
    public lazy var pages : Pages = {
        return Pages(session: self)
        }()
    
    public lazy var pageUpdates : PageUpdates = {
        return PageUpdates(session: self)
        }()
    
    public lazy var multi : Multi = {
        return Multi(session: self)
        }()
    
    public init(configuration: Configuration, completionQueue: NSOperationQueue = NSOperationQueue.mainQueue()) {
        if configuration.shouldControllNetworkActivityIndicator {
            networkActivityController = NetworkActivityIndicatorController()
        }
        self.configuration = configuration
        self.completionQueue = completionQueue
        let URLConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
        URLConfiguration.timeoutIntervalForRequest = configuration.timeoutInterval
        let delegateQueue = NSOperationQueue()
        delegateQueue.maxConcurrentOperationCount = 1
        self.URLSession = NSURLSession(configuration: URLConfiguration, delegate: nil, delegateQueue: delegateQueue)
        dataCache = DataCache(name: configuration.userTag)
        if configuration.debugEnabled {
            self.logger = ConsoleLogger()
        }
        keychain = Keychain(configuration: self.configuration)
        keychain.logger = self.logger
    }
    
    public class func setupSharedSessionWithConfiguration(configuration: Configuration, completionQueue: NSOperationQueue = NSOperationQueue.mainQueue()) {
        if _sharedSession == nil {
            _sharedSession = Session(configuration: configuration, completionQueue: completionQueue)
        } else {
            fatalError("You shouldn't call call setupSharedSessionWithConfiguration twice!")
        }
    }
    
    public class func sharedSession() -> Session {
        if _sharedSession == nil {
            fatalError("You must call setupSharedInstanceWithConfiguration before!")
        }
        return _sharedSession!
    }
    
    /** Whether session is authorized or not. */
    public func isAuthorized() -> Bool {
        do {
            let accessToken = try keychain.accessToken()
            return accessToken != nil
        } catch {
            return false
        }
    }
    
    /** 
        Removes access token from keychain.
        This method doesn't post `QuadratSessionDidBecomeUnauthorizedNotification`.
    */
    public func deauthorize() {
        do {
            try keychain.deleteAccessToken()
{'fraction_non_alphanumeric': 0.0791913946587537, 'fraction_numerical': 0.0025964391691394658, 'mean_word_length': 2.3472998137802605, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '7028781', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2642, 'n_words': 841}