<commit_before>//
//  OAuthSwift.swift
//  OAuthSwift
//
//  Created by phimage on 04/12/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public class OAuthSwift: NSObject {
    
    // MARK: Properties
    
    // Client to make signed request
    public var client: OAuthSwiftClient
    // Version of the protocol
    public var version: OAuthSwiftCredential.Version { return self.client.credential.version }
    
    // Handle the authorize url into a web view or browser
    public var authorize_url_handler: OAuthSwiftURLHandlerType = OAuthSwiftOpenURLExternally.sharedInstance

    // MARK: callback alias
    public typealias TokenSuccessHandler = (credential: OAuthSwiftCredential, response: NSURLResponse?, parameters: Dictionary<String, String>) -> Void
    public typealias FailureHandler = (error: NSError) -> Void

    // MARK: init
    init(consumerKey: String, consumerSecret: String) {
        self.client = OAuthSwiftClient(consumerKey: consumerKey, consumerSecret: consumerSecret)
    }

    // MARK: callback notification
    struct CallbackNotification {
        static let notificationName = "OAuthSwiftCallbackNotificationName"
        static let optionsURLKey = "OAuthSwiftCallbackNotificationOptionsURLKey"
    }

    // Handle callback url which contains now token information
    public class func handleOpenURL(url: NSURL) {
        let notification = NSNotification(name: CallbackNotification.notificationName, object: nil,
            userInfo: [CallbackNotification.optionsURLKey: url])
        NSNotificationCenter.defaultCenter().postNotification(notification)
    }
    
    var observer: AnyObject?

    func observeCallback(block: (url: NSURL) -> Void) {
        self.observer = NSNotificationCenter.defaultCenter().addObserverForName(CallbackNotification.notificationName, object: nil, queue: NSOperationQueue.mainQueue()){
            notification in
            NSNotificationCenter.defaultCenter().removeObserver(self.observer!)

            let urlFromUserInfo = notification.userInfo![CallbackNotification.optionsURLKey] as! NSURL
            block(url: urlFromUserInfo)
        }
    }

}

// MARK: OAuthSwift errors
public let OAuthSwiftErrorDomain = "oauthswift.error"
<commit_msg>Allow to remove callback observer if callback url never called<commit_after>//
//  OAuthSwift.swift
//  OAuthSwift
//
//  Created by phimage on 04/12/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public class OAuthSwift: NSObject {
    
    // MARK: Properties
    
    // Client to make signed request
    public var client: OAuthSwiftClient
    // Version of the protocol
    public var version: OAuthSwiftCredential.Version { return self.client.credential.version }
    
    // Handle the authorize url into a web view or browser
    public var authorize_url_handler: OAuthSwiftURLHandlerType = OAuthSwiftOpenURLExternally.sharedInstance

    // MARK: callback alias
    public typealias TokenSuccessHandler = (credential: OAuthSwiftCredential, response: NSURLResponse?, parameters: Dictionary<String, String>) -> Void
    public typealias FailureHandler = (error: NSError) -> Void

    // MARK: init
    init(consumerKey: String, consumerSecret: String) {
        self.client = OAuthSwiftClient(consumerKey: consumerKey, consumerSecret: consumerSecret)
    }

    // MARK: callback notification
    struct CallbackNotification {
        static let notificationName = "OAuthSwiftCallbackNotificationName"
        static let optionsURLKey = "OAuthSwiftCallbackNotificationOptionsURLKey"
    }

    // Handle callback url which contains now token information
    public class func handleOpenURL(url: NSURL) {
        let notification = NSNotification(name: CallbackNotification.notificationName, object: nil,
            userInfo: [CallbackNotification.optionsURLKey: url])
        notificationCenter.postNotification(notification)
    }

    var observer: AnyObject?
    class var notificationCenter: NSNotificationCenter {
        return NSNotificationCenter.defaultCenter()
    }

    func observeCallback(block: (url: NSURL) -> Void) {
        self.observer = OAuthSwift.notificationCenter.addObserverForName(CallbackNotification.notificationName, object: nil, queue: NSOperationQueue.mainQueue()){
            notification in
            self.removeCallbackNotificationObserver()

            let urlFromUserInfo = notification.userInfo![CallbackNotification.optionsURLKey] as! NSURL
            block(url: urlFromUserInfo)
        }
    }

    public func removeCallbackNotificationObserver() {
      	if let observer = self.observer {
            OAuthSwift.notificationCenter.removeObserver(observer)
        }
    }

}

// MARK: OAuthSwift errors
public let OAuthSwiftErrorDomain = "oauthswift.error"

{'fraction_non_alphanumeric': 0.07716371220020855, 'fraction_numerical': 0.004171011470281543, 'mean_word_length': 3.4738805970149254, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '3072058', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1185, 'n_words': 333}