<commit_before>import json
import logging
import ssl
import time

import requests
import websocket
import websocket._exceptions

logger = logging.getLogger(__name__)


class MattermostAPI(object):
    def __init__(self, url, ssl_verify):
        self.url = url
        self.token = ""
        self.initial = None
        self.default_team_id = None # the first team in API returned value
        self.teams_channels_ids = None  # struct: {team_id:[channel_id,...], ...}
        self.ssl_verify = ssl_verify
        if not ssl_verify:
            requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)

    def _get_headers(self):
        return {"Authorization": "Bearer " + self.token}

    def get(self, request):
        return json.loads(
            requests.get(
                self.url + request,
                headers=self._get_headers(),
                verify=self.ssl_verify
            ).text)

    def post(self, request, data=None):
        return json.loads(requests.post(
            self.url + request,
            headers=self._get_headers(),
            data=json.dumps(data),
            verify=self.ssl_verify
        ).text)

    def login(self, team, account, password):
        props = {'login_id': account, 'password': password}
        response =requests.post(
            self.url + '/users/login',
            data = json.dumps(props),
            verify=self.ssl_verify)
        if response.status_code == 200:
            self.token = response.headers["Token"]
            self.load_initial_data()
            self.user = json.loads(response.text)
            return self.user
        else:
            response.raise_for_status()

    def load_initial_data(self):
        self.teams = self.get('/users/me/teams')
        self.default_team_id = self.teams[0]['id']
        self.teams_channels_ids = {}
        for team in self.teams:
            self.teams_channels_ids[team['id']] = []
            # get all channels belonging to each team
            for channel in self.get_channels(team['id']):
                self.teams_channels_ids[team['id']].append(channel['id'])

    def create_post(self, user_id, channel_id, message, files=None, pid=""):<commit_msg>deal with redirected login request<commit_after>import json
import logging
import ssl
import time

import requests
import websocket
import websocket._exceptions

logger = logging.getLogger(__name__)


class MattermostAPI(object):
    def __init__(self, url, ssl_verify):
        self.url = url
        self.token = ""
        self.initial = None
        self.default_team_id = None # the first team in API returned value
        self.teams_channels_ids = None  # struct: {team_id:[channel_id,...], ...}
        self.ssl_verify = ssl_verify
        if not ssl_verify:
            requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)

    def _get_headers(self):
        return {"Authorization": "Bearer " + self.token}

    def get(self, request):
        return json.loads(
            requests.get(
                self.url + request,
                headers=self._get_headers(),
                verify=self.ssl_verify
            ).text)

    def post(self, request, data=None):
        return json.loads(requests.post(
            self.url + request,
            headers=self._get_headers(),
            data=json.dumps(data),
            verify=self.ssl_verify
        ).text)

    def login(self, team, account, password):
        props = {'login_id': account, 'password': password}
        response = requests.post(
            self.url + '/users/login',
            data = json.dumps(props),
            verify=self.ssl_verify,
            allow_redirects=False)
        if response.status_code in [301, 302, 307]:
            # reset self.url to the new URL
            self.url = response.headers['Location'].replace('/users/login', '')
            # re-try login if redirected
            response = requests.post(
                self.url + '/users/login',
                data = json.dumps(props),
                verify=self.ssl_verify,
                allow_redirects=False)
        if response.status_code == 200:
            self.token = response.headers["Token"]
            self.load_initial_data()
            self.user = json.loads(response.text)
            return self.user
        else:
            response.raise_for_status()

    def load_initial_data(self):
        self.teams = self.get('/users/me/teams')
        self.default_team_id = self.teams[0]['id']
        self.teams_channels_ids = {}
        for team in self.teams:
            self.teams_channels_ids[team['id']] = []
            # get all channels belonging to each team
            for channel in self.get_channels(team['id']):
                self.teams_channels_ids[team['id']].append(channel['id'])

    def create_post(self, user_id, channel_id, message, files=None, pid=""):
{'fraction_non_alphanumeric': 0.11019339242546333, 'fraction_numerical': 0.004230459307010476, 'mean_word_length': 2.1463878326996197, 'pattern_counts': {'":': 2, '<': 3, '<?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': '215419', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1399, 'n_words': 300}