diff --git a/ajax/scan_article.php b/ajax/scan_article.php index 58532dd..5cde426 100644 --- a/ajax/scan_article.php +++ b/ajax/scan_article.php @@ -16,12 +16,12 @@ $fp = fopen($file, 'r'); $result = json_decode(fread($fp, filesize($file))); fclose($fp); -$price = 'N/A'; +$returnVal = 'N/A'; // Get the price from the given code and remove 1 in quantity. Do not get the price if quantity is at 0 foreach ($result->articles as $articleObject) { if ($articleObject->code === $_POST["code"] && intval($articleObject->quantity) > 0) { - $price = $articleObject->price; + $returnVal = $articleObject->name . "\n". $articleObject->price . "€"; $articleObject->quantity = strval(intval($articleObject->quantity) - 1); } } @@ -31,5 +31,5 @@ $fp = fopen('../data/stock.json', 'w'); fwrite($fp, json_encode($result)); fclose($fp); -echo $price; +echo $returnVal; diff --git a/zapette.py b/zapette.py new file mode 100644 index 0000000..05c1416 --- /dev/null +++ b/zapette.py @@ -0,0 +1,37 @@ +# run pip3 install requests +import requests +import json + +API_ENDPOINT = "http://localhost/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()