RESTful webservice API:如何處理100個音樂文件的Web URI路由

[英]RESTful webservice API : how to handle a web URI route for a 100 music files


I am a totally newbie to the world of REST API. from what I have read and I think I understand 2 main things .

我是REST API世界的新手。從我所讀到的,我認為我理解2個主要的事情。

1) REST uses the the same HTTP methods (GET PUT etc) for a specific resource on the server.

1)REST對服務器上的特定資源使用相同的HTTP方法(GET PUT等)。

2) Every resource on my server must be accessible from a unique URI.

2)我的服務器上的每個資源都必須可以從唯一的URI訪問。

So Combining 1 and 2, I can send a HTTP request to send a resource URI (eg. Radio.com/Music/1 and a GET request which will then fetch Music song 1).

因此,結合1和2,我可以發送HTTP請求以發送資源URI(例如,Radio.com/Music/1和GET請求,然后將獲取音樂歌曲1)。

Now I have a simple HTML page which has links to 100 Music files on the server. The idea is that the client will click on a link and then Should be able to play/download the music file (I am not so concerned about the how part related to audio). So the server must provide the links to each Music file eg: Radio.com/Music/1 .

現在我有一個簡單的HTML頁面,其中包含指向服務器上100個音樂文件的鏈接。這個想法是客戶端點擊一個鏈接,然后應該能夠播放/下載音樂文件(我不太關心音頻的相關部分)。因此服務器必須提供每個音樂文件的鏈接,例如:Radio.com/Music/1。

My Question: Q1) In this way must the should the client be exposed to each and every URI for the resource. So When the hosting the html page should i put these links individually for each music file link

我的問題:Q1)通過這種方式,客戶端必須暴露於資源的每個URI。因此,當托管html頁面時,我應該為每個音樂文件鏈接單獨放置這些鏈接

Q2) We need to have a route to each and every resource for the 100 music files on the server . If I am doing in Sinatra like do I need a do route for every of the 100 music files ? Something link '/do' /Music/1 , '/do' /Music/2 and so on

Q2)我們需要為服務器上的100個音樂文件提供到每個資源的路由。如果我在Sinatra這樣做,我是否需要為100個音樂文件中的每一個做一個路線?鏈接'/ do'/ Music / 1,'/ do'/ Music / 2等等

(Isn't this cumbersome and if I add another 100 music files , then I need to add another 100 routes in Sinatra).

(這不是很麻煩,如果我再添加100個音樂文件,那么我需要在Sinatra中添加另外100條路線)。

Is this how the REST API would work, or have I totally got it wrong?

這是REST API如何工作,還是我完全弄錯了?

2 个解决方案

#1


Imagine you have two servers.

想象一下,你有兩台服務器。

  1. First server is your api server: http://api.yourhost.com/v1/... (java, node.js, etc...) This REST-server will return a simple json response:

    第一台服務器是你的api服務器:http://api.yourhost.com/v1 / ...(java,node.js等...)這個REST服務器將返回一個簡單的json響應:

    {
    "base_url"  : "http://static.yourhost.com/music/",
    "tracks"    : [
            {
                "name" : "track1",
                "file_name" : "track1.mp3"
                "file_size" : "3.43MB"
            },
            {
                "name" : "track2",
                "file_name" : "track2.mp3"
                "file_size" : "6.12MB"
            }
    
            [...]
    ]
    }
    
  2. The second server: http://static.yourhost.com/ is your content delivery server (apache, nginx, etc...)

    第二台服務器:http://static.yourhost.com/是您的內容交付服務器(apache,nginx等...)

The idea is, that your api REST server should never send binary data like audio or image. It sends json/xml only.

我們的想法是,您的api REST服務器永遠不應該發送音頻或圖像等二進制數據。它只發送json / xml。

You can also send the full url for every audio file, but this information could be redundant.

您還可以為每個音頻文件發送完整的URL,但此信息可能是多余的。

#2


ad 1)

When the hosting the html page should i put these links individually for each music file link

當托管html頁面時,我應該為每個音樂文件鏈接單獨放置這些鏈接

If you take REST seriously and if you see this HTML page as a representation of the collection resource of all music files, you should do this. One basic mechanism of REST is links for the client. The client should be able to just follow links without having to build them himself.

如果您認真對待REST,並且如果您將此HTML頁面視為所有音樂文件的集合資源的表示,那么您應該這樣做。 REST的一個基本機制是客戶端的鏈接。客戶端應該能夠只跟蹤鏈接而無需自己構建鏈接。

ad 2)

We need to have a route to each and every resource for the 100 music files on the server.

我們需要為服務器上的100個音樂文件提供到每個資源的路由。

Every library that implements a RESTful server is able to have routes with path paramters. Usually such a route is defined using a pattern like

實現RESTful服務器的每個庫都可以擁有帶路徑參數的路由。通常使用類似的模式來定義這樣的路線

/Music/{id}

where {id} is the path parameter. The code that is bound to this route will then have available a variable named id. Using this approach you need only one route.

其中{id}是路徑參數。綁定到此路由的代碼將具有名為id的變量。使用這種方法,您只需要一條路線。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/03/725ead4b808a3537977a224bc4414746.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com