<commit_before>import demistomock as demisto
from CommonServerPython import *
from CommonServerUserPython import *

''' IMPORTS '''

import json
import time
import requests
import dateparser
from datetime import datetime, timezone
import secrets
import string
import hashlib
from typing import Any, Dict

# Disable insecure warnings
requests.packages.urllib3.disable_warnings()

TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
NONCE_LENGTH = 64
API_KEY_LENGTH = 128

INTEGRATION_CONTEXT_BRAND = 'PaloAltoNetworksXDR'


def convert_epoch_to_milli(ts):
    if ts is None:
        return None
    if 9 < len(str(ts)) < 13:
        ts = int(ts) * 1000
    return int(ts)


def convert_datetime_to_epoch(the_time=0):
    if the_time is None:
        return None
    else:
        try:
            if isinstance(the_time, datetime):
                return int(the_time.strftime('%s'))
        except Exception as e:
            print(e)
            return 0


def convert_datetime_to_epoch_millis(the_time=0):
    return convert_epoch_to_milli(convert_datetime_to_epoch(the_time=the_time))


def generate_current_epoch_utc():
    return convert_datetime_to_epoch_millis(datetime.now(timezone.utc))


def generate_key():
    return "".join([secrets.choice(string.ascii_letters + string.digits) for _ in range(API_KEY_LENGTH)])


def create_auth(api_key):
    nonce = "".join([secrets.choice(string.ascii_letters + string.digits) for _ in range(NONCE_LENGTH)])
    timestamp = str(generate_current_epoch_utc())  # Get epoch time utc millis
    m = hashlib.sha256()
    m.update((api_key + nonce + timestamp).encode("utf-8"))
    return nonce, timestamp, m.hexdigest()


def clear_trailing_whitespace(res):
    index = 0
    while index < len(res):
        for key, value in res[index].items():
            if isinstance(value, str):
                key = value.rstrip()
        index += 1
    return res


class Client(BaseClient):
    def test_module(self, first_fetch_time):
        """
            Performs basic get request to get item samples
        """
        last_one_day, _ = parse_date_range(first_fetch_time, TIME_FORMAT)
        self.get_incidents(lte_creation_time=last_one_day, limit=1)

    def get_incidents(self, incident_id_list=None, lte_modification_time=None, gte_modification_time=None,
                      lte_creation_time=None, gte_creation_time=None, sort_by_modification_time=None,
                      sort_by_creation_time=None, page_number=0, limit=100, gte_creation_time_milliseconds=0):
        """
        Filters and returns incidents

        :param incident_id_list: List of incident ids - must be list
        :param lte_modification_time: string of time format "2019-12-31T23:59:00"<commit_msg>Improve function<commit_after>import demistomock as demisto
from CommonServerPython import *
from CommonServerUserPython import *

''' IMPORTS '''

import json
import time
import requests
import dateparser
from datetime import datetime, timezone
import secrets
import string
import hashlib
from typing import Any, Dict

# Disable insecure warnings
requests.packages.urllib3.disable_warnings()

TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
NONCE_LENGTH = 64
API_KEY_LENGTH = 128

INTEGRATION_CONTEXT_BRAND = 'PaloAltoNetworksXDR'


def convert_epoch_to_milli(ts):
    if ts is None:
        return None
    if 9 < len(str(ts)) < 13:
        ts = int(ts) * 1000
    return int(ts)


def convert_datetime_to_epoch(the_time=0):
    if the_time is None:
        return None
    else:
        try:
            if isinstance(the_time, datetime):
                return int(the_time.strftime('%s'))
        except Exception as e:
            print(e)
            return 0


def convert_datetime_to_epoch_millis(the_time=0):
    return convert_epoch_to_milli(convert_datetime_to_epoch(the_time=the_time))


def generate_current_epoch_utc():
    return convert_datetime_to_epoch_millis(datetime.now(timezone.utc))


def generate_key():
    return "".join([secrets.choice(string.ascii_letters + string.digits) for _ in range(API_KEY_LENGTH)])


def create_auth(api_key):
    nonce = "".join([secrets.choice(string.ascii_letters + string.digits) for _ in range(NONCE_LENGTH)])
    timestamp = str(generate_current_epoch_utc())  # Get epoch time utc millis
    m = hashlib.sha256()
    m.update((api_key + nonce + timestamp).encode("utf-8"))
    return nonce, timestamp, m.hexdigest()


def clear_trailing_whitespace(res):
    index = 0
    while index < len(res):
        for key, value in res[index].items():
            if isinstance(value, str):
                res[index][key] = value.rstrip()
        index += 1
    return res


class Client(BaseClient):
    def test_module(self, first_fetch_time):
        """
            Performs basic get request to get item samples
        """
        last_one_day, _ = parse_date_range(first_fetch_time, TIME_FORMAT)
        self.get_incidents(lte_creation_time=last_one_day, limit=1)

    def get_incidents(self, incident_id_list=None, lte_modification_time=None, gte_modification_time=None,
                      lte_creation_time=None, gte_creation_time=None, sort_by_modification_time=None,
                      sort_by_creation_time=None, page_number=0, limit=100, gte_creation_time_milliseconds=0):
        """
        Filters and returns incidents

        :param incident_id_list: List of incident ids - must be list
        :param lte_modification_time: string of time format "2019-12-31T23:59:00"
{'fraction_non_alphanumeric': 0.09075877273562373, 'fraction_numerical': 0.015432665809296343, 'mean_word_length': 3.4696223316912973, 'pattern_counts': {'":': 0, '<': 9, '<?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': '7393577', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1735, 'n_words': 394}