<commit_before>/**
 * Copyright 2017 The AMP HTML Authors. 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 {Services} from './services';
import {assertHttpsUrl} from './url';
import {getValueForExpr} from './json';
import {user} from './log';

/**
 * @enum {number}
 */
export const UrlReplacementPolicy = {
  NONE: 0,
  OPT_IN: 1,
  ALL: 2,
};

/**
 * Batch fetches the JSON endpoint at the given element's `src` attribute.
 * Sets the fetch credentials option from the element's `credentials` attribute,
 * if it exists.
 *
 * @param {!./service/ampdoc-impl.AmpDoc} ampdoc
 * @param {!Element} element
 * @param {string=} opt_expr Dot-syntax reference to subdata of JSON result
 *     to return. If not specified, entire JSON result is returned.
 * @param {UrlReplacementPolicy=} opt_urlReplacement If ALL, replaces all URL
 *     vars. If OPT_IN, replaces whitelisted URL vars. Otherwise, don't expand..
 * @param {boolean=} opt_refresh Forces refresh of browser cache.
 * @param {string=} opt_token Auth token that forces a POST request.
 * @return {!Promise<!JsonObject|!Array<JsonObject>>} Resolved with JSON
 *     result or rejected if response is invalid.
 */
export function batchFetchJsonFor(
  ampdoc,
  element,
  opt_expr = '.',
  opt_urlReplacement = UrlReplacementPolicy.NONE,
  opt_refresh = false,
  opt_token = undefined
) {
  assertHttpsUrl(element.getAttribute('src'), element);
  const xhr = Services.batchedXhrFor(ampdoc.win);
  return requestForBatchFetch(element, opt_urlReplacement, opt_refresh)
    .then(data => {
      if (opt_token !== undefined) {
        data.fetchOpt['method'] = 'POST';
        data.fetchOpt['headers'] = {
          'Content-Type': 'application/x-www-form-urlencoded',
        };
        data.fetchOpt['body'] = {
          'ampViewerAuthToken': opt_token,
        };
      }
      return xhr.fetchJson(data.xhrUrl, data.fetchOpt);
    })
    .then(res => res.json())
    .then(data => {
      if (data == null) {
        throw new Error('Response is undefined.');
      }
      return getValueForExpr(data, opt_expr || '.');
    });
}
<commit_msg>Tag batched JSON fetches as user errors (#25497)<commit_after>/**
 * Copyright 2017 The AMP HTML Authors. 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 {Services} from './services';
import {assertHttpsUrl} from './url';
import {getValueForExpr} from './json';
import {user} from './log';

/**
 * @enum {number}
 */
export const UrlReplacementPolicy = {
  NONE: 0,
  OPT_IN: 1,
  ALL: 2,
};

/**
 * Batch fetches the JSON endpoint at the given element's `src` attribute.
 * Sets the fetch credentials option from the element's `credentials` attribute,
 * if it exists.
 *
 * @param {!./service/ampdoc-impl.AmpDoc} ampdoc
 * @param {!Element} element
 * @param {string=} opt_expr Dot-syntax reference to subdata of JSON result
 *     to return. If not specified, entire JSON result is returned.
 * @param {UrlReplacementPolicy=} opt_urlReplacement If ALL, replaces all URL
 *     vars. If OPT_IN, replaces whitelisted URL vars. Otherwise, don't expand..
 * @param {boolean=} opt_refresh Forces refresh of browser cache.
 * @param {string=} opt_token Auth token that forces a POST request.
 * @return {!Promise<!JsonObject|!Array<JsonObject>>} Resolved with JSON
 *     result or rejected if response is invalid.
 */
export function batchFetchJsonFor(
  ampdoc,
  element,
  opt_expr = '.',
  opt_urlReplacement = UrlReplacementPolicy.NONE,
  opt_refresh = false,
  opt_token = undefined
) {
  assertHttpsUrl(element.getAttribute('src'), element);
  const xhr = Services.batchedXhrFor(ampdoc.win);
  return requestForBatchFetch(element, opt_urlReplacement, opt_refresh)
    .then(data => {
      if (opt_token !== undefined) {
        data.fetchOpt['method'] = 'POST';
        data.fetchOpt['headers'] = {
          'Content-Type': 'application/x-www-form-urlencoded',
        };
        data.fetchOpt['body'] = {
          'ampViewerAuthToken': opt_token,
        };
      }
      return xhr.fetchJson(data.xhrUrl, data.fetchOpt);
    })
    .then(res => res.json())
    .then(data => {
      if (data == null) {
        throw new Error('Response is undefined.');
      }
      return getValueForExpr(data, opt_expr || '.');
    })
    .catch(err => {
      throw user().createError('failed fetching JSON data', err);
    });
}

{'fraction_non_alphanumeric': 0.11946085672082718, 'fraction_numerical': 0.004985228951255539, 'mean_word_length': 3.4184339314845023, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4637453', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1578, 'n_words': 538}