<commit_before>'''
Send events to CoScale API
==========================

This state is useful for creating and sending events to CoScale API during state runs.

.. code-block:: yaml

    coscale-event:
        coscale.event:
            - baseurl: https://api.coscale.com/
            - accesstoken: <PASSWORD>
            - appid: 00006e0e-5d0c-4633-abce-1e424f767a03
            - event_name: 'Software updates'
            - event_message: 'Updating to version 2.2'
            - event_timestamp: 0        #optional
'''

import json
import requests

def _login(accesstoken, url):
    '''
    Authentication using the Access token to get a HTTPAuthentication token from the CoScale API.

    The following parameters are required:
    accessToken
        The accessToken is used to login to the API
    url
        The url we need to access in order to get the token

    Return the HTTPAuthentication token.
    '''
    data = {'accessToken': accesstoken}
    req = requests.post(url, data=data, timeout=1)
    if req.status_code != 200:
        raise AttributeError(req.text)
    response = json.loads(req.text)
    return response['token']

def _eventpush(name, token, url):
    '''
    Create and event using the event name.

    The following parameters are required:
    name
        The event name
    token
        HTTPAuthentication token used for authentication
    url
        We create a POST request to this url to create an event

    Return request if there was an error or the event id if the request succeed
    '''
    data = {'name':			name,
            'description':	'',
            'type':			'',
            'source':		'SaltStack'}
    headers = {'HTTPAuthorization': token}
    req = requests.post(url, data=data, headers=headers, timeout=1)
    if req.status_code == 409 or req.status_code == 200:
        response = json.loads(req.text)
        return (None, response["id"])
    return (req, None)
<commit_msg>Added icon to event creation.<commit_after>'''
Send events to CoScale API
==========================

This state is useful for creating and sending events to CoScale API during state runs.

.. code-block:: yaml

    coscale-event:
        coscale.event:
            - baseurl: https://api.coscale.com/
            - accesstoken: <PASSWORD>
            - appid: 00006e0e-5d0c-4633-abce-1e424f767a03
            - event_name: 'Software updates'
            - event_message: 'Updating to version 2.2'
            - event_timestamp: 0        #optional
'''

import json
import requests

def _login(accesstoken, url):
    '''
    Authentication using the Access token to get a HTTPAuthentication token from the CoScale API.

    The following parameters are required:
    accessToken
        The accessToken is used to login to the API
    url
        The url we need to access in order to get the token

    Return the HTTPAuthentication token.
    '''
    data = {'accessToken': accesstoken}
    req = requests.post(url, data=data, timeout=1)
    if req.status_code != 200:
        raise AttributeError(req.text)
    response = json.loads(req.text)
    return response['token']

def _eventpush(name, token, url):
    '''
    Create an event using the event name.

    The following parameters are required:
    name
        The event name
    token
        HTTPAuthentication token used for authentication
    url
        We create a POST request to this url to create an event

    Return request if there was an error or the event id if the request succeed
    '''
    data = {'name':			name,
            'description':	'',
            'type':			'',
            'source':		'SaltStack',
            'icon':         'salt'}
    headers = {'HTTPAuthorization': token}
    req = requests.post(url, data=data, headers=headers, timeout=1)
    if req.status_code == 409 or req.status_code == 200:
        response = json.loads(req.text)
        return (None, response["id"])
    return (req, None)

{'fraction_non_alphanumeric': 0.10007639419404125, 'fraction_numerical': 0.017825311942959002, 'mean_word_length': 2.421602787456446, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4276794', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1109, 'n_words': 394}