Add an http GET/POST entry point to a Django with channels websocket

41 Views Asked by At

I'm new in Django and I'm building a site for booking.

I build the front-end in vue 3 and the back-end in django using channels.

I've implemented the websockets but now I'm trying to add a GET or POST entry point for the confirmation via link (like "url/api/confirm/confirm_code" or "url/api/confirm/confirm_code=code") in a mail I sent from the back-end. The problem is that my back-end never receive the request.

I tried like this:

app_name.urls

from django.contrib import admin
from django.urls import path, include

urlpatterns: list[path] = [
    path('admin/', admin.site.urls),
    path('api/', include('app.routing')),
]

app.routing

from django.urls import path

from app.consumers.view import ViewConsumer
from app.consumers.booking import BookingConsumer
from app.consumers.account import AccountConsumer
from app.consumers.profile import ProfileConsumer
from app.http.confirm_reservation import confirm_reservation

websocket_urlpatterns = [
    path(r"ws/view/", ViewConsumer.as_asgi()),
    path(r"ws/booking/", BookingConsumer.as_asgi()),
    path(r"ws/account/", AccountConsumer.as_asgi()),
    path(r"ws/profile/", ProfileConsumer.as_asgi()),
]

urlpatterns = [
    path(r"confirm/<str:confirm_code>/", confirm_reservation, name="confirm_reservation"),
]

app.http.confirm_reservation

from django.http import JsonResponse

from app.services.booking import BookingService


def confirm_reservation(request, confirm_code: str):
    print(request)
    print(confirm_code)
    return JsonResponse(BookingService().confirm_reservation(request))

app_name.asgi

from os import environ

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

from app.routing import websocket_urlpatterns
from app.cronjobs.cron import Cron
from app.events.event import Event

environ.setdefault("DJANGO_SETTINGS_MODULE", "nail_booking_b.settings")

application: ProtocolTypeRouter = ProtocolTypeRouter(
    {
        "http": get_asgi_application(),
        "websocket": AllowedHostsOriginValidator(
            URLRouter(
                websocket_urlpatterns
            )
        ),
    }
)

Cron.start_process()
Event.start_process()

Least but not last, if you have any suggestions for a better code than I wrote, please tell me in a comment.

Thanks.

I'm using my own site and postman (http://192.168.1.5:8080/api/confirm/1234/) to try sending the confirmation code but the front-end get "Cannot GET /api/confirm/1234/" and the back-end doesn't print any data that shows it receive that call, not even any kind of error.

0

There are 0 best solutions below