在 REST API 中检索相关数据的最佳实践

发布于 2024-12-05 07:38:07 字数 1320 浏览 0 评论 0原文

所以我有一个 REST API,其中有一个资源,其他资源链接到该资源(从编程的角度来看,相关模型)。

所以我现在的做法是,每当我请求资源时,都会通过 URL('/lated_data/lated_data_id/')引用相关资源。

然而,我担心,假设有 5 个与我正在检索的资源相关的资源,我会执行 5 个 GET 请求。我正在编写一个 iPhone 客户端,我想知道这是否是使用 REST 正确执行此操作的方法(我正在返回 URL)。 JSON 响应示例如下:

{
"meta": {
            "limit": 20, 
            "next": null, 
            "offset": 0, 
            "previous": null, 
            "total_count": 2
        }, 
"objects": [
    {
        "away_team": "/api/team/3/", 
        "country": "/api/country/1/", 
        "event_date": "2011-08-16", 
        "event_time": "06:00:00", 
        "event_timezone": "GMT", 
        "home_team": "/api/team/4/", 
        "id": "1", 
        "level": "/api/level/4/", 
        "resource_uri": "/api/event/1/", 
        "tournament": "/api/tournament/1/"
    }, 
    {
        "away_team": "/api/team/4/", 
        "country": "/api/country/1/", 
        "event_date": "2011-09-29", 
        "event_time": "12:00:00", 
        "event_timezone": "UTC", 
        "home_team": "/api/team/3/", 
        "id": "2", 
        "level": "/api/level/1/", 
        "resource_uri": "/api/event/2/", 
        "tournament": "/api/tournament/6/"
    }
]
}

考虑到“每个 URI 必须映射到资源”以及所有这些事情,这是在 REST 中执行此操作的正确方法吗?

我正在使用 Django 和 django-tastypie

提前致谢!

So I have a REST API in which I Have a resource in which other resources are linked to (related models, in programming point of view).

So how I am doing it right now is that whenever I request for a resource, related resources are referenced via URLs ('/related_data/related_data_id/').

However, I worry that, let's say there are 5 related resources to the one I'm retrieving, is that I would perform 5 GET requests. I am writing an iPhone client and I'm wondering if this is how to properly do it using REST (that I'm returning URLs). A sample JSON response is this:

{
"meta": {
            "limit": 20, 
            "next": null, 
            "offset": 0, 
            "previous": null, 
            "total_count": 2
        }, 
"objects": [
    {
        "away_team": "/api/team/3/", 
        "country": "/api/country/1/", 
        "event_date": "2011-08-16", 
        "event_time": "06:00:00", 
        "event_timezone": "GMT", 
        "home_team": "/api/team/4/", 
        "id": "1", 
        "level": "/api/level/4/", 
        "resource_uri": "/api/event/1/", 
        "tournament": "/api/tournament/1/"
    }, 
    {
        "away_team": "/api/team/4/", 
        "country": "/api/country/1/", 
        "event_date": "2011-09-29", 
        "event_time": "12:00:00", 
        "event_timezone": "UTC", 
        "home_team": "/api/team/3/", 
        "id": "2", 
        "level": "/api/level/1/", 
        "resource_uri": "/api/event/2/", 
        "tournament": "/api/tournament/6/"
    }
]
}

Is this a proper way of doing it in REST, considering that "each URI must map to a resource" and all those things?

I am using Django and django-tastypie

Thanks in advance!

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

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

发布评论

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

评论(1

羁绊已千年 2024-12-12 07:38:07

是的;如果相关资源独立更新就可以了。 REST 架构依赖于缓存来提高性能,因此最适合充当原子实体的资源(请参阅 更多信息请点击此处)。这样,您就可以更新资源 B 并使其表示保持新鲜,而无需更新资源 A。请参阅 此评论了解更多设计细节。

Yes; that is proper if the related resources are updated independently. REST architectures depend on caching for performance, and therefore work best with resources which act as atomic entities (see more here). That way, you can update resource B and have its representation be fresh without having to update resource A. See this SO comment for more design details.

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