From 1f9786c1fb9a2abe2da42ccb00bf9826b5472a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=A0krab=C3=A1nek?= Date: Fri, 22 Mar 2024 12:27:24 +0100 Subject: [PATCH] add REST API py --- restAPIpy/.idea/.gitignore | 3 ++ .../inspectionProfiles/profiles_settings.xml | 6 +++ restAPIpy/.idea/misc.xml | 10 +++++ restAPIpy/.idea/modules.xml | 8 ++++ restAPIpy/.idea/restAPIpy.iml | 10 +++++ restAPIpy/client.py | 19 ++++++++ restAPIpy/client_v2.py | 43 +++++++++++++++++++ restAPIpy/server.py | 40 +++++++++++++++++ restAPIpy/templates/index.html | 17 ++++++++ 9 files changed, 156 insertions(+) create mode 100644 restAPIpy/.idea/.gitignore create mode 100644 restAPIpy/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 restAPIpy/.idea/misc.xml create mode 100644 restAPIpy/.idea/modules.xml create mode 100644 restAPIpy/.idea/restAPIpy.iml create mode 100644 restAPIpy/client.py create mode 100644 restAPIpy/client_v2.py create mode 100644 restAPIpy/server.py create mode 100644 restAPIpy/templates/index.html diff --git a/restAPIpy/.idea/.gitignore b/restAPIpy/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/restAPIpy/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/restAPIpy/.idea/inspectionProfiles/profiles_settings.xml b/restAPIpy/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/restAPIpy/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/restAPIpy/.idea/misc.xml b/restAPIpy/.idea/misc.xml new file mode 100644 index 0000000..92c8b99 --- /dev/null +++ b/restAPIpy/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/restAPIpy/.idea/modules.xml b/restAPIpy/.idea/modules.xml new file mode 100644 index 0000000..a051e2f --- /dev/null +++ b/restAPIpy/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/restAPIpy/.idea/restAPIpy.iml b/restAPIpy/.idea/restAPIpy.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/restAPIpy/.idea/restAPIpy.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/restAPIpy/client.py b/restAPIpy/client.py new file mode 100644 index 0000000..8fe178a --- /dev/null +++ b/restAPIpy/client.py @@ -0,0 +1,19 @@ +import requests + +url = "http://localhost:8000/api/data" + +response = requests.get(url) + +if response.status_code == 200: + data = response.json() + print(data) +else: + print("Chyba:", response.status_code) + +teplota = data["teplota"] +print(f"Teplota: {teplota}°C") +vlhkost = data["vlhkost"] +print(f"Vlhkost: {vlhkost}%") +led_stav = data["led_stav"] +print(f"Stav LED: {led_stav}") + diff --git a/restAPIpy/client_v2.py b/restAPIpy/client_v2.py new file mode 100644 index 0000000..abac32a --- /dev/null +++ b/restAPIpy/client_v2.py @@ -0,0 +1,43 @@ +import requests + +base_url = "http://localhost:8000/api" + +options = { + "1": "Zobrazit vše", + "2": "Zobrazit teplotu", + "3": "Zobrazit vlhkost" +} + +while True: + for key, value in options.items(): + print(f"{key}:{value}") + + vyber = input("Zadejte cislo volby:") + if vyber in options: + break + else: + print("Spatna volba") + +if vyber == "1": + url = f"{base_url}/data" +elif vyber == "2": + url = f"{base_url}/teplota" +elif vyber == "3": + url = f"{base_url}/vlhkost" + +response = requests.get(url) + +if response.status_code == 200: #uspěšné připojení + data = response.json() + if vyber == "1": + print(data) + if vyber == "2": + teplota = data["teplota"] + print(f"Teplota: {teplota}°C") + if vyber == "3": + vlhkost = data["vlhkost"] + print(f"Vlhkost: {vlhkost}%") +else: + print("Chyba:", response.status_code) + + diff --git a/restAPIpy/server.py b/restAPIpy/server.py new file mode 100644 index 0000000..cd6c2f9 --- /dev/null +++ b/restAPIpy/server.py @@ -0,0 +1,40 @@ +from flask import Flask, jsonify, render_template +import random + +#vytvoření Flask aplikace +app = Flask(__name__) + +def generuj_data(): + teplota = random.randint(20,28) + vlhkost = random.randint(40,80) + led_stav = bool(random.randint(0,1)) + return { + "nazev": "hodnota", + "teplota": teplota, + "vlhkost": vlhkost, + "led_stav": led_stav + } + +@app.route("/") +def index(): + #vytvoříme si složku templates a vložíme do ní index.html + return render_template("index.html") + +@app.route("/api/data",methods=["GET"]) +def get_data(): + data = generuj_data() + return jsonify(data) + +@app.route("/api/teplota",methods=["GET"]) +def get_teplota(): + data = generuj_data() + return jsonify({"teplota":data["teplota"]}) + +@app.route("/api/vlhkost",methods=["GET"]) +def get_vlhkost(): + data = generuj_data() + return jsonify({"vlhkost":data["vlhkost"]}) + +if __name__ == "__main__": + app.run(debug=True, port=8000) + diff --git a/restAPIpy/templates/index.html b/restAPIpy/templates/index.html new file mode 100644 index 0000000..acb4179 --- /dev/null +++ b/restAPIpy/templates/index.html @@ -0,0 +1,17 @@ + + + + + REST API PYTHON + + + +

Vítejte na REST API v pythonu

+ + + + \ No newline at end of file