Is a RESTful PUT with no data "kosher," or should a DELETE always be used? - api

I have a RESTful route that works on an array field of a resource, such:
PUT /:id/mylist
When I do a PUT, I throw an error if the input is empty. That is, if an empty array is passed. I require at least one element in the array. So if the resource has an array of nine elements, and the route is called to PUT three, those three replace the existing nine.
But you cannot pass in no elements, because that would erase the nine and leave nothing.
Having no element IS ALLOWED, however - it just seems to me that allowing the array to be "cleared" in a PUT is wrong, and that it should only be done thusly:
DELETE /:id/mylist
Am I wrong? Are both okay? Is one preferred over the other?

I would think that doing DELETE on a list resource would infer that the list is no longer there and future GET requests to the URL would return a 404.
However, doing a PUT with an empty list would cause future GET requests to return a 200 and an empty list (however that is represented).
I would say both are valid approaches, it just depends one what are the most natural semantics for the resource.

Related

Clarifying behavior of unset

I wanted to clarify my understanding of unset() or rather the behavior I am observing. I understand if I call unset() it replaces the value with a null (per the deleting data in Gun). So this is what I would like to confirm, assuming you've called unset():
1) When you call once() or on() it returns null for nodes which have been unset()
2) When you call Gun.obj.empty(table, '_') it returns false
I also tried setting the value of my set to null e.g.
get('mylist').put(null)
Which worked! I wanted to empty my set. However, the next time I added a new node my original set along with all of the original nodes were restored. I ended up writing the following to empty my set
this.context.once().map().once(data => {
let key = data["_"]["#"];
let node = this.context.get(key);
if (node) {
this.context.unset(node);
}
});
I believe that .unset tombstones (null) an item in the table, but not the table itself. Just so others are aware: .unset is community maintained inside of the GUN repo, and not maintained by me - so I may be wrong about its behavior OR the extension may be out of date.
Gun.obj.empty({}) is just a utility to check if an object is empty or not. The 2nd parameter lets you pass it a property to ignore (like '_' which every GUN node has on it in the JS implementation).
You are correct though, that gun.get('list').put(null) should "clear out" the list, such that when you go to save data to it again (or call .set(item) to add an item) this should force/cause GUN to generate a new list/table/set/collection.
It is wrong behavior for it to "resurrect" the old list (unless another peer was trying to at the same time re-write to the old list at the same parent context), so this should be considered a bug that needs to be fixed. (Probably due to the old list ID being cached and not cleared from memory properly)

What is the correct query parameter for "match-all" in a REST filter when there is a default?

If a REST API endpoint gets all, then filters are easy. Lack of a filter means "get all results".
GET /persons - gets all persons
GET /persons?name=john - gets all persons with name of "john"
But if there is a default, then I need some way to explicitly not set the default filter. Continuing the above example, if each person has a state, "approved" or "pending", and if my system is set such that if I do not explicitly specify a state, it will return all "approved":
GET /persons - gets all approved persons, because defaults to state=approved
GET /persons?state=approved - same thing, gets all approved persons
GET /persons?state=pending - gets all pending persons
How do I get all persons? What if there are 10 possible states? Or 100?
I can think of a few ways:
GET /persons?state=any - but then I can never use the actual state any
GET /persons?state=* - would work, but feels strange? Or is it?
GET /persons?state= - some URL parsing libraries would complain about a blank query parameter, and does this not imply "state is empty" as opposed to "state is anything"?
How do I say in my GET, "override the default for the state to be anything"?
Maybe this could work for you:
GET /persons?state - gets all persons that have a state name, no matter which value
GET /persons?state= - gets all persons that have an empty value for the state name
You probably don’t need to differentiate between these two situations, so you could use either one for getting all persons with the state name (I just think that the variant without = is more beautiful).
FWIW, the application/x-www-form-urlencoded format (i.e., typically used in HTML forms) doesn’t differ between an empty and no value.
As far as the URI standard is concerned, this name-value pair syntax in the query component is only a convention anyway, so you can use whichever syntax/semantics you wish.
I don't think there is one answer to this question. As long as you document that the default state is approved well I don't think it matter to the clients if you pass any, * etc. All of your proposals are fine except the last one. I don't think that is a good one.
If I was designing the API I would use all and keep this as a standard. I would also recommend to use paging for all endpoints that returns list of elements. I use offset and limit as paging query parameters. In my API I return 20 elements as default if the client haven't specified another paging criteria.

How to identify Drive ID?

The new Google Drive Android API has 2 types of string IDs available, the 'resource' ID and the 'encoded' ID.
'encoded' id from DriveId.encodeToString()
"DriveId:CAESHDBCMW1RVVcyYUZKZmRhakIzMDBVbXMYjAUgssy8yYFRTTNKRU55"
'resource' id from DriveId.getResourceId()
"UW2aFJfdajB3M3JENy00Ums0B1mQ"
In the process I end-up with a string that can contain any one of them (result of some timing issues). My question is:
If I need to 'parse' the string in order to identify the type, is there a characteristic I can rely on? For instance:
'encoded' id will always start with 'DriveId:' substring
'resource' id will have some length limit
can I abuse error return from 'decodeFromString()'?
or should I form (pre-pend) the string container with my own tag? What could be the minimal 'safe' tag (i.e. what will never appear in the beginning of these ids) ?
Please point me in the right direction so I don't have to re-do it with the next release.
I have run into yet another issue that should be mentioned here so others don't waste time falling into the same pit. The 'resourceID' can be ported and will remain unique for the object it identifies, where 'encodedID' has only 'device' scope. Means that you CAN'T transfer 'encodedID' to another device (with the same account) and try to retrieve file/folder with it. So I assume it is unique to a Google Play Services instance.
Please do not rely on any formatting of either ID type. This are subject to change without notice.
If you need to use both, and track the differences between them you should have your own method of doing so within your app.
Really, you should probably always just store the encoded ID since this one is always guaranteed to present, and if it contains a resourceId, its easy to get back out.

character manipulation?

the idea is to take a word and sub out all specified letters for another letter.
Any help on how to make this kind of function work?
The last array_push is not being called because you're returning before. Change it to:
array_push($stepsinchain, $subed);
return $subed;
Since $subed is never stored in the $stepsinchain array, due to the return being before, you're not able to access previous alternations.
array_push is also slower and not recommended when entering one element in an array. Instead, use
$stepsinchain[] = $subed;
It is also much faster as documented at http://www.php.net/manual/en/function.array-push.php#Hcom83388

String Template: is it possible to get the n-th element of a Java List in the template?

In String Template one can easily get an element of a Java Map within the template.
Is it possible to get the n-th element of an array in a similar way?
According to the String Template Cheat Sheet you can easily get the first or second element:
You can combine operations to say things like first(rest(names)) to get second element.
but it doesn't seem possible to get the n-th element easily. I usually transform my list into a map with list indexes as keys and do something like
map.("25")
Is there some easier/more straightforward way?
Sorry, there is no mechanism to get a[i].
There is no easy way getting n-th element of the list.
In my opinion this indicates that your view and business logic are not separated enough: knowledge of what magic number 25 means is spread in both tiers.
One possible solution might be converting list of values to object which provides meaning to the elements. For example, lets say list of String represents address lines, in which case instead of map.("3") you would write address.street.