2015-11-19 65 views
0

从消费者的角度来看,有没有抽象的任何资源属性值,使场自描述?或者文档应该处理它。REST API抽象资源属性?

的想法是,每一个属性将被包裹在一个更复杂的对象,这将提供fieldId,字段类型,和的值。使每个领域更具描述性。

此外,Web服务将包括另一个端点进一步描述每个字段。

所以,相反的以下内容:

{ 
    "id":123, 
    "type":"person", 
    "attributes":{ 
     "name":"John Smith", 
     "dateOfBirth":"2000-01-01", 
     "ssn":123456789 
    } 
} 

JSON的应该是这样的:

{ 
    "id":123, 
    "type":"person", 
    "attributes":[ 
     { 
     "fieldId":"name", 
     "dataType":"string", 
     "value":"John Smith" 
     }, 
     { 
     "fieldId":"dateOfBirth", 
     "dataType":"date", 
     "value":"2000-01-01" 
     }, 
     { 
     "fieldId":"ssn", 
     "dataType":"integer", 
     "value":123456789 
     } 
    ], 
    "relationships":{ 
     "dataType":{ 
     "links":{ 
      "related":{ 
       "href":"http://acme.com/ws/dataTypes/" 
      } 
     }, 
     "data":[ 
      { 
       "id":"string", 
       "type":"dataType" 
      }, 
      { 
       "id":"date", 
       "type":"dataType" 
      }, 
      { 
       "id":"integer", 
       "type":"dataType" 
      } 
     ] 
     }, 
     "field":{ 
     "links":{ 
      "related":{ 
       "href":"http://acme.com/ws/fields/" 
      } 
     }, 
     "data":[ 
      { 
       "id":"name", 
       "type":"field" 
      }, 
      { 
       "id":"dateOfBirth", 
       "type":"field" 
      }, 
      { 
       "id":"ssn", 
       "type":"field" 
      } 
     ] 
     } 
    } 
} 

再挂一个数据类型资源,将给予一定的说明和/或格式:

{ 
    "id":"ssn", 
    "type":"field", 
    "attributes":{ 
     "valueType":"string", 
     "description":"Social security in the xxx-xx-xxxx format." 
    }, 
    "links":{ 
     "self":{ 
     "href":"http://acme.com/ws/fields/ssn", 
     "meta":{ 
      "httpMethod":"GET" 
     } 
     } 
    } 
} 

{ 
    "id":"date", 
    "type":"dataType", 
    "attributes":{ 
     "valueType":"string", 
     "description":"yyyy-MM-dd" 
    }, 
    "links":{ 
     "self":{ 
     "href":"http://acme.com/ws/dataTypes/date", 
     "meta":{ 
      "httpMethod":"GET" 
     } 
     } 
    } 
} 
+0

或者你选择一个现有的格式:http://sookocheff.com/post/api /上选择-A-超媒体格式/;) – sp00m

回答

1

回答这个问题From the perspective of a consumer, is there any value in abstracting resource attributes to make the fields self-describing? Or should the documentation handle it.

  • 基于经验和评估多个API的API的应只发送所需的数据。处理描述时没有意义,需要文件处理。

  • 加考虑数据的多余量要发送刚刚描述字段

  • 除了前端(说的JavaScript)将需要分析对象,通过仅发送所需要的数据保存时间

考虑这个

{ 
    "id":123, 
    "type":"person", 
    "attributes":{ 
     "name":"John Smith", 
     "dateOfBirth":"2000-01-01", 
     "ssn":123456789 
    } 
} 

采取的带宽相比,这个庞大的数据

{ 
    "id":123, 
    "type":"person", 
    "attributes":[ 
     { 
     "fieldId":"name", 
     "dataType":"string", 
     "value":"John Smith" 
     }, 
     { 
     "fieldId":"dateOfBirth", 
     "dataType":"date", 
     "value":"2000-01-01" 
     }, 
     { 
     "fieldId":"ssn", 
     "dataType":"integer", 
     "value":123456789 
     } 
    ], 
    "relationships":{ 
     "dataType":{ 
     "links":{ 
      "related":{ 
       "href":"http://acme.com/ws/dataTypes/" 
      } 
     }, 
     "data":[ 
      { 
       "id":"string", 
       "type":"dataType" 
      }, 
      { 
       "id":"date", 
       "type":"dataType" 
      }, 
      { 
       "id":"integer", 
       "type":"dataType" 
      } 
     ] 
     }, 
     "field":{ 
     "links":{ 
      "related":{ 
       "href":"http://acme.com/ws/fields/" 
      } 
     }, 
     "data":[ 
      { 
       "id":"name", 
       "type":"field" 
      }, 
      { 
       "id":"dateOfBirth", 
       "type":"field" 
      }, 
      { 
       "id":"ssn", 
       "type":"field" 
      } 
     ] 
     } 
    } 
} 

从消费者的角度来看,他们只提供所需的数据作为回应和文件中的描述。

不把独立的呼吁提供更多的细节,这将是非常难以维持,如果你改变的版本

+0

感谢您分享您的前端视角。这很有道理。 – user2793390