<commit_before>//
//  AskCredential.swift
//
//  Copyright © 2017 Niji. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import UIKit
import RxSwift

/** 

AskCredential

# Description

This struct define the behavior when NetworkStack can't find how to provide token for request which need authorization.

# Usage

- Use triggerCondition to define for what error you need to call the handler.
by default 401 error trigger handler.

- Your provided handler must fetch new token and update the NetworkStack with it.

*/
public struct AskCredential {
  
  // MARK: - Type aliases
  
  public typealias AskCredentialHandler = (() -> Observable<Void>)
  public typealias AskCredentialTriggerCondition = ((Error) -> Bool)
  
  // MARK: - Properties
  
  public var triggerCondition: AskCredentialTriggerCondition
  public var handler: AskCredentialHandler
  
  // MARK: - Setup
  
  public init(triggerCondition: @escaping AskCredentialTriggerCondition, handler: @escaping AskCredentialHandler) {
    self.triggerCondition = triggerCondition
    self.handler = handler
  }
  
  public init(handler: @escaping AskCredentialHandler) {
    self.handler = handler
    self.triggerCondition = AskCredential.defaultTriggerCondition()
  }
  
  // MARK: - Private
  
  private static func defaultTriggerCondition() -> AskCredentialTriggerCondition {
    let triggerCondition = { (error: Error) -> Bool in
      return AskCredential.shouldAskCredential(forError: error)
    }
    return triggerCondition
  }
  
  private static func shouldAskCredential(forError error: Error) -> Bool {
    var shouldAskCredentials = false
    if case NetworkStackError.http(httpURLResponse: let httpURLResponse, data: _) = error, httpURLResponse.statusCode == 401 {
      shouldAskCredentials = true
    }
    return shouldAskCredentials
  }
}<commit_msg>AskCredentials : Optimize default trigger condition fonctions<commit_after>//
//  AskCredential.swift
//
//  Copyright © 2017 Niji. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import UIKit
import RxSwift

/** 

AskCredential

# Description

This struct define the behavior when NetworkStack can't find how to provide token for request which need authorization.

# Usage

- Use triggerCondition to define for what error you need to call the handler.
by default 401 error trigger handler.

- Your provided handler must fetch new token and update the NetworkStack with it.

*/
public struct AskCredential {
  
  // MARK: - Type aliases
  
  public typealias AskCredentialHandler = (() -> Observable<Void>)
  public typealias AskCredentialTriggerCondition = ((Error) -> Bool)
  
  // MARK: - Properties
  
  public var triggerCondition: AskCredentialTriggerCondition
  public var handler: AskCredentialHandler
  
  // MARK: - Setup
  
  public init(triggerCondition: @escaping AskCredentialTriggerCondition, handler: @escaping AskCredentialHandler) {
    self.triggerCondition = triggerCondition
    self.handler = handler
  }
  
  public init(handler: @escaping AskCredentialHandler) {
    self.handler = handler
    self.triggerCondition = AskCredential.defaultTriggerCondition
  }
  
  // MARK: - Private
  
  private static func defaultTriggerCondition(error: Error) -> Bool {
    var shouldAskCredentials = false
    if case NetworkStackError.http(httpURLResponse: let httpURLResponse, data: _) = error, httpURLResponse.statusCode == 401 {
      shouldAskCredentials = true
    }
    return shouldAskCredentials
  }

}
{'fraction_non_alphanumeric': 0.07576761652308373, 'fraction_numerical': 0.006185111552904793, 'mean_word_length': 3.610997963340122, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2198799', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1181, 'n_words': 450}