What's the best way to handle resource properties which must be modified/updated through another method that is not exposed to the API consumer?
Examples:
Requesting a new token to used for X. The token must be generated following a specific set of business rules/logic.
Requesting/refreshing the exchange rate of a currency after the old rate expires. The rate is for informational purposes and will be used in subsequent transactions.
Note that in the above two examples, the values are properties of a resource and not separate resources on their owns.
What's the best way to handle these types of scenarios and other scenarios where the API consumer doesn't have control of the value of the property, but needs to request a new one. One option would be to allow a PATCH
with that specific property in the request body but not actually update the property to the value specified, instead, run the necessary logic to update the property and return the updated resource.
Lets look at #1 in more detail:
Request:
GET /User/1
Response:
{
"Id": 1,
"Email": "myemail@gmail.com",
"SpecialToken": "12345689"
}
As the consumer of the API, I want to be able to request a new SpecialToken
, but the business rules to generate the token are not visible to me.
How do I tell the API that I need a new/refreshed SpecialToken
with in the REST paradigm?
One thought would be to do:
Request:
PATCH /User/1
{
"SpecialToken": null
}
The server would see this request and know that it needs to refresh the token. The backend will update the SpecialToken
with a specific algorithm and return the updated resource:
Response:
{
"Id": 1,
"Email": "myemail@gmail.com",
"SpecialToken": "99999999"
}
This example can be extended to example #2 where SpecialToken
is an exchange rate on resource CurrencyTrade
. ExchangeRate
is a read only value that the consumer of the API can't change directly, but can request for it to be changed/refreshed:
Request:
GET /CurrencyTrade/1
Response:
{
"Id": 1,
"PropertyOne": "Value1",
"PropertyTwo": "Value2",
"ExchangeRate": 1.2
}
Someone consuming the API would need a way to request a new ExchangeRate, but they don't have control of what the value will be, it's strictly a read only property
.
How about a separate resource which is responsible for refreshing the token within the User resource?
This could return the refreshed User representation (with the new token) in the response.
You're really dealing with two different representations of the resource: one for what the client can send via POST / PUT, and one for what the server can return. You are not dealing with the resource itself.
What are the requirements for being able to update a token? What is the token for? Can a token be calculated from the other values in User? This may just be an example, but context will drive how you end up building the system.
Unless there were a requirement which prohibited it, I would probably implement the token generation scenario by "touching" the resource representation using a PUT. Presumably the client can't update the Id field, so it would not be defined in the client's representation.
Request
Response
From the client's perspective,
Email
is the only field which is mutable, so this represents the complete representation of the resource when the client sends a message to the server. Since the server's response contains additional, immutable information, it's really sending a different representation of the same resource. (What's confusing is that, in the real world, you don't usually see the media type spelled out so clearly... it's often wrapped in something vague like application/json).For your exchange rate example, I don't understand why the client would have to tell the server that the exchange rate was stale. If the client knew more about the freshness of the exchange rate than the server did, and the server is serving up the value, it's not a very good service. :) But again, in a scenario like this, I'd "touch" the resource like I did with the User scenario.
Another thought just occurred to me. Rather than model a RefreshToken resource, you could simply POST the existing special token to a RevokedTokens collection that's associated with this User (assuming that only one special token is allowed per user at a given time).
Request:
Response:
Following the token-revocation relation and POSTing the existing special token would then look like this:
Request:
Response: 202 Accepted (or 204 No Content)
A subsequent GET for the user would then have the new special token assigned to it:
Request:
Response:
This has the advantage of modeling an actual resource (a token revocation list) which can effect other resources, rather than modeling a service as a resource (i.e., a token refresher resource).
I reckon you should consider making
SpecialToken
a resource, and allow consumers of the api toPOST
to it to retrieve a new instance. Somehow, you'll want to link theUser
resource to aSpecialToken
resource. Remember, one of the central tenets of REST is that you should not depend on out-of-band information so if you want to stay true to that you'll want to investigate the possibility of using links.First, let's look at what you've got:
Request:
Response:
While this response does include the
SpecialToken
property in the object, because theContent-Type
isapplication/json
will not actually mean anything to clients that aren't programmed to understand this particular object structure. A client that just understands JSON will take this as an object like any other. Let's ignore that for now. Let's just say we go with the idea of using a different resource for theSpecialToken
field; it might look something like this:Request:
Response:
Because we did a
GET
, making this call ideally shouldn't modify the resource. ThePOST
method however doesn't follow those same semantics. In fact, it may well be that issuing aPOST
message to this resource could return a different body. So let's consider the following:Request:
Response:
Note how the
POST
message doesn't include a body. This may seem unconventional, but the HTTP spec doesn't prohibit this and in fact the W3C TAG says it's all right:Sounds about right to me. Back in the day, I've heard some servers had problems with
POST
messages without a body, but I personally have not had a problem with this. Just make sure theContent-Length
header is set appropriately and you should be golden.So with that in mind, this seems like a perfectly valid way (according to REST) to do what you're suggesting. However, remember before when I mentioned the bits about JSON not actually having any application level semantics? Well, this means that in order for your client to actually send a
POST
to get a newSpecialToken
in the first place, it needs to know the URL for that resource, or at least how to craft such a URL. This is considered a bad practice, because it ties the client to the server. Let's illustrate.Given the following request:
If the server no longer recognizes the URL
/User/1/SpecialToken
, it might return a 404 or other appropriate error message and your client is now broken. To fix it, you'll need to change the code responsible. This means your client and server can't evolve independently from each other and you've introduced coupling. Fixing this however, can be relatively easy, provided your client HTTP routines allow you to inspect headers. In that case, you can introduce links to your messages. Let's go back to our first resource:Request:
Response:
Now in the response, there's a link specified in the headers. This little addition means your client no longer has to know how to get to the
SpecialToken
resource, it can just follow the link. While this doesn't take care of all coupling issues (for instance,token
is not a registered link relation,) it does go a long way. Your server can now change theSpecialToken
URL at will, and your client will work without having to change.This is a small example of HATEOAS, short for Hypermedia As The Engine Of Application State, which essentially means that your application discovers how to do things rather than know them up front. Someone in the acronym department did get fired for this. To wet your appetite on this topic, there's a really cool talk by Jon Moore that shows an API that makes extensive use of hypermedia. Another nice intro to hypermedia is the writings of Steve Klabnik. This should get you started.
Hope this helps!
There are many approaches to that. I'd say the best one is probably to have a
/User/1/SpecialToken
resource, that gives a202 Accepted
with a message explaining that the resource can't be deleted completely and will be refreshed whenever someone tries to. Then you can do that with a DELETE, with a PUT that replaces it with a null value, and even with a PATCH directly to SpecialToken or to the attribute of User. Despite what someone else mentioned, there's nothing wrong with keeping theSpecialToken
value in the User resource. The client won't have to do two requests.The approach suggested by @AndyDennie, a POST to a TokenRefresher resource, is also fine, but I'd prefer the other approach because it feels less like a customized behavior. Once it's clear in your documentation that this resource can't be deleted and the server simply refreshes it, the client knows that he can delete or set it to null with any standardized action in order to refresh it.
Keep in mind that in a real RESTful API, the hypermedia representation of user would just have a link labeled "refresh token", with whatever operation is done, and the semantics of the URI wouldn't matter much.