I have an API I have designed which can return data in 2 different units. To explain this I am tending to use the example of a measurement, in particular temperature. However my use case is slightly more complex, representing a tree of values with percentage values on each level, whether showing this in relation to the parent, or the top of the tree.
I am looking how I would accept a value and a unit for that data. For example say my API returns a bunch of temperatures:
GET:{
temperature_c: 20,
temperature_f: 68
}
Now say I want to set a temperature, I would only expect to receive one temperature as I don't want to force the consumer to calculate a value they don't know/care about.
But is there any recommended RESTful way to do this? I have thought of using a query parameter:
PUT?unit=c:{
temperature: 20
}
Or to add a unit field into the body:
PUT:{
temperature: 20,
unit: c
}
Or, what I feel is worse, forcing a consumer to one of the types:
PUT:{
temperature_c: 20
}
Is there any standard way of approaching this problem? Or any significant drawbacks? Or perhaps there is another method I am missing?
Edit: For information of my intended usecase, I plan to represent a tree structure which exposes weights in two forms 'to top' and 'to parent'. To Top being the overall weight of the node, as a % of the entire tree. To Parent being the weight of the node, as a % compared to it's immediate parent. As an example, imagine this tree structure and weights:
-- Nodes To Top To Parent
Root node 100 100
Node 1.1 80 80
Node 1.1.1 60 75
Node 1.1.2 20 25
Node 1.2 20 20
Node 1.2.1 10 50
Node 1.2.2 10 50
I can easily convert between each, however if a consumer manipulated To Parent for example, I feel they shouldn't also have to work out the To Top weights before doing a PUT/POST.
temperature_c
, I'd expect to deal withtemperature_c
going forward rather than having to remember how to map it. – bitsoflogic Jun 22 '18 at 14:16