641 lines
11 KiB
Markdown
641 lines
11 KiB
Markdown
# API Playground
|
|
|
|
Proyek sederhana untuk playground API enrollment berbasis Python Bottle dan SQLite.
|
|
|
|
Aplikasi ini menyediakan halaman UI playground dan API endpoint untuk mengelola data enrollment secara CRUD.
|
|
|
|
---
|
|
|
|
## 🚀 Fitur
|
|
|
|
- Server API berbasis Bottle
|
|
- Database SQLite
|
|
- UI playground untuk testing endpoint langsung dari browser
|
|
- Autentikasi sederhana menggunakan Bearer Token
|
|
- Validasi nilai `type` dan `service`
|
|
|
|
---
|
|
|
|
## 📋 Requirements
|
|
|
|
Pastikan sudah terinstall:
|
|
|
|
- Python 3
|
|
- pip
|
|
- Git
|
|
|
|
> Catatan: Di Windows, umumnya gunakan `py` untuk menjalankan Python dan `py -m pip` untuk menjalankan pip, karena Python installer resmi Windows biasanya mendaftarkan Python melalui Python Launcher (`py`).
|
|
|
|
### Untuk Pengguna Windows
|
|
|
|
Sebelum menjalankan project, install tools berikut:
|
|
|
|
1. **Python 3**
|
|
- Download installer Python terbaru dari: https://www.python.org/downloads/windows/
|
|
- Saat instalasi, centang opsi **Add python.exe to PATH**
|
|
- Pastikan `pip` ikut terinstall bersama Python
|
|
|
|
2. **Git for Windows**
|
|
- Download dari: https://git-scm.com/download/win
|
|
- Gunakan opsi default saat instalasi
|
|
|
|
3. **Terminal**
|
|
- Bisa menggunakan **Command Prompt**, **PowerShell**, atau **Windows Terminal**
|
|
|
|
4. **Text Editor / IDE** *(opsional)*
|
|
- Visual Studio Code, PyCharm, atau editor lain sesuai preferensi
|
|
|
|
> Catatan: SQLite sudah tersedia sebagai library bawaan Python, jadi tidak perlu install SQLite secara terpisah.
|
|
|
|
---
|
|
|
|
## 🛠️ Instalasi
|
|
|
|
### 1. Clone / Masuk ke Folder Project
|
|
|
|
```bash
|
|
cd api-playground
|
|
```
|
|
|
|
### 2. Buat Virtual Environment
|
|
|
|
Disarankan menggunakan virtual environment agar dependency tidak bercampur dengan environment global.
|
|
|
|
Di Linux/macOS:
|
|
|
|
```bash
|
|
python3 -m venv venv
|
|
```
|
|
|
|
Di Windows:
|
|
|
|
```bash
|
|
py -m venv venv
|
|
```
|
|
|
|
### 3. Aktifkan Virtual Environment
|
|
|
|
Di Linux/macOS:
|
|
|
|
```bash
|
|
source venv/bin/activate
|
|
```
|
|
|
|
Di Windows:
|
|
|
|
```bash
|
|
venv\Scripts\activate
|
|
```
|
|
|
|
### 4. Install Dependencies
|
|
|
|
Di Linux/macOS:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
Di Windows:
|
|
|
|
```bash
|
|
py -m pip install -r requirements.txt
|
|
```
|
|
|
|
---
|
|
|
|
## ▶️ Menjalankan Aplikasi
|
|
|
|
Jalankan server dengan perintah:
|
|
|
|
Di Linux/macOS:
|
|
|
|
```bash
|
|
python app.py
|
|
```
|
|
|
|
Di Windows:
|
|
|
|
```bash
|
|
py app.py
|
|
```
|
|
|
|
Server akan berjalan di:
|
|
|
|
```bash
|
|
http://localhost:8080
|
|
```
|
|
|
|
atau:
|
|
|
|
```bash
|
|
http://0.0.0.0:8080
|
|
```
|
|
|
|
Jika port `8080` sudah terpakai, gunakan opsi `--port` untuk mengganti port server.
|
|
|
|
Di Linux/macOS:
|
|
|
|
```bash
|
|
python app.py --port 5000
|
|
```
|
|
|
|
Di Windows:
|
|
|
|
```bash
|
|
py app.py --port 5000
|
|
```
|
|
|
|
Server kemudian dapat diakses di:
|
|
|
|
```bash
|
|
http://localhost:5000
|
|
```
|
|
|
|
Untuk mengganti host dan port sekaligus:
|
|
|
|
Di Linux/macOS:
|
|
|
|
```bash
|
|
python app.py --host 127.0.0.1 --port 5000
|
|
```
|
|
|
|
Di Windows:
|
|
|
|
```bash
|
|
py app.py --host 127.0.0.1 --port 5000
|
|
```
|
|
|
|
Jika kode diubah, server akan otomatis restart karena menggunakan mode `debug=True` dan `reloader=True`.
|
|
|
|
---
|
|
|
|
## 🔐 Autentikasi
|
|
|
|
Semua request ke endpoint API wajib menyertakan header:
|
|
|
|
```bash
|
|
Authorization: Bearer secret123
|
|
```
|
|
|
|
Token default disimpan di `app.py` pada variabel:
|
|
|
|
```python
|
|
API_TOKEN = "secret123"
|
|
```
|
|
|
|
Jika tidak menyertakan token atau token salah, API akan mengembalikan response `401 Unauthorized`.
|
|
|
|
---
|
|
|
|
## 📡 Daftar Endpoint
|
|
|
|
Base URL default:
|
|
|
|
```bash
|
|
http://localhost:8080
|
|
```
|
|
|
|
Jika menjalankan server dengan port lain, misalnya `py app.py --port 5000`, ganti `8080` menjadi `5000`.
|
|
|
|
Semua request ke endpoint API wajib menyertakan header autentikasi:
|
|
|
|
```bash
|
|
Authorization: Bearer secret123
|
|
```
|
|
|
|
### Ringkasan Endpoint
|
|
|
|
| Method | URL | Auth | Fungsi |
|
|
|---|---|---|---|
|
|
| `GET` | `/api/enroll` | Ya | List semua enrollment |
|
|
| `GET` | `/api/enroll?id=<id>` | Ya | Mendapatkan detail enrollment berdasarkan ID |
|
|
| `POST` | `/api/enroll` | Ya | Tambah enrollment baru |
|
|
| `POST` | `/api/enroll` | Ya | Edit enrollment berdasarkan `id` |
|
|
| `POST` | `/api/enroll` | Ya | Hapus enrollment berdasarkan `id` |
|
|
|
|
Endpoint `POST /api/enroll` dapat menerima data dalam format:
|
|
|
|
- Form URL-encoded
|
|
- JSON
|
|
|
|
---
|
|
|
|
## 🧪 Detail Endpoint
|
|
|
|
### 1. `GET /api/enroll` — List Semua Enrollment
|
|
|
|
**Method:** `GET`
|
|
|
|
**URL:**
|
|
|
|
```bash
|
|
http://localhost:8080/api/enroll
|
|
```
|
|
|
|
**Autentikasi:** wajib
|
|
|
|
**Parameter request:** tidak ada
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer secret123" \
|
|
http://localhost:8080/api/enroll
|
|
```
|
|
|
|
**Response sukses:**
|
|
|
|
```json
|
|
{
|
|
"data": [
|
|
{
|
|
"id": 2,
|
|
"type": "Monthly",
|
|
"service": "Lite",
|
|
"user": "bob"
|
|
},
|
|
{
|
|
"id": 1,
|
|
"type": "Annually",
|
|
"service": "Pro",
|
|
"user": "alice"
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
**Response error jika token tidak valid:**
|
|
|
|
```json
|
|
{
|
|
"error": "invalid token"
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
### 2. `GET /api/enroll?id=<id>` — Detail Enrollment Berdasarkan ID
|
|
|
|
**Method:** `GET`
|
|
|
|
**URL:**
|
|
|
|
```bash
|
|
http://localhost:8080/api/enroll?id=1
|
|
```
|
|
|
|
**Autentikasi:** wajib
|
|
|
|
**Parameter request:**
|
|
|
|
| Parameter | Tipe | Wajib | Keterangan |
|
|
|---|---|---|---|
|
|
| `id` | integer | Ya | ID enrollment yang ingin dicari |
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer secret123" \
|
|
"http://localhost:8080/api/enroll?id=1"
|
|
```
|
|
|
|
**Response sukses:**
|
|
|
|
```json
|
|
{
|
|
"data": {
|
|
"id": 1,
|
|
"type": "Annually",
|
|
"service": "Pro",
|
|
"user": "alice"
|
|
}
|
|
}
|
|
```
|
|
|
|
**Response jika ID tidak ditemukan:**
|
|
|
|
```json
|
|
{
|
|
"error": "not found"
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
### 3. `POST /api/enroll` — Tambah Enrollment Baru
|
|
|
|
**Method:** `POST`
|
|
|
|
**URL:**
|
|
|
|
```bash
|
|
http://localhost:8080/api/enroll
|
|
```
|
|
|
|
**Autentikasi:** wajib
|
|
|
|
**Request body:**
|
|
|
|
| Field | Wajib | Keterangan |
|
|
|---|---|---|
|
|
| `type` | Ya | Harus `Annually` atau `Monthly` |
|
|
| `service` | Ya | Harus `Lite`, `Value`, atau `Pro` |
|
|
| `user` | Ya | Nama pengguna |
|
|
|
|
#### Menggunakan Form Data
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/enroll \
|
|
-H "Authorization: Bearer secret123" \
|
|
-d "type=Annually" \
|
|
-d "service=Pro" \
|
|
-d "user=alice"
|
|
```
|
|
|
|
#### Menggunakan JSON
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/enroll \
|
|
-H "Authorization: Bearer secret123" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"type": "Annually",
|
|
"service": "Pro",
|
|
"user": "alice"
|
|
}'
|
|
```
|
|
|
|
**Response sukses:**
|
|
|
|
```json
|
|
{
|
|
"message": "created",
|
|
"id": 1
|
|
}
|
|
```
|
|
|
|
**Response error jika field wajib kosong:**
|
|
|
|
```json
|
|
{
|
|
"error": "type, service, user are required"
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
### 4. `POST /api/enroll` — Edit Enrollment
|
|
|
|
**Method:** `POST`
|
|
|
|
**URL:**
|
|
|
|
```bash
|
|
http://localhost:8080/api/enroll
|
|
```
|
|
|
|
**Autentikasi:** wajib
|
|
|
|
**Request body:**
|
|
|
|
| Field | Wajib | Keterangan |
|
|
|---|---|---|
|
|
| `id` | Ya | ID enrollment yang ingin diubah |
|
|
| `type` | Ya | Harus `Annually` atau `Monthly` |
|
|
| `service` | Ya | Harus `Lite`, `Value`, atau `Pro` |
|
|
| `user` | Ya | Nama pengguna |
|
|
|
|
> Jangan kirim `action=remove` jika ingin melakukan edit. Kirim `action=remove` hanya untuk menghapus data.
|
|
|
|
#### Menggunakan Form Data
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/enroll \
|
|
-H "Authorization: Bearer secret123" \
|
|
-d "id=1" \
|
|
-d "type=Monthly" \
|
|
-d "service=Value" \
|
|
-d "user=bob"
|
|
```
|
|
|
|
#### Menggunakan JSON
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/enroll \
|
|
-H "Authorization: Bearer secret123" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"id": 1,
|
|
"type": "Monthly",
|
|
"service": "Value",
|
|
"user": "bob"
|
|
}'
|
|
```
|
|
|
|
**Response sukses:**
|
|
|
|
```json
|
|
{
|
|
"message": "updated",
|
|
"id": 1
|
|
}
|
|
```
|
|
|
|
**Response jika ID tidak ditemukan:**
|
|
|
|
```json
|
|
{
|
|
"error": "not found"
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
### 5. `POST /api/enroll` — Hapus Enrollment
|
|
|
|
**Method:** `POST`
|
|
|
|
**URL:**
|
|
|
|
```bash
|
|
http://localhost:8080/api/enroll
|
|
```
|
|
|
|
**Autentikasi:** wajib
|
|
|
|
**Request body:**
|
|
|
|
| Field | Wajib | Keterangan |
|
|
|---|---|---|
|
|
| `id` | Ya | ID enrollment yang ingin dihapus |
|
|
| `action` | Ya | Harus bernilai `remove` |
|
|
|
|
#### Menggunakan Form Data
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/enroll \
|
|
-H "Authorization: Bearer secret123" \
|
|
-d "id=1" \
|
|
-d "action=remove"
|
|
```
|
|
|
|
#### Menggunakan JSON
|
|
|
|
**Contoh request:**
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/enroll \
|
|
-H "Authorization: Bearer secret123" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"id": 1,
|
|
"action": "remove"
|
|
}'
|
|
```
|
|
|
|
**Response sukses:**
|
|
|
|
```json
|
|
{
|
|
"message": "removed",
|
|
"id": 1
|
|
}
|
|
```
|
|
|
|
**Response jika ID tidak ditemukan:**
|
|
|
|
```json
|
|
{
|
|
"error": "not found"
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 🧠 Logika `POST /api/enroll`
|
|
|
|
```text
|
|
POST /api/enroll
|
|
│
|
|
├── action = "remove" → hapus data, wajib menyertakan id
|
|
├── ada field id → edit data
|
|
└── tidak ada field id → tambah data baru
|
|
```
|
|
|
|
Untuk tambah dan edit, field berikut wajib dikirim:
|
|
|
|
| Field | Wajib | Keterangan |
|
|
|---|---|---|
|
|
| `type` | Ya | Harus `Annually` atau `Monthly` |
|
|
| `service` | Ya | Harus `Lite`, `Value`, atau `Pro` |
|
|
| `user` | Ya | Nama pengguna |
|
|
|
|
---
|
|
|
|
## ⚠️ Validasi
|
|
|
|
Nilai yang diterima API:
|
|
|
|
```text
|
|
type:
|
|
- Annually
|
|
- Monthly
|
|
|
|
service:
|
|
- Lite
|
|
- Value
|
|
- Pro
|
|
```
|
|
|
|
Jika nilai tidak sesuai, API akan mengembalikan error `400 Bad Request`.
|
|
|
|
Contoh error:
|
|
|
|
```json
|
|
{
|
|
"error": "type must be one of: Annually, Monthly"
|
|
}
|
|
```
|
|
|
|
```json
|
|
{
|
|
"error": "service must be one of: Lite, Value, Pro"
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 🎮 Menggunakan Playground UI
|
|
|
|
Setelah server berjalan, buka browser ke:
|
|
|
|
```bash
|
|
http://localhost:8080
|
|
```
|
|
|
|
Halaman playground dapat digunakan untuk:
|
|
|
|
1. Mengisi token Bearer
|
|
2. Melihat status autentikasi
|
|
3. Mengisi form request
|
|
4. Mengirim request ke endpoint
|
|
5. Melihat response JSON secara langsung
|
|
|
|
---
|
|
|
|
## 🧾 Contoh Response Error
|
|
|
|
| Status | Kondisi | Response |
|
|
|---|---|---|
|
|
| `401` | Tidak menyertakan Authorization header | `{"error": "missing or invalid Authorization header"}` |
|
|
| `401` | Token tidak valid | `{"error": "invalid token"}` |
|
|
| `400` | Field wajib kosong | `{"error": "type, service, user are required"}` |
|
|
| `400` | `type` tidak valid | `{"error": "type must be one of: Annually, Monthly"}` |
|
|
| `400` | `service` tidak valid | `{"error": "service must be one of: Lite, Value, Pro"}` |
|
|
| `400` | `id` bukan integer | `{"error": "id must be an integer"}` |
|
|
| `404` | Data tidak ditemukan | `{"error": "not found"}` |
|
|
|
|
---
|
|
|
|
## 📁 Struktur Project
|
|
|
|
```text
|
|
api-playground/
|
|
├── app.py # Routing API dan konfigurasi aplikasi
|
|
├── db.py # Koneksi dan operasi database SQLite
|
|
├── enroll.db # Database SQLite
|
|
├── requirements.txt # Dependency Python
|
|
├── templates/
|
|
│ └── playground.html # UI playground
|
|
└── README.md # Dokumentasi project
|
|
```
|
|
|
|
---
|
|
|
|
## 🔧 Catatan
|
|
|
|
- Token default hanya untuk development. Untuk production, gunakan token yang lebih aman dan simpan melalui environment variable.
|
|
- Database SQLite disimpan dalam file `enroll.db`.
|
|
- Mode debug dan reloader aktif saat menjalankan `python app.py` / `py app.py`.
|
|
|
|
---
|
|
|
|
## 🤝 Kontribusi
|
|
|
|
Untuk menambahkan fitur baru:
|
|
|
|
1. Buat perubahan pada kode
|
|
2. Jalankan server untuk memastikan tidak ada error
|
|
3. Tes endpoint menggunakan playground UI atau `curl`
|
|
4. Dokumentasikan perubahan jika endpoint atau behavior berubah
|