-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
125 lines (110 loc) · 5.09 KB
/
Copy pathclient.py
File metadata and controls
125 lines (110 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import requests
import threading
import time
# --- Konfigurasi Klien ---
BASE_API_URL = "http://127.0.0.1:5000"
# Data untuk diuji oleh klien: Daftar ISBN buku yang akan dicek
ISBN_UNTUK_DICEK = [
"978-1408855652",
"978-0261102217",
"999-9999999999",
"978-0321765723",
] # Satu ISBN tidak ada
# ==============================================================================
# SOAL: Implementasi Fungsi untuk Cek Ketersediaan Buku via API
# ==============================================================================
def client_cek_buku_via_api(isbn, thread_name):
"""
TUGAS ANDA:
Lengkapi fungsi ini untuk mengambil informasi ketersediaan buku dari API
dan mencetak hasilnya ke konsol.
Langkah-langkah:
1. Bentuk URL target: f"{BASE_API_URL}/buku/{isbn}/ketersediaan"
2. Cetak pesan ke konsol bahwa thread ini ('thread_name') memulai pengecekan untuk 'isbn'.
Contoh: print(f"[{thread_name}] Mengecek buku ISBN: {isbn}")
3. Gunakan blok 'try-except' untuk menangani potensi error saat melakukan permintaan HTTP.
a. Di dalam 'try':
i. Kirim permintaan GET ke URL target menggunakan 'requests.get()'. Sertakan timeout.
ii. Periksa 'response.status_code':
- Jika 200 (sukses):
- Dapatkan data JSON dari 'response.json()'.
- Cetak judul dan status buku ke konsol.
Contoh: print(f"[{thread_name}] Buku '{data.get('judul')}': Status {data.get('status')}")
- Jika 404 (buku tidak ditemukan):
- Cetak pesan bahwa buku tidak ada di katalog.
Contoh: print(f"[{thread_name}] Buku dengan ISBN {isbn} tidak ada di katalog.")
- Untuk status code lain:
- Cetak pesan error umum ke konsol.
b. Di blok 'except requests.exceptions.Timeout':
- Cetak pesan bahwa permintaan timeout ke konsol.
c. Di blok 'except requests.exceptions.RequestException as e':
- Cetak pesan error permintaan umum ke konsol.
4. Setelah blok try-except, cetak pesan ke konsol bahwa thread ini ('thread_name') selesai memproses 'isbn'.
"""
target_url = f"{BASE_API_URL}/buku/{isbn}/ketersediaan"
print(f"[{thread_name}] Mengecek buku ISBN: {isbn}")
try:
response = requests.get(target_url, timeout=5)
if response.status_code == 200:
data = response.json()
print(
f"[{thread_name}] Buku '{data.get('judul')}': Status {data.get('status')}"
)
elif response.status_code == 404:
print(f"[{thread_name}] Buku dengan ISBN {isbn} tidak ada di katalog.")
else:
print(
f"[{thread_name}] Terjadi error (status code: {response.status_code}) saat mengecek buku."
)
except requests.exceptions.Timeout:
print(f"[{thread_name}] Permintaan timeout saat mengecek buku ISBN {isbn}.")
except requests.exceptions.RequestException as e:
print(f"[{thread_name}] Error permintaan: {e}")
print(f"[{thread_name}] Selesai memproses buku ISBN: {isbn}")
def client_cek_antrean_via_api(id_antrean, thread_name):
"""
Fungsi untuk mengambil status antrean dari API dan mencetak hasilnya ke konsol.
"""
target_url = f"{BASE_API_URL}/antrean/{id_antrean}/status"
print(f"[{thread_name}] Mengecek status antrean ID: {id_antrean}")
try:
response = requests.get(target_url, timeout=5)
if response.status_code == 200:
data = response.json()
print(
f"[{thread_name}] Antrean '{data.get('nama')}': Status {data.get('status')}"
)
elif response.status_code == 404:
print(f"[{thread_name}] Antrean dengan ID {id_antrean} tidak ditemukan.")
else:
print(
f"[{thread_name}] Terjadi error (status code: {response.status_code}) saat mengecek antrean."
)
except requests.exceptions.Timeout:
print(
f"[{thread_name}] Permintaan timeout saat mengecek antrean ID {id_antrean}."
)
except requests.exceptions.RequestException as e:
print(f"[{thread_name}] Error permintaan: {e}")
print(f"[{thread_name}] Selesai memproses antrean ID: {id_antrean}")
# --- Bagian Utama Skrip (Tidak Perlu Diubah Peserta) ---
if __name__ == "__main__":
print(
f"Memulai Klien Pengecek untuk {len(ISBN_UNTUK_DICEK)} Buku Secara Concurrent."
)
threads = []
start_time = time.time()
for i, isbn_cek in enumerate(ISBN_UNTUK_DICEK):
thread_name_for_task = f"Mahasiswa-{i+1}"
thread = threading.Thread(
target=client_cek_buku_via_api, args=(isbn_cek, thread_name_for_task)
)
threads.append(thread)
thread.start()
for thread_instance in threads:
thread_instance.join()
end_time = time.time()
total_time = end_time - start_time
print(
f"\nSemua pengecekan buku telah selesai diproses dalam {total_time:.2f} detik."
)