从DjangoRestFramework中的API端点动态请求数据 [英] Dynamic request of data from API endpoint in DjangoRestFramework

查看:67
本文介绍了从DjangoRestFramework中的API端点动态请求数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从外部来源进行一些API调用,但希望使其动态化,而不是在提供的DRF UI中将引用号手动放入我的视图中.

I am making some API calls from an external source but would like to make it dynamic instead of manually putting the reference number in my views in the DRF UI provided.

我想要的是在DRF用户界面中,我应该有一个字段,当我输入参考编号时,我应该从API获取响应,我已经成功地手动完成了此操作,但是我想使其动态DRF用户界面.

What I want is that in my DRF UI, I should have a field whereby when I enter a reference number, I should get the response from from the API, I am successfully doing this manually but I want to make it dynamic from the DRF UI.

我还想在我的DRF UI中获得更好格式的JSON响应.下面的图像可以更好地解释我的意思

I would also like to get a better formatted JSON Response in my DRF UI. An image is below to better explain what I meant

Views.py

class Paystack(APIView):
    def get(self, request):
        url = "https://api.paystack.co/transaction/verify/{{REFERENCE_NO}}"
        payload = {}
        files = {}
        headers = {
            'Authorization': 'Bearer SECRET_KEY',
            'Content-Type': 'application/json'
        }

        response = requests.request("GET", url, headers=headers, data= payload, files=files)
        return Response(response)

    def post(self, request):
        url = "https://api.paystack.co/transaction/verify/{{REFERENCE_NO}}"
        payload = {}
        files = {}
        headers = {
            'Authorization': 'Bearer SECRET_KEY',
            'Content-Type': 'application/json'
        }

        response = requests.request("GET", url, headers=headers, data= payload, files=files)
        return Response(response)

urls.py

from django.urls import path, include
from .views import *
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('paystack', Paystack, basename='paystack')

urlpatterns = [
    path('paystack/', Paystack.as_view(), name='paystack'),
]

目前,我的DRF用户界面看起来像这样,

Presently, my DRF UI looks likes this,

推荐答案

如果您希望能够在DRF UI中获取reference_id,则必须定义一个序列化器并从中获取值,或者可以定义一个要求参考ID的网址格式.

If you want to be able to get reference_id in your DRF UI, you must either define a serializer and catch the value from that, or, you can define a URL pattern which asks for a reference ID.

您可以做到

在urls.py

urlpatterns = [
path('paystack/<str:reference_id>', Paystack.as_view(), name='paystack'),

]

在您的意见中.py

class Paystack(APIView):

    def get(self, request, reference_id):
        url = f"https://api.paystack.co/transaction/verify/{reference_id}"
        payload = {}
        files = {}
        headers = {
            'Authorization': 'Bearer SECRET_KEY',
            'Content-Type': 'application/json'
        }

        response = requests.request("GET", url, headers=headers, data= payload, files=files)
        return Response(response)

这篇关于从DjangoRestFramework中的API端点动态请求数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆