@@ -670,4 +670,81 @@ class ChatSessionMessageView(APIView):
670
670
})
671
671
```
672
672
673
+ Now let's add urls
673
674
675
+ ```
676
+ from django.urls import path
677
+
678
+ from .views import (
679
+ ChatSessionView, ChatSessionMessageView
680
+ )
681
+
682
+ urlpatterns += [
683
+ path('chats/', ChatSessionView.as_view()),
684
+ path('chats/<uri>/', ChatSessionView.as_view()),
685
+ path('chats/<uri>/messages/', ChatSessionMessageView.as_view()),
686
+ ]
687
+ ```
688
+
689
+ Also include chat urls in base urls
690
+
691
+ ```
692
+ urlpatterns += [
693
+ path('auth/', include('djoser.urls')),
694
+ path('auth/', include('djoser.urls.authtoken')),
695
+ path('api/', include('chat.urls')),
696
+ ]
697
+ ```
698
+
699
+ Our endpoints are ready and any AUTHENTICATED user make requests to them
700
+
701
+ Let’s try it out in ** terminal** or ** cmd** :
702
+
703
+ ```
704
+ # command
705
+ curl -X POST http://127.0.0.1:8000/auth/token/create/ --data "username=admin&password=trydjango"
706
+
707
+ # output
708
+ {"auth_token":"6e6b6b6f4f1efe2e3f88db8dbec8da74cc556cf6"}
709
+
710
+ # put auth_token in next command
711
+ curl -X POST http://127.0.0.1:8000/api/chats/ -H "Authorization: Token 6e6b6b6f4f1efe2e3f88db8dbec8da74cc556cf6"
712
+
713
+ # output
714
+ {"status":"SUCCESS","uri":"467f53e415044c0","message":"New chat session created"}
715
+ F:\coder\django_chat\chat>
716
+
717
+ # create auth token for another user
718
+ curl -X POST http://127.0.0.1:8000/auth/token/create/ --data "username=alex&password=trydjango"
719
+ {"auth_token":"169fcd5067cc55c500f576502637281fa367b3a6"}
720
+
721
+ # use uri in next command
722
+ curl -X PATCH http://127.0.0.1:8000/api/chats/467f53e415044c0/ --data "username=alex" -H "Authorization: Token 169fcd5067cc55c500f576502637281fa367b3a6"
723
+
724
+ # output
725
+ 'Authorization: Token 9c3ea2d194d7236ac68d2faefba017c8426a8484'
726
+ {"status":"SUCCESS","members":[{"id":1,"username":"admin","email":"[email protected] ","first_name":"","last_name":""},{"id":2,"username":"alex","email":"","first_name":"","last_name":""}],"message":"alex joined that chat","user":{"id":2,"username":"alex","email":"","first_name":"","last_name":""}}
727
+ ```
728
+
729
+ Let's send some messages...
730
+
731
+ ```
732
+ # command to send message from the auth token generated by usernam = admin
733
+ curl -X POST http://127.0.0.1:8000/api/chats/63c5a282c5b640d/messages/ --data "message=Hello there" -H "Authorization: Token
734
+ 6e6b6b6f4f1efe2e3f88db8dbec8da74cc556cf6"
735
+
736
+ # output
737
+ {"status":"SUCCESS","uri":"63c5a282c5b640d","message":"Hello there","user":{"id":1,"username":"admin","email":"","first_name":"","last_name":""}}
738
+
739
+ # command to send message from alex user using alex user token
740
+ curl -X POST http://127.0.0.1:8000/api/chats/63c5a282c5b640d/messages/ --data "message=Ohh hey there admin whats up" -H "Authorization: Token 0e7497a938f06c83f6f07853480e24ad0448e188"
741
+
742
+ # output
743
+ {"status":"SUCCESS","uri":"63c5a282c5b640d","message":"Ohh hey there admin whats up","user":{"id":3,"username":"archit","email":"[email protected] ","first_name":"","last_name":""}}
744
+
745
+ curl http://127.0.0.1:8000/api/chats/63c5a282c5b640d/messages/ -H "Authorization: Token 6e6b6b6f4f1efe2e3f88db8dbec8da74cc556cf6"
746
+
747
+ # messages history
748
+ {"id":4,"uri":"63c5a282c5b640d","messages":[{"user":{"id":1,"username":"admin","email":"","first_name":"","last_name":""},"message":"Hello there"},{"user":{"id":1,"username":"admin","email":"","first_name":"","last_name":""},"message":"Hello there"},{"user":{"id":3,"username":"archit","email":"","first_name":"","last_name":""},"message":"Ohh hey there admin whats up"}]}
749
+ F:\coder\django_chat\chat>
750
+ ```
0 commit comments