<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

try:
    import base64
    import hashlib
    import hmac
    import json
    import requests
    import sys
    import time
    import urllib
    import urllib2
    from datetime import datetime, timedelta
    from urllib2 import HTTPError, URLError

except ImportError, e:
    print "Import error in %s : %s" % (__name__, e)
    import sys
    sys.exit()


def logger_debug(logger, message):
    if logger is not None:
        logger.debug(message)


def login(url, username, password):
    """
    Login and obtain a session to be used for subsequent API calls
    Wrong username/password leads to HTTP error code 531
    """
    args = {}

    args["command"] = 'login'
    args["username"] = username
    args["password"] = password
    args["domain"] = "/"
    args["response"] = "json"

    sessionkey = ''
    session = requests.Session()

    resp = session.post(url, params=args)
    if resp.status_code == 200:
        sessionkey = resp.json()['loginresponse']['sessionkey']
    elif resp.status_code == 531:
        print "Error authenticating at %s, with username: %s" \
              ", and password: %s" % (url, username, password)
        session = None
        sessionkey = None
    else:
        resp.raise_for_status()

    return session, sessionkey


def logout(url, session):
    if session is None:
        return
    session.get(url, params={'command': 'logout'})


def make_request_with_password(command, args, logger, url, credentials):

    error = None
    username = credentials['username']
    password = credentials['password']

    if not (username and password):
        error = "Username and password cannot be empty"
        result = None
        return result, error

    tries = 0
    retry = True

    while tries < 2 and retry:
        sessionkey = credentials.get('sessionkey')
        session = credentials.get('session')
        tries += 1

        # obtain a valid session if not supplied
        if not (session and sessionkey):
            session, sessionkey = login(url, username, password)
            if not (session and sessionkey):
                return None, 'Error authenticating'
            credentials['session'] = session
            credentials['sessionkey'] = sessionkey

        args['sessionkey'] = sessionkey

        # make the api call
        resp = session.get(url, params=args)
        result = resp.text
        logger_debug(logger, "Response received: %s" % resp.text)

        if resp.status_code == 200:  # success
            retry = False
            break
        if resp.status_code == 401:  # sessionkey is wrong
            credentials['session'] = None
            credentials['sessionkey'] = None
            continue

        if resp.status_code != 200 and resp.status_code != 401:
            error = "%s: %s" %\
                    (str(resp.status_code), resp.headers.get('X-Description'))
            result = None
            retry = False

    return result, error


def make_request(command, args, logger, host, port,
                 credentials, protocol, path, expires):
    response = None
    error = None

    if protocol != 'http' and protocol != 'https':
        error = "Protocol must be 'http' or 'https'"
        return None, error

    if args is None:
        args = {}

    args["command"] = command
    args["response"] = "json"
    args["signatureversion"] = "3"
    expirationtime = datetime.utcnow() + timedelta(seconds=int(expires))
    args["expires"] = expirationtime.strftime('%Y-%m-%dT%H:%M:%S+0000')

    # try to use the apikey/secretkey method by default
    # if not present, use the username/password method
    if not credentials['apikey']:
        url = "%s://%s:%s%s" % (protocol, host, port, path)
        return make_request_with_password(command, args,
                                          logger, url, credentials)

    args['apikey'] = credentials['apikey']
    secretkey = credentials['secretkey']
    request = zip(args.keys(), args.values())
    request.sort(key=lambda x: x[0].lower())

    request_url = "&".join(["=".join([r[0], urllib.quote_plus(str(r[1]))])
                           for r in request])
    hashStr = "&".join(["=".join([r[0].lower(),
                       str.lower(urllib.quote_plus(str(r[1]))).replace("+",
                       "%20")]) for r in request])
<commit_msg>requester: Fix typo from 61901f20 'apiKey' should be sent<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

try:
    import base64
    import hashlib
    import hmac
    import json
    import requests
    import sys
    import time
    import urllib
    import urllib2
    from datetime import datetime, timedelta
    from urllib2 import HTTPError, URLError

except ImportError, e:
    print "Import error in %s : %s" % (__name__, e)
    import sys
    sys.exit()


def logger_debug(logger, message):
    if logger is not None:
        logger.debug(message)


def login(url, username, password):
    """
    Login and obtain a session to be used for subsequent API calls
    Wrong username/password leads to HTTP error code 531
    """
    args = {}

    args["command"] = 'login'
    args["username"] = username
    args["password"] = password
    args["domain"] = "/"
    args["response"] = "json"

    sessionkey = ''
    session = requests.Session()

    resp = session.post(url, params=args)
    if resp.status_code == 200:
        sessionkey = resp.json()['loginresponse']['sessionkey']
    elif resp.status_code == 531:
        print "Error authenticating at %s, with username: %s" \
              ", and password: %s" % (url, username, password)
        session = None
        sessionkey = None
    else:
        resp.raise_for_status()

    return session, sessionkey


def logout(url, session):
    if session is None:
        return
    session.get(url, params={'command': 'logout'})


def make_request_with_password(command, args, logger, url, credentials):

    error = None
    username = credentials['username']
    password = credentials['password']

    if not (username and password):
        error = "Username and password cannot be empty"
        result = None
        return result, error

    tries = 0
    retry = True

    while tries < 2 and retry:
        sessionkey = credentials.get('sessionkey')
        session = credentials.get('session')
        tries += 1

        # obtain a valid session if not supplied
        if not (session and sessionkey):
            session, sessionkey = login(url, username, password)
            if not (session and sessionkey):
                return None, 'Error authenticating'
            credentials['session'] = session
            credentials['sessionkey'] = sessionkey

        args['sessionkey'] = sessionkey

        # make the api call
        resp = session.get(url, params=args)
        result = resp.text
        logger_debug(logger, "Response received: %s" % resp.text)

        if resp.status_code == 200:  # success
            retry = False
            break
        if resp.status_code == 401:  # sessionkey is wrong
            credentials['session'] = None
            credentials['sessionkey'] = None
            continue

        if resp.status_code != 200 and resp.status_code != 401:
            error = "%s: %s" %\
                    (str(resp.status_code), resp.headers.get('X-Description'))
            result = None
            retry = False

    return result, error


def make_request(command, args, logger, host, port,
                 credentials, protocol, path, expires):
    response = None
    error = None

    if protocol != 'http' and protocol != 'https':
        error = "Protocol must be 'http' or 'https'"
        return None, error

    if args is None:
        args = {}

    args["command"] = command
    args["response"] = "json"
    args["signatureversion"] = "3"
    expirationtime = datetime.utcnow() + timedelta(seconds=int(expires))
    args["expires"] = expirationtime.strftime('%Y-%m-%dT%H:%M:%S+0000')

    # try to use the apikey/secretkey method by default
    # if not present, use the username/password method
    if not credentials['apikey']:
        url = "%s://%s:%s%s" % (protocol, host, port, path)
        return make_request_with_password(command, args,
                                          logger, url, credentials)

    args['apiKey'] = credentials['apikey']
    secretkey = credentials['secretkey']
    request = zip(args.keys(), args.values())
    request.sort(key=lambda x: x[0].lower())

    request_url = "&".join(["=".join([r[0], urllib.quote_plus(str(r[1]))])
                           for r in request])
    hashStr = "&".join(["=".join([r[0].lower(),
                       str.lower(urllib.quote_plus(str(r[1]))).replace("+",
                       "%20")]) for r in request])

{'fraction_non_alphanumeric': 0.11062969381860197, 'fraction_numerical': 0.00933949547467745, 'mean_word_length': 2.5150592216582064, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4192898', 'n_tokens_mistral': 2935, 'n_tokens_neox': 2820, 'n_words': 962}