# run pip3 install requests
import requests
import json

API_ENDPOINT = "https://srv-falcon.etud.insa-toulouse.fr/~proximo/ajax/scan_article.php"

def get_password():
    with open('pass') as f:
        password = f.readline()
    return password.strip()


def search_product(code):
    # data to be sent to api
    data = {
        'password': get_password(),
        'code': str(code)
    }
    # sending post request and saving response as response object
    r = requests.post(url=API_ENDPOINT, data=json.dumps(data))
    return r.text


def main():
    code_input = input('Scannez le code\n')
    try:
        code = int(code_input)
        result = search_product(code)
        print(result)
    except requests.exceptions.MissingSchema:
        print("Format URL invalide !")
    except requests.exceptions.ConnectionError:
        print("URL invalide !")
    except ValueError:
        print("Code invalide !")

main()
