duandanai6470 2014-07-02 13:52
浏览 94
已采纳

REST API设计考虑因素

We are building this REST API layer for out partners where the architecture is something like this:

  • We will have a bunch of REST API scripts built using PHP sitting on our storage server.
  • A partner will hit our API with a transfer request
  • We will fetch the customer data files related to that transfer request and push them to the partner's server.

The problem we are having is with the data files being requested. One transfer request can can ask for hundreds of data files.

Ideally we would prefer getting a single request from the partner for the transfer. (One request that will have all the data files to be transferred).

The other way is that each data file is a separate request and so one order of a hundred files will have 100 requests. This will have additional to and fro traffic as well as individual notification for each file which can be cumbersome.

So my question is - how to format a request to a REST API to retrieve a collection of over hundred objects? If I ask the partner to POST a txt file with the data file names it will not technically be REST, right? What is the ideal way to tackle this?

  • 写回答

2条回答 默认 最新

  • douyong4842 2014-07-02 14:04
    关注

    It sounds like you will need to handle the actual file transfers separately from the REST response, regardless of how you structure the actual API call. And I would suggest allowing an arbitrary number of files to be included in a single API call, this is, as you mention, less traffic to handle, and makes your API easier to use for the client. This does make for large payloads per request (for example a single call requesting a few thousands files would likely be up to 1MB of data), but you can fiddle with a cap on the maximum number of files per request as needed. So, your API will accept requests for file transfers, and then perhaps insert them in a queue of pending file transfers. This allows you to scale the API and the file transfer systems separately, managing the load on each as needed. For example if there is a slowdown in your transfer system, you can still accept requests via your REST API without overloading the system. A sample JSON structure would look something like this:

    {
      files: [ { filepath: /path/to/file, height: 35, width: 100, type: image/jpeg  },
         { filepath: /path/to/file, height: 35, width: 100, type: image/jpeg  }, ]
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)