Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #53 from EvanVallet/main
Browse files Browse the repository at this point in the history
api to NATS
  • Loading branch information
EvanVallet authored Jun 24, 2024
2 parents 5073f44 + e4947c4 commit 1628926
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 261 deletions.
25 changes: 25 additions & 0 deletions Django_api/airline/api_common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
from django.shortcuts import get_object_or_404
from .models import *
from .serializers import *
from django.contrib.auth import authenticate
from rest_framework.authtoken.models import Token
from rest_framework.permissions import IsAuthenticated
from rest_framework.exceptions import NotAuthenticated
import asyncio
import nats


class ObtainAuthToken(APIView):
permission_classes = (AllowAny,)
Expand Down Expand Up @@ -283,6 +290,24 @@ def get_queryset(self):
user = self.request.user
return PaymentGateway.objects.filter(transaction__client=user, pk=self.kwargs.get('pk'))

async def valid_payment(self):
global nc
nc = await nats.connect("nats://192.168.164.130:4222")
try:
client = self.request.user
flight_reserv = self.request.Flight.objects.get(id=self.kwargs.get('pk'))
seat = self.request.Flight.objects.get(Plane.objects.get(id=flight_reserv.plane_id).second_class_capacity)+self.request.Flight.objects.get(Plane.objects.get(id=flight_reserv.plane_id).first_class_capacity)
response = await nc.request(f"banque.validation.{client}",timeout=10)
response_data = response.data.decode()
data = response_data.split(",")
payment=data[0]
if payment == "True":
response = await nc.request(f"validation.reservation.place.client",f"{flight_reserv} : {seat}",timeout=10)
data = response_data.split(",")
payment=data[0]
except Exception as e:
print(e)

class TrackCreateView(generics.CreateAPIView):
queryset = Track.objects.all()
serializer_class = TrackSerializer
Expand Down
Binary file modified Django_api/requirements.txt
Binary file not shown.
3 changes: 1 addition & 2 deletions NATS/banque/banque.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ async def handle_payment_request(msg):
reply = msg.reply
data = msg.data.decode()
parts = subject.split('.')
client = parts[1]
client = parts[2]
montant = float(data)

print(f"Requête de paiement reçue : client={client}, montant={montant}")

if client in clients:
if clients[client] >= montant:
autorize = 'true'
print(f"Validation de paiement reçue : autorize={autorize}")

if autorize:
Expand Down
260 changes: 260 additions & 0 deletions NATS/banque/banque_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,263 @@ def populate_combo_box(self, response):
window = MainWindow()
window.show()
sys.exit(app.exec())


'''import sys
import asyncio
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLabel, QComboBox, QStackedWidget, QLineEdit, QInputDialog
from PyQt6.QtCore import QThread, pyqtSignal, pyqtSlot
from nats.aio.client import Client as NATS
class NATSWorker(QThread):
response_received = pyqtSignal(str)
def __init__(self, subject, payload=b'', timeout=20):
super().__init__()
self.subject = subject
self.payload = payload
self.timeout = timeout
async def send_request(self):
nc = NATS()
try:
print(f"Envoi de la requête au sujet {self.subject} avec payload {self.payload}")
await nc.connect("nats://192.168.164.130:4222")
response = await nc.request(self.subject, self.payload, timeout=self.timeout)
await nc.close()
print(f"Réponse reçue : {response.data.decode()}")
self.response_received.emit(response.data.decode())
except Exception as e:
print(f"Erreur lors de l'envoi de la requête : {str(e)}")
self.response_received.emit(f"Error: {str(e)}")
def run(self):
asyncio.run(self.send_request())
class NATSSubscriptionWorker(QThread):
validation_request_received = pyqtSignal(str, str)
def __init__(self, client_name):
super().__init__()
self.client_name = client_name
self.nc = NATS()
async def subscribe_to_validation(self):
await self.nc.connect("nats://192.168.164.130:4222")
await self.nc.subscribe(f"banque.validation.{self.client_name}", cb=self.handle_validation_request)
async def handle_validation_request(self, msg):
client_name = msg.subject.split('.')[-1]
montant = msg.data.decode()
print(f"Requête de validation reçue pour {client_name} : {montant}")
self.validation_request_received.emit(montant, msg.reply)
def run(self):
asyncio.run(self.subscribe_to_validation())
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Client Selector")
self.setGeometry(100, 100, 400, 300)
self.stacked_widget = QStackedWidget()
self.setCentralWidget(self.stacked_widget)
self.page1 = QWidget()
self.page2 = QWidget()
self.page_create_account = QWidget()
self.page_validation = QWidget() # Nouvelle page pour la validation
self.stacked_widget.addWidget(self.page1)
self.stacked_widget.addWidget(self.page2)
self.stacked_widget.addWidget(self.page_create_account)
self.stacked_widget.addWidget(self.page_validation)
self.init_page1()
self.init_page2()
self.init_page_create_account()
self.init_page_validation() # Initialiser la nouvelle page
self.update_accounts_list()
self.client_name = ""
self.validation_subject = ""
self.subscription_worker = None
def init_page1(self):
layout = QVBoxLayout()
self.combo_box = QComboBox()
layout.addWidget(self.combo_box)
button = QPushButton("Select Client")
button.clicked.connect(self.go_to_page2)
layout.addWidget(button)
create_account_button = QPushButton("Create Account")
create_account_button.clicked.connect(self.go_to_page_create_account)
layout.addWidget(create_account_button)
self.page1.setLayout(layout)
def init_page2(self):
layout = QVBoxLayout()
self.label = QLabel("Client Selected:")
layout.addWidget(self.label)
self.response_label = QLabel("Response: ")
layout.addWidget(self.response_label)
pay_button = QPushButton("Pay")
pay_button.clicked.connect(self.make_payment)
layout.addWidget(pay_button)
back_button = QPushButton("Back to Selection")
back_button.clicked.connect(self.go_to_page1)
layout.addWidget(back_button)
self.page2.setLayout(layout)
def init_page_create_account(self):
layout = QVBoxLayout()
self.name_input = QLineEdit()
self.name_input.setPlaceholderText("Enter account name")
layout.addWidget(self.name_input)
self.balance_input = QLineEdit()
self.balance_input.setPlaceholderText("Enter initial balance")
layout.addWidget(self.balance_input)
create_button = QPushButton("Create Account")
create_button.clicked.connect(self.create_account)
layout.addWidget(create_button)
back_button = QPushButton("Back to Selection")
back_button.clicked.connect(self.go_to_page1)
layout.addWidget(back_button)
self.page_create_account.setLayout(layout)
def init_page_validation(self):
layout = QVBoxLayout()
self.validation_label = QLabel("Validation de paiement")
layout.addWidget(self.validation_label)
self.accept_button = QPushButton("Accepter")
self.accept_button.clicked.connect(self.accept_payment)
layout.addWidget(self.accept_button)
self.reject_button = QPushButton("Rejeter")
self.reject_button.clicked.connect(self.reject_payment)
layout.addWidget(self.reject_button)
self.page_validation.setLayout(layout)
def start_subscription(self):
if self.subscription_worker:
self.subscription_worker.terminate()
self.subscription_worker = NATSSubscriptionWorker(self.client_name)
self.subscription_worker.validation_request_received.connect(self.display_validation_request)
self.subscription_worker.start()
@pyqtSlot(str, str)
def display_validation_request(self, montant, subject):
self.validation_subject = subject
self.validation_label.setText(f"Valider le paiement de {montant} ?")
self.stacked_widget.setCurrentIndex(3)
def accept_payment(self):
asyncio.create_task(self.publish_response('true'))
self.stacked_widget.setCurrentIndex(1)
def reject_payment(self):
asyncio.create_task(self.publish_response('false'))
self.stacked_widget.setCurrentIndex(1)
async def publish_response(self, response):
await self.subscription_worker.nc.publish(self.validation_subject, response.encode())
def go_to_page1(self):
self.update_accounts_list()
self.stacked_widget.setCurrentIndex(0)
def go_to_page2(self):
selected_client = self.combo_box.currentText()
self.client_name = selected_client # Store the selected client name
self.label.setText(f"Client Selected: {selected_client}")
subject = f"banque.{selected_client}.compte"
self.nats_worker = NATSWorker(subject)
self.nats_worker.response_received.connect(self.display_response)
self.nats_worker.start()
self.start_subscription() # Start the subscription when a client is selected
self.stacked_widget.setCurrentIndex(1)
def go_to_page_create_account(self):
self.stacked_widget.setCurrentIndex(2)
def display_response(self, response):
self.response_label.setText(f"Response: {response}")
def create_account(self):
name = self.name_input.text()
balance = self.balance_input.text()
if name and balance:
try:
balance = int(balance)
subject = "banque.creation"
payload = f"{name}:{balance}".encode()
self.nats_worker = NATSWorker(subject, payload)
self.nats_worker.response_received.connect(self.account_created_response)
self.nats_worker.start()
except ValueError:
self.response_label.setText("Invalid balance. Please enter a number.")
def account_created_response(self, response):
self.response_label.setText(f"Response: {response}")
self.go_to_page1()
def update_accounts_list(self):
subject = "banque.list_accounts"
self.nats_worker = NATSWorker(subject)
self.nats_worker.response_received.connect(self.populate_combo_box)
self.nats_worker.start()
def populate_combo_box(self, response):
accounts = response.split(',')
self.combo_box.clear()
self.combo_box.addItems(accounts)
def make_payment(self):
amount, ok = QInputDialog.getDouble(self, "Payment", "Enter amount to pay:", min=0)
if ok:
subject = f"banque.{self.client_name}.payment"
payload = str(amount).encode()
self.nats_worker = NATSWorker(subject, payload)
self.nats_worker.response_received.connect(self.display_payment_response)
self.nats_worker.start()
def display_payment_response(self, response):
self.response_label.setText(f"Payment Response: {response}")
def handle_validation_request(self, montant, reply_subject):
print(f"Autorisation automatique de paiement de {montant} pour {self.client_name}")
worker = NATSWorker(reply_subject, b"true")
worker.response_received.connect(self.show_validation_response)
worker.start()
def show_validation_response(self, response):
print(f"Réponse de validation : {response}")
self.response_label.setText(f"Validation Response: {response}")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
'''
Loading

0 comments on commit 1628926

Please sign in to comment.