<commit_before>/* @flow */

import https from 'https'
import Debug from 'debug'

const debug = Debug('crypto-exchange-api:api')

export class API {
  /**
   * Store and check per-second rate limits
   *
   * @private
   */
  _checkRateLimit (ts: number, limit: number, rates: number[]) {
    rates.push(ts)
    let edge = rates.find((d) => ts - 1000 < d)
    if (edge) rates.splice(0, rates.indexOf(edge))
    return rates.length <= limit
  }

  /**
   * Parse https.request responses
   *
   * @private
   */
  _resJsonParse (response: { statusCode: number, data: string }) {
    let resObj
    try {
      resObj = JSON.parse(response.data)
      debug('Successful response %o', response)
    } catch (e) {
      debug('Response error %o', response)
      throw new Error(`HTTP ${response.statusCode} Returned error: ${response.data}`)
    }
    if (resObj.error) {
      debug('Response error %o', response)
      throw new Error(`HTTP ${response.statusCode} Returned error: ${resObj.error}`)
    }
    return resObj
  }

  /**
   * Execute https.request(s)
   *
   * @private
   */
  _httpsRequest (options: {}, body?: string) {
    return new Promise((resolve, reject) => {
      debug(`sending https request with body: %o and options:\n%O`, body, options)
      let req : https.ClientRequest = https.request(options, (res: https.IncomingMessage) => {
        let rawData: string = ''
        res.on('data', (chunk) => { rawData += chunk })
        res.on('end', () => {
          return resolve({ statusCode: res.statusCode, data: rawData })
        })
      })
      req.on('error', reject)
      if (body) {
        req.write(body)
      }
      req.end()
    })
  }
}<commit_msg>add missing flow annotation to api._get<commit_after>/* @flow */

import https from 'https'
import Debug from 'debug'

const debug = Debug('crypto-exchange-api:api')

export class API {
  /**
   * Store and check per-second rate limits
   *
   * @private
   */
  _checkRateLimit (ts: number, limit: number, rates: number[]) {
    rates.push(ts)
    let edge = rates.find((d) => ts - 1000 < d)
    if (edge) rates.splice(0, rates.indexOf(edge))
    return rates.length <= limit
  }

  /**
   * Parse https.request responses
   *
   * @private
   */
  _resJsonParse (response: { statusCode: number, data: string }) {
    let resObj
    try {
      resObj = JSON.parse(response.data)
      debug('Successful response %o', response)
    } catch (e) {
      debug('Response error %o', response)
      throw new Error(`HTTP ${response.statusCode} Returned error: ${response.data}`)
    }
    if (resObj.error) {
      debug('Response error %o', response)
      throw new Error(`HTTP ${response.statusCode} Returned error: ${resObj.error}`)
    }
    return resObj
  }

  /**
   * Execute https.request(s)
   *
   * @private
   */
  _httpsRequest (options: {}, body?: string): Promise<*> {
    return new Promise((resolve, reject) => {
      debug(`sending https request with body: %o and options:\n%O`, body, options)
      let req : https.ClientRequest = https.request(options, (res: https.IncomingMessage) => {
        let rawData: string = ''
        res.on('data', (chunk) => { rawData += chunk })
        res.on('end', () => {
          return resolve({ statusCode: res.statusCode, data: rawData })
        })
      })
      req.on('error', reject)
      if (body) {
        req.write(body)
      }
      req.end()
    })
  }
}
{'fraction_non_alphanumeric': 0.15319273579379028, 'fraction_numerical': 0.0029291154071470417, 'mean_word_length': 2.579664570230608, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, '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': '3245207', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1073, 'n_words': 282}