Django 测试客户端可以用于生产中的 API 调用吗?

发布于 2024-10-09 23:18:55 字数 732 浏览 0 评论 0原文

我正在使用基于 Piston 构建的 API 构建 Django 应用程序。为了使所有内容尽可能保持 DRY 并使 API 完整,我希望我的内部应用程序调用 API 而不是模型(类似于代理视图控制器 https://github.com/raganwald/homoiconic/blob/master/2010/10/vc_without_m.md< /a> 但目前全部安装在一个 django 上)。所以基本设置是:

Model -> API -> Application -> User Client

我可以重载一些核心 Piston 类来为应用程序创建内部客户端接口,但我想知道是否可以使用 Django 测试客户端来完成同样的事情。因此,要创建一篇文章,而不是调用我将运行的模型:

from django.test.client import Client
c = Client()
article = c.post('/api/articles', {
  'title' : 'My Title',
  'content' : 'My Content'
})

是否有理由我不应该使用测试客户端来执行此操作? (例如性能)是否有更好的工具更适合此特定目的?

I'm building a Django app with an API built on Piston. For the sake of keeping everything as DRY as possible and the API complete, I'd like my internal applications to call the API rather than the models (kind of a proxy-view-controller a la https://github.com/raganwald/homoiconic/blob/master/2010/10/vc_without_m.md but all on one django install for now). So the basic setup is:

Model -> API -> Application -> User Client

I can overload some core Piston classes to create an internal client interface for the application, but I'm wondering if I could just use the Django Test Client to accomplish the same thing. So to create an article, rather than calling the model I would run:

from django.test.client import Client
c = Client()
article = c.post('/api/articles', {
  'title' : 'My Title',
  'content' : 'My Content'
})

Is there a reason I shouldn't use the test client to do this? (performance, for instance) Is there a better tool that's more tailored for this specific purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-10-16 23:18:56

查看 TestClient 的代码后,它似乎没有任何与测试相关的额外开销。相反,它只是充当内部请求的基本客户端。我将使用测试客户端作为内部客户端,并使用 Piston 的 DjangoEmitter 从 API 获取模型对象。

只有测试才能判断内部请求机制是否对性能造成太大影响。

After reviewing the code for TestClient, it doesn't appear to have any additional overhead related to testing. Rather, it just functions as a basic client for internal requests. I'll be using the test client as the internal client, and using Piston's DjangoEmitter to get model objects back from the API.

Only testing will tell whether the internal request mechanism is too much of a performance hit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文