<commit_before>import json
import requests
import sys

try:
    import configparser
except ImportError:
    import ConfigParser as configparser
import re
import errno
import os

from pnc_cli import swagger_client
from pnc_cli.swagger_client.rest import ApiException

__author__ = 'thauser'


def get_keycloak_token(config):
    username = config.get('PNC', 'username')
    password = config.get('PNC', 'password')
    params = {'grant_type': 'password',
              'client_id': 'pncdirect',
              'username': username,
              'password': password}
    r = requests.post('https://keycloak3-pncauth.rhcloud.com/auth/realms/pncredhat/tokens/grants/access', params)
    if r.status_code == 200:
        reply = json.loads(r.content.decode('utf-8'))
        return reply.get('access_token')


config = configparser.ConfigParser()
configfilename = os.path.expanduser("~") + "/.config/pnc-cli/pnc-cli.conf"
configdirname = os.path.dirname(configfilename)

try:
    os.makedirs(configdirname)
except OSError as e:
    if e.errno != errno.EEXIST:
        raise
found = config.read(os.path.join(configfilename))
if not found:
    config.add_section('PNC')
    config.set('PNC', 'restEndpoint', 'http://ncl-test-vm-01.host.prod.eng.bos.redhat.com:8180/pnc-rest/rest/')
    # prompt user for his keycloak username / passwords
    username = input('Keycloak username: ')
    password = input('Keycloak password: ')
    config.set('PNC', 'username', username)
    config.set('PNC', 'password', password)
    with open(os.path.join(configfilename), 'w') as configfile:
        config.write(configfile)
pnc_rest_url = config.get('PNC', 'restEndpoint').rstrip('/')
keycloaktoken = get_keycloak_token(config)
if keycloaktoken:
    apiclient = swagger_client.ApiClient(pnc_rest_url, header_name='Authorization',
                                         header_value="Bearer " + keycloaktoken)
else:<commit_msg>allow configuring keycloak server through config; change default rest endpoint<commit_after>import json
import requests
import sys

try:
    import configparser
except ImportError:
    import ConfigParser as configparser
import re
import errno
import os

from pnc_cli import swagger_client
from pnc_cli.swagger_client.rest import ApiException

__author__ = 'thauser'


def get_keycloak_token(config):
    server = config.get('PNC', 'keycloakServer')
    username = config.get('PNC', 'username')
    password = config.get('PNC', 'password')
    params = {'grant_type': 'password',
              'client_id': 'pncdirect',
              'username': username,
              'password': password}
    r = requests.post(server, params)
    if r.status_code == 200:
        reply = json.loads(r.content.decode('utf-8'))
        return reply.get('access_token')


config = configparser.ConfigParser()
configfilename = os.path.expanduser("~") + "/.config/pnc-cli/pnc-cli.conf"
configdirname = os.path.dirname(configfilename)

try:
    os.makedirs(configdirname)
except OSError as e:
    if e.errno != errno.EEXIST:
        raise
found = config.read(os.path.join(configfilename))
if not found:
    config.add_section('PNC')
    config.set('PNC', 'restEndpoint', 'http://localhost:8080/pnc-rest/rest/')
    # prompt user for his keycloak username / passwords
    username = input('Keycloak username: ')
    password = input('Keycloak password: ')
    config.set('PNC', 'username', username)
    config.set('PNC', 'password', password)
    with open(os.path.join(configfilename), 'w') as configfile:
        config.write(configfile)
pnc_rest_url = config.get('PNC', 'restEndpoint').rstrip('/')
keycloaktoken = get_keycloak_token(config)
if keycloaktoken:
    apiclient = swagger_client.ApiClient(pnc_rest_url, header_name='Authorization',
                                         header_value="Bearer " + keycloaktoken)
else:
{'fraction_non_alphanumeric': 0.12689691261119831, 'fraction_numerical': 0.004971219256933543, 'mean_word_length': 3.7023370233702337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '867694', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1198, 'n_words': 255}