API REST per una pesatrice

5

Sto creando un'API REST per una pesatrice (da eseguire su un Raspberry Pi, integrato nella bilancia), e mi chiedevo se le mie idee fossero RESTful e corrette.

Ho la "misura" di risorse che puoi usare per recuperare misure precedentemente memorizzate, usando un ID per ottenerne uno specifico:

GET http://1.2.3.4/measurement/<id>

Stavo pensando di usare ID 0 come "la misurazione corrente", cioè cosa pesa la bilancia al momento della chiamata.

Sarebbe 'corretto'? O dovrei usare una risorsa diversa?

    
posta Bart Friederichs 26.04.2017 - 10:08
fonte

2 risposte

6

REST non si cura di quale ortografia si usa per l'URI.

Le considerazioni sulla progettazione degli URI generalmente derivano dai bisogni degli esseri umani che lavorano con loro, piuttosto che da quelli dei client (che trattano gli identificatori come dati opachi). La struttura dell'URI è principalmente gerarchica , con le convenzioni che separano dati gerarchici dal dati non gerarchici . Ma queste linee guida sono convenzioni umane, proprio come le regole sui nomi delle variabili nei tuoi standard di codifica.

/3fa527b4-1036-44b2-a7f3-7770d4b15beb

è un URI perfettamente cromogeno.

I was thinking to use ID 0 as "the current measurement", i.e. what the weigher is weighing at the moment of the call.

La semantica di ciò è perfettamente ragionevole. Fielding, nella sua tesi che definisce REST, ha scritto

The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.

... The only thing that is required to be static for a resource is the semantics of the mapping, since the semantics is what distinguishes one resource from another.

Non è uno spelling che consiglierei; però. Puzza di MagicNumber . Per evitare fraintendimenti da parte dei lettori umani, normalmente vado con uno degli altri consigli qui: /measurements/live , /measurements/current , /measurements?asOf=now , ecc.

Come modo di meccanica, se anche la misurazione corrente ha un identificatore permanente, reindirizzerei le richieste dalla corrente al permanente.

GET /measurements/latest

302 See-Other
Location /measurements/12345

(di nuovo, in REST è perfettamente ragionevole avere molte risorse che condividono la stessa rappresentazione, o condividere la stessa rappresentazione per il momento.)

    
risposta data 26.04.2017 - 14:07
fonte
1

No, questo è assolutamente sbagliato. Significa che l'ID 0 non è in realtà un identificatore, ma piuttosto una cosa magica che potrebbe riferirsi a qualcos'altro ogni volta che lo usi.

Invece, offri un'opzione per ordinare il tuo indice di misurazione ( /measurements ) per tempo, prima la misurazione più recente. Se un chiamante vuole ottenere l'ultima misurazione, semplicemente recupera l'indice e legge quello più recente:

GET http://1.2.3.4/measurements

[
  {
    "value": 78.3,
    "measuredAt": "2017-04-16T10:15:10.0Z"
  },
  {
    "value": 78.4,
    "measuredAt": "2017-04-16T10:15:09.5Z"
  },   
  {
    "value": 78.3,
    "measuredAt": "2017-04-16T10:15:09.0Z"
  },
  {
    "value": 78.2,
    "measuredAt": "2017-04-16T10:15:08.5Z"
  },
  ... and so forth
]

pseudo code:
function getCurrentMeasurement()
  measurements = GET http://1.2.3.4/measurements | json
  return measurements[0]
    
risposta data 26.04.2017 - 10:52
fonte

Leggi altre domande sui tag