<commit_before>//
//  RateLimit.swift
//  Sword
//
//  Created by <NAME>
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import Dispatch

/// Rate Limit
extension Sword {

  /**
   Gets the "route" for an HTTP request

   - parameter url: URL to get route from
  */
  func getRoute(for url: String) -> String {

  let regex = try! NSRegularExpression(pattern: "/([a-z-]+)/(?:[0-9]{17,})+?", options: .caseInsensitive)

  let string = NSString(string: url)
  let matches = regex.matches(in: url, options: [], range: NSMakeRange(0, string.length))

  guard matches.count > 0 else {
    return url
  }

  var route = ""

  for match in matches {
    let miniRoute = string.substring(with: match.range)
    var parameters = miniRoute.components(separatedBy: "/")

    parameters.remove(at: 0)

    let parameterId = parameters[1]
    parameters[1] = ":id"

    if (parameters[0] == "channels" || parameters[0] == "guilds") && route.isEmpty {
      parameters[1] = parameterId
    }

    route += "/" + parameters.joined(separator: "/")
  }

  return route
}

  /// Used to un clog the global queue full of requests that woudld've resulted in 429 because of global rate limit
  func globalUnlock() {
    self.isGloballyLocked = false
    for request in self.globalRequestQueue {
      request()
    }
  }

  /**
   Handles creating buckets, and making sure the bucket is up to date with the headers

   - parameter headers: The received headers from the request
  */
  func handleRateLimitHeaders(_ limitHeader: Any?, _ remainingHeader: Any?, _ intervalHeader: Any?, _ date: Double, _ route: String) {
    guard limitHeader != nil, remainingHeader != nil, intervalHeader != nil else {
      return
    }

    let limit = Int(limitHeader as! String)!
    let remaining = Int(remainingHeader as! String)!
    let interval = Int(Double(intervalHeader as! String)! - date)

    if self.rateLimits[route] == nil {
      let bucket = Bucket(name: "gg.azoy.sword.rest.\(route)", limit: limit, interval: interval)
      bucket.take(1)

      self.rateLimits[route] = bucket
    }else {
      if self.rateLimits[route]!.tokens != remaining {
        self.rateLimits[route]!.tokens = remaining<commit_msg>Fix style for getRoute<commit_after>//
//  RateLimit.swift
//  Sword
//
//  Created by <NAME>
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import Dispatch

/// Rate Limit
extension Sword {

  /**
   Gets the "route" for an HTTP request

   - parameter url: URL to get route from
  */
  func getRoute(for url: String) -> String {

    let regex = try! NSRegularExpression(pattern: "/([a-z-]+)/(?:[0-9]{17,})+?", options: .caseInsensitive)

    let string = NSString(string: url)
    let matches = regex.matches(in: url, options: [], range: NSMakeRange(0, string.length))

    guard matches.count > 0 else {
      return url
    }

    var route = ""

    for match in matches {
      let miniRoute = string.substring(with: match.range)
      var parameters = miniRoute.components(separatedBy: "/")

      parameters.remove(at: 0)

      let parameterId = parameters[1]
      parameters[1] = ":id"

      if (parameters[0] == "channels" || parameters[0] == "guilds") && route.isEmpty {
        parameters[1] = parameterId
      }

      route += "/" + parameters.joined(separator: "/")
    }

    return route
  }

  /// Used to un clog the global queue full of requests that woudld've resulted in 429 because of global rate limit
  func globalUnlock() {
    self.isGloballyLocked = false
    for request in self.globalRequestQueue {
      request()
    }
  }

  /**
   Handles creating buckets, and making sure the bucket is up to date with the headers

   - parameter headers: The received headers from the request
  */
  func handleRateLimitHeaders(_ limitHeader: Any?, _ remainingHeader: Any?, _ intervalHeader: Any?, _ date: Double, _ route: String) {
    guard limitHeader != nil, remainingHeader != nil, intervalHeader != nil else {
      return
    }

    let limit = Int(limitHeader as! String)!
    let remaining = Int(remainingHeader as! String)!
    let interval = Int(Double(intervalHeader as! String)! - date)

    if self.rateLimits[route] == nil {
      let bucket = Bucket(name: "gg.azoy.sword.rest.\(route)", limit: limit, interval: interval)
      bucket.take(1)

      self.rateLimits[route] = bucket
    }else {
      if self.rateLimits[route]!.tokens != remaining {
        self.rateLimits[route]!.tokens = remaining
{'fraction_non_alphanumeric': 0.1250837614473978, 'fraction_numerical': 0.008934554389099843, 'mean_word_length': 2.9982142857142855, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 11, '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': '4131987', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1305, 'n_words': 414}