From 7a6cf9b295b5bb144970e261dcfdcf6aa9ad31ee Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 6 Mar 2024 04:51:43 -0500 Subject: [PATCH] Update API send view to allow different submit types (GET, PUT, etc) --- .../test_app/templates/test_app/api_send.html | 53 +++++++++++++- django_rest/test_app/views.py | 72 ++++++++++++++++--- .../test_app/templates/test_app/api_send.html | 53 +++++++++++++- django_v4/test_app/views.py | 72 ++++++++++++++++--- 4 files changed, 232 insertions(+), 18 deletions(-) diff --git a/django_rest/test_app/templates/test_app/api_send.html b/django_rest/test_app/templates/test_app/api_send.html index e0aedb2..42ac53a 100644 --- a/django_rest/test_app/templates/test_app/api_send.html +++ b/django_rest/test_app/templates/test_app/api_send.html @@ -1,5 +1,6 @@ {% extends "test_app/base.html" %} + {% block page_header %} Django - API Send {% endblock page_header %} @@ -12,6 +13,13 @@ {% block stylesheets %} <style> + form h2, form h3, form h4, form h5, form h6, + .result-box h2, .result-box h3, .result-box h4, .result-box h5, .result-box h6, + .example h2, .example h3, .example h4, .example h5, .example h6 { + margin-top: 6px; + margin-bottom: 6px; + } + form { margin: 10px; padding: 15px; @@ -81,6 +89,16 @@ padding: 0 10px 0 10px; } + .submit-buttons { + display: flex; + flex-direction: row; + } + + .submit-buttons input { + margin: 5px; + padding: 5px; + } + .example { margin-top: 25px; margin-right: 10px; @@ -108,6 +126,10 @@ } .italics { + margin-top: 5px; + margin-bottom: 8px; + + font-size: 90%; font-style: italic; color: #575757; } @@ -128,6 +150,7 @@ </style> {% endblock stylesheets %} + {% block content %} <p>Use this to generate and send test API requests to other projects.</p> @@ -173,7 +196,35 @@ {% endfor %} - <input type="submit" value="Submit"> + <div class="submit-buttons"> + <input + type="submit" + name="submit_get" + value="Submit as GET" + title="Generally used to retrieve data from the server." + > + <input + type="submit" + name="submit_post" + value="Submit as POST" + title="Generally used to send data to the server, and create a new resource." + > + <input + type="submit" + name="submit_put" + value="Submit as PUT" + title="Generally used to send data to the server, and update an existing resource by full replacement." + > + <input type="submit" name="submit_put" value="Submit as PATCH" + title="Generally used to send data to the server, and update an existing resource by partial replacement." + > + <input + type="submit" + name="submit_delete" + value="Submit as DELETE" + title="Generally used to send data to the server, and delete an existing resource." + > + </div> </form> <div class="result-box"> diff --git a/django_rest/test_app/views.py b/django_rest/test_app/views.py index b1327cb..bd5b04c 100644 --- a/django_rest/test_app/views.py +++ b/django_rest/test_app/views.py @@ -221,14 +221,31 @@ def api_send(request): print('Is POST submission.') has_error = False - data = request.POST - form = ApiSendForm(data=data) + post_data = request.POST + form = ApiSendForm(data=post_data) if form.is_valid(): # Handle for form submission. print('Submitted form data:') print('{0}'.format(form.cleaned_data)) + send_type = '' + if 'submit_get' in post_data: + send_type = 'GET' + # data.pop('submit_get') + if 'submit_post' in post_data: + send_type = 'POST' + # data.pop('submit_post') + if 'submit_put' in post_data: + send_type = 'PUT' + # data.pop('submit_put') + if 'submit_patch' in post_data: + send_type = 'PATCH' + # data.pop('submit_patch') + if 'submit_delete' in post_data: + send_type = 'DELETE' + # data.pop('submit_delete') + url = str(form.cleaned_data['url']).strip() get_params = str(form.cleaned_data.get('get_params', '')).strip() header_token = str(form.cleaned_data.get('header_token', '')).strip() @@ -269,12 +286,50 @@ def api_send(request): # Generate API send object. try: - response = requests.post( - url, - headers=headers, - data=data, - timeout=5, - ) + # Generate based on clicked send button. + if send_type == 'GET': + response = requests.get( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'POST': + response = requests.post( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'PUT': + response = requests.put( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'PATCH': + response = requests.patch( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'DELETE': + response = requests.delete( + url, + headers=headers, + data=data, + timeout=5, + ) + + else: + # Unknown send type. Somehow. Raise error. + form.add_error(None, 'Invalid send_type. Was "{0}".'.format(send_type)) except Exception as err: has_error = True response_error['query_sent'] = False if not err.response else True @@ -290,6 +345,7 @@ def api_send(request): # Display sent input data to user. # That way they can change the form for a subsequent request and still see what was sent last time. + sent_data['send_type'] = send_type sent_data['url'] = url sent_data['headers'] = headers sent_data['content'] = data diff --git a/django_v4/test_app/templates/test_app/api_send.html b/django_v4/test_app/templates/test_app/api_send.html index 17755b5..bf13d5f 100644 --- a/django_v4/test_app/templates/test_app/api_send.html +++ b/django_v4/test_app/templates/test_app/api_send.html @@ -1,5 +1,6 @@ {% extends "test_app/base.html" %} + {% block page_header %} Django LTS v4.2 - API Send {% endblock page_header %} @@ -12,6 +13,13 @@ {% block stylesheets %} <style> + form h2, form h3, form h4, form h5, form h6, + .result-box h2, .result-box h3, .result-box h4, .result-box h5, .result-box h6, + .example h2, .example h3, .example h4, .example h5, .example h6 { + margin-top: 6px; + margin-bottom: 6px; + } + form { margin: 10px; padding: 15px; @@ -81,6 +89,16 @@ padding: 0 10px 0 10px; } + .submit-buttons { + display: flex; + flex-direction: row; + } + + .submit-buttons input { + margin: 5px; + padding: 5px; + } + .example { margin-top: 25px; margin-right: 10px; @@ -108,6 +126,10 @@ } .italics { + margin-top: 5px; + margin-bottom: 8px; + + font-size: 90%; font-style: italic; color: #575757; } @@ -128,6 +150,7 @@ </style> {% endblock stylesheets %} + {% block content %} <p>Use this to generate and send test API requests to other projects.</p> @@ -173,7 +196,35 @@ {% endfor %} - <input type="submit" value="Submit"> + <div class="submit-buttons"> + <input + type="submit" + name="submit_get" + value="Submit as GET" + title="Generally used to retrieve data from the server." + > + <input + type="submit" + name="submit_post" + value="Submit as POST" + title="Generally used to send data to the server, and create a new resource." + > + <input + type="submit" + name="submit_put" + value="Submit as PUT" + title="Generally used to send data to the server, and update an existing resource by full replacement." + > + <input type="submit" name="submit_put" value="Submit as PATCH" + title="Generally used to send data to the server, and update an existing resource by partial replacement." + > + <input + type="submit" + name="submit_delete" + value="Submit as DELETE" + title="Generally used to send data to the server, and delete an existing resource." + > + </div> </form> <div class="result-box"> diff --git a/django_v4/test_app/views.py b/django_v4/test_app/views.py index 9baa733..3f02107 100644 --- a/django_v4/test_app/views.py +++ b/django_v4/test_app/views.py @@ -214,14 +214,31 @@ def api_send(request): print('Is POST submission.') has_error = False - data = request.POST - form = ApiSendForm(data=data) + post_data = request.POST + form = ApiSendForm(data=post_data) if form.is_valid(): # Handle for form submission. print('Submitted form data:') print('{0}'.format(form.cleaned_data)) + send_type = '' + if 'submit_get' in post_data: + send_type = 'GET' + # data.pop('submit_get') + if 'submit_post' in post_data: + send_type = 'POST' + # data.pop('submit_post') + if 'submit_put' in post_data: + send_type = 'PUT' + # data.pop('submit_put') + if 'submit_patch' in post_data: + send_type = 'PATCH' + # data.pop('submit_patch') + if 'submit_delete' in post_data: + send_type = 'DELETE' + # data.pop('submit_delete') + url = str(form.cleaned_data['url']).strip() get_params = str(form.cleaned_data.get('get_params', '')).strip() header_token = str(form.cleaned_data.get('header_token', '')).strip() @@ -262,12 +279,50 @@ def api_send(request): # Generate API send object. try: - response = requests.post( - url, - headers=headers, - data=data, - timeout=5, - ) + # Generate based on clicked send button. + if send_type == 'GET': + response = requests.get( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'POST': + response = requests.post( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'PUT': + response = requests.put( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'PATCH': + response = requests.patch( + url, + headers=headers, + data=data, + timeout=5, + ) + + elif send_type == 'DELETE': + response = requests.delete( + url, + headers=headers, + data=data, + timeout=5, + ) + + else: + # Unknown send type. Somehow. Raise error. + form.add_error(None, 'Invalid send_type. Was "{0}".'.format(send_type)) except Exception as err: has_error = True response_error['query_sent'] = False if not err.response else True @@ -283,6 +338,7 @@ def api_send(request): # Display sent input data to user. # That way they can change the form for a subsequent request and still see what was sent last time. + sent_data['send_type'] = send_type sent_data['url'] = url sent_data['headers'] = headers sent_data['content'] = data -- GitLab