<commit_before>/* global __DEVELOPMENT__ */
/* global __CLIENT__ */
import request from 'superagent';
import path from 'path';
import resourceVersions from 'constants/riot/resourceVersions';
import regions from 'constants/riot/regions';

const API_KEY = process.env.RIOT_GAMES_API_KEY;
if (!API_KEY && !__CLIENT__) {
  console.error('process.env.RIOT_GAMES_API_KEY must be set');
}

function validateRegion(region) {
  if (!regions[region.toUpperCase()]) {
    throw `Unknown Riot region: ${region}`;
  }
}

function validateResource(resource) {
  if (!resourceVersions[resource.toLowerCase()]) {
    throw `Unknown Riot resource: ${resource}`;
  }
}

function baseUrl(region) {
  return `${region}.api.pvp.net/api/lol/`;
}

function apiUrl(region, resourcePath) {
  const base = `${baseUrl(region)}/${region}`;
  const [resource] = resourcePath.split('/');
  const resourceVersion = resourceVersions[resource];

  validateRegion(region);
  validateResource(resource);

  return 'https://' + path.join(base, resourceVersion, resourcePath);
}

function staticDataApiUrl(region, resourcePath) {
  const base = `${baseUrl(region)}/static-data/${region}`;
  const staticDataVersion = resourceVersions['lol-static-data'];

  validateRegion(region);

  return 'https://' + path.join(base, staticDataVersion, resourcePath);
}

function get(url, { extract, query = {} } = {}) {
  return new Promise((resolve, reject) => {
    request
      .get(url)
      .query({api_key: API_KEY})
      .query(query)
      .end((err, res) => {
        if (__DEVELOPMENT__) {
          console.info(`Riot API GET: (${res.status}) ${url}`);
        }<commit_msg>Fix `staticDataApiUrl` for non-NA regions<commit_after>/* global __DEVELOPMENT__ */
/* global __CLIENT__ */
import request from 'superagent';
import path from 'path';
import resourceVersions from 'constants/riot/resourceVersions';
import regions from 'constants/riot/regions';

const API_KEY = process.env.RIOT_GAMES_API_KEY;
if (!API_KEY && !__CLIENT__) {
  console.error('process.env.RIOT_GAMES_API_KEY must be set');
}

function validateRegion(region) {
  if (!regions[region.toUpperCase()]) {
    throw `Unknown Riot region: ${region}`;
  }
}

function validateResource(resource) {
  if (!resourceVersions[resource.toLowerCase()]) {
    throw `Unknown Riot resource: ${resource}`;
  }
}

function baseUrl(region) {
  return `${region}.api.pvp.net/api/lol/`;
}

function apiUrl(region, resourcePath) {
  const base = `${baseUrl(region)}/${region}`;
  const [resource] = resourcePath.split('/');
  const resourceVersion = resourceVersions[resource];

  validateRegion(region);
  validateResource(resource);

  return 'https://' + path.join(base, resourceVersion, resourcePath);
}

function staticDataApiUrl(region, resourcePath) {
  const base = `global.api.pvp.net/api/lol/static-data/${region}`;
  const staticDataVersion = resourceVersions['lol-static-data'];

  validateRegion(region);

  return 'https://' + path.join(base, staticDataVersion, resourcePath);
}

function get(url, { extract, query = {} } = {}) {
  return new Promise((resolve, reject) => {
    request
      .get(url)
      .query({api_key: API_KEY})
      .query(query)
      .end((err, res) => {
        if (__DEVELOPMENT__) {
          console.info(`Riot API GET: (${res.status}) ${url} (query: ${JSON.stringify(query)})`);
        }
{'fraction_non_alphanumeric': 0.15780035863717873, 'fraction_numerical': 0.0, 'mean_word_length': 4.504934210526316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, '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': '3997338', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1079, 'n_words': 224}