简体   繁体   English

设计数据驱动的JSON API

[英]Designing a Data-Driven JSON API

I'm currently working on an API that accepts a large batch of data and performs a few different kinds of analysis on the data. 我目前正在开发一种API,该API可以接收大量数据并对该数据执行几种不同类型的分析。 Rather than forcing clients to pass in their data once for each sort of analysis they wish to perform, I thought it would make sense to offer just one API call and have that call accept the data and a list of actions (ie, analysis types) to perform. 我认为,不强迫客户针对他们希望执行的每种分析一次传递其数据,而是提供一个API调用并让该调用接受数据和一系列操作(即分析类型)才有意义。去表演。 (This means that my API isn't RESTful, right?) I'll lose some clarity of function this way, but the gains in processing time will easily make up for that. (这意味着我的API不是RESTful的,对吗?)我将以这种方式失去一些功能的清晰性,但是处理时间的增加将轻松弥补这一点。

My question is about how the requests should be formatted in JSON. 我的问题是关于如何以JSON格式格式化请求。 If the client just needed to POST an array of data elements and an array of actions to perform, then formatting would be easy. 如果客户端只需要发布一个数据元素数组和一个要执行的动作数组,那么格式化将很容易。 Unfortunately, a few of the analysis types have options that need to be set. 不幸的是,一些分析类型具有需要设置的选项。 I see a few ways forward. 我看到一些前进的方向。

  1. Separate actions and options items: 单独的操作和选项项:

     { 'data': [{'id': 1, 'content': 'blah'}, {'id': 2, 'content': 'blah}], 'actions': ['analysis_a', 'analysis_b', 'analysis_c'], 'options': {'option_c': 'blah'} } 

    Here it's up to the documentation to note that option_c only needs to be set if analysis_c is enabled. 在此由文档决定,只有在启用了analysis_c情况下,才需要设置option_c

  2. Separate actions and options items, with options explicitly tied to an action: 单独的操作和选项项,其中选项与操作明确关联:

     { 'data': [{'id': 1, 'content': 'blah'}, {'id': 2, 'content': 'blah}], 'actions': ['analysis_a', 'analysis_b', 'analysis_c'], 'options': {'analysis_c': {'option_c': 'blah'}} } 
  3. Single actions/options item, with item acting as both a list and a dictionary: 单个操作/选项项,该项既充当列表又充当字典:

     { 'data': [{'id': 1, 'content': 'blah'}, {'id': 2, 'content': 'blah}], 'actions': {'analysis_a': {}, 'analysis_b': {}, 'analysis_c': {'option_c': 'blah'}} } 
  4. Single actions/options item with a bit more data structure clarity: 单个操作/选项项目,数据结构更加清晰:

     { 'data': [{'id': 1, 'content': 'blah'}, {'id': 2, 'content': 'blah}], 'actions': [ {'action': 'analysis_a'}, {'action': 'analysis_b'}, {'action': 'analysis_c', 'options': {'option_c': 'blah'}} ] } 

Option #1 is probably my least favorite, but in the near future a few of our analysis types will require that the same option be set (necessarily with the same value), and that situation is a little easier to handle with Option #1. 选项#1可能是我最不喜欢的选项,但是在不久的将来,我们的一些分析类型将要求设置相同的选项(必须使用相同的值),并且使用选项#1来处理这种情况要容易一些。

I'm pretty torn, so any thoughts or suggestions would be much appreciated. 我很伤心,所以任何想法或建议都将不胜感激。 Thanks! 谢谢!

I appreciate option #3 and option #4, because each action is self contained, which offers more clarity. 我赞赏选项#3和选项#4,因为每个动作都是独立的,可以提供更多的清晰度。

Whether choosing option #3 or option #4 depends on how you will perform the action, if the server have some operation that will retrieve a specific action from the action list by name. 如果服务器具有某些操作,可以按名称从操作列表中检索特定操作,则选择选项#3还是选择选项#4取决于操作的方式。 option #3 might be better(because option #4 requires you to iterate the array), if not, I think option #4 is better. 选项3可能更好(因为选项4需要您迭代数组),否则,我认为选项4更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM