From ccfe72c34726912cc602877e93171c41a1a7ff7e Mon Sep 17 00:00:00 2001 From: lawdfrogo Date: Tue, 27 May 2025 11:11:54 +0700 Subject: [PATCH] add new fitur api --- bruno.json | 9 +++++++++ config/server.py | 2 +- handler.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- web.bru | 17 +++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 bruno.json create mode 100644 web.bru diff --git a/bruno.json b/bruno.json new file mode 100644 index 0000000..62c9092 --- /dev/null +++ b/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "costapy", + "type": "collection", + "ignore": [ + "node_modules", + ".git" + ] +} \ No newline at end of file diff --git a/config/server.py b/config/server.py index 8c191b8..dbbd507 100644 --- a/config/server.py +++ b/config/server.py @@ -4,7 +4,7 @@ host = "localhost" port = 11000 reloader = False debug = False -server = 'gunicorn' # default = 'wsgiref' +server = 'wsgiref' # default = 'wsgiref' session_opts = { 'session.type': 'file', diff --git a/handler.py b/handler.py index 64ae435..7d56635 100644 --- a/handler.py +++ b/handler.py @@ -5,9 +5,11 @@ # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. -from bottle import Bottle, route +from bottle import Bottle, route, response, request from config import directory +import json + import templates.plain.main as template_public import modules.public.home as public_home @@ -21,3 +23,45 @@ def index(): } } return public_home.main().html(params) + +@app.route('/api/', methods=['OPTIONS', 'GET', 'POST', 'PUT', 'DELETE']) +def list(crud): + #Menentukan tipe konten untuk response + response.content_type = 'application/json' + #Mengambil data dari request body + params = request.json + #Keluaran bawaan + keluaran = { + "status": "berhasil", + "message": "Anda Tidak dapat mengakses endpoint ini.", + "endpoint": f"/api/{crud}", + } + + if crud == "create": + keluaran["status"] = "berhasil" + keluaran["message"] = "Dummy: selamat data anda telah berhasil disimpan." + keluaran["data"] = { + "food":params["food"] + } + + elif crud == "read": + keluaran["status"] = "berhasil" + keluaran["message"] = "Dummy: data berhasil diambil." + keluaran["data"] = [ + "banana", + "apple", + "marshmallow" + ] + + elif crud == "update": + keluaran["status"] = "berhasil" + keluaran["message"] = "Dummy: belum disediakan, silakan hubungi developer untuk menambahkan fitur ini." + + elif crud == "delete": + keluaran["status"] = "berhasil" + keluaran["message"] = "Dummy: belum disediakan, silakan hubungi developer untuk menambahkan fitur ini." + + else: + pass + + return json.dumps(keluaran, indent=2).encode() \ No newline at end of file diff --git a/web.bru b/web.bru new file mode 100644 index 0000000..17e9ab6 --- /dev/null +++ b/web.bru @@ -0,0 +1,17 @@ +meta { + name: web + type: http + seq: 10 +} + +get { + url: http://localhost:11000/api/create + body: json + auth: inherit +} + +body:json { + { + "food":"Pasta" + } +}