<commit_before># This file is part of the Indico plugins.
# Copyright (C) 2002 - 2019 CERN
#
# The Indico plugins are free software; you can redistribute
# them and/or modify them under the terms of the MIT License;
# see the LICENSE file for more details.

from __future__ import unicode_literals

import posixpath

import requests
from werkzeug.exceptions import ServiceUnavailable


def _get_settings():
    from indico_ursh.plugin import UrshPlugin
    api_key = UrshPlugin.settings.get('api_key')
    api_host = UrshPlugin.settings.get('api_host')

    if not api_key or not api_host:
        raise ServiceUnavailable('Not configured')

    return api_key, api_host


def request_short_url(original_url):
    api_key, api_host = _get_settings()
    headers = {'Authorization': 'Bearer {api_key}'.format(api_key=api_key)}
    url = posixpath.join(api_host, 'urls/')

    response = requests.post(url, data={'url': original_url, 'allow_reuse': True}, headers=headers)
    if response.status_code not in (400,):
        response.raise_for_status()

    data = response.json()
    return data['short_url']


def register_shortcut(original_url, shortcut):
    api_key, api_host = _get_settings()
    headers = {'Authorization': 'Bearer {api_key}'.format(api_key=api_key)}
    url = posixpath.join(api_host, 'urls', shortcut)

    response = requests.put(url, data={'url': original_url, 'allow_reuse': True}, headers=headers)
    if not (400 <= response.status_code < 500):
        response.raise_for_status()

    return response.json()


def strip_end(text, suffix):
    if not text.endswith(suffix):
        return text
    return text[:len(text) - len(suffix)]<commit_msg>URSH: Always include /api/ for API calls<commit_after># This file is part of the Indico plugins.
# Copyright (C) 2002 - 2019 CERN
#
# The Indico plugins are free software; you can redistribute
# them and/or modify them under the terms of the MIT License;
# see the LICENSE file for more details.

from __future__ import unicode_literals

import posixpath

import requests
from werkzeug.exceptions import ServiceUnavailable


def _get_settings():
    from indico_ursh.plugin import UrshPlugin
    api_key = UrshPlugin.settings.get('api_key')
    api_host = UrshPlugin.settings.get('api_host')

    if not api_key or not api_host:
        raise ServiceUnavailable('Not configured')

    return api_key, api_host


def request_short_url(original_url):
    api_key, api_host = _get_settings()
    headers = {'Authorization': 'Bearer {api_key}'.format(api_key=api_key)}
    url = posixpath.join(api_host, 'api/urls/')

    response = requests.post(url, data={'url': original_url, 'allow_reuse': True}, headers=headers)
    response.raise_for_status()
    data = response.json()
    return data['short_url']


def register_shortcut(original_url, shortcut):
    api_key, api_host = _get_settings()
    headers = {'Authorization': 'Bearer {api_key}'.format(api_key=api_key)}
    url = posixpath.join(api_host, 'api/urls', shortcut)

    response = requests.put(url, data={'url': original_url, 'allow_reuse': True}, headers=headers)
    if not (400 <= response.status_code < 500):
        response.raise_for_status()

    return response.json()


def strip_end(text, suffix):
    if not text.endswith(suffix):
        return text
    return text[:len(text) - len(suffix)]
{'fraction_non_alphanumeric': 0.10788133053640996, 'fraction_numerical': 0.0092897812406353, 'mean_word_length': 4.298412698412698, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '4978065', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1042, 'n_words': 288}