Multiple ID's in REST endpoints - javascript

coming from a Wordpress background my knowledge on using REST API's is quite limited. I've started using JSON server (dummy local rest API) to learn the ins and outs.
Looking at the documentation, there doesn't seem to be any way to delete, update or post multiple items in one go? The PATCH, POST, PUT and DELETE methods all require an endpoint structured with one trailing ID eg /posts/1. I've tried both sending multiple ID's in the url ie ?id=1&id=2 and also as part of the request body but neither seem to work.
Is this how typical REST API's work, and if so does this mean i would have to loop though ID's and send multiple requests for each, or am i missing the point??

With REST, you don't typically have bulk operations, unless you are, say, deleting a collection. A POST request usually inserts a single entity into a collection, although that doesn't mean that you CANNOT construct the endpoint to accept an array of entities to insert into the collection. When I design a RESTful endpoint for POSTing a new resource, I only accept a single entity. I leave it up to the client to perform multiple POST requests in parallel to reduce the time it takes to create the list of entities.

Related

Loading multiple data objects from backend API - all at once or every data object in its own call?

Lets say i have a form that i use to edit a Customer. In addition to various input fields it will also have multiple drop down lists to set some fields (eg. Country, Category, Status...). Every drop down list will need a seperate lists that i need to get from the backend to populate.
That means that if i want to edit a customer with my form, i need to load:
The Customer Object which will be edited
A list of countries
A list of categories
A list of different stattus types
...
My question is:
Should each of these things be loaded seperatly with its own backend API call, or should i write a API backend call that will combine all these things into a single object and use it to load my data?
I think It's better use multiple API call in almost situation.
After compare pros and cons as table below, I always choose Multiple API call for projects.
Credit to Andrew Corrigan and Amrit remind me some criterias.
Single API
Multiple API
Network
Less request
Multiple request => Caching
UI Render
Render data in nearly same time
Render if any api response
Reuse FE component
Need to call big API to take one array data
Get what needed
Reusability API
Low
High
Single Responsibility
No
Yes
Flexible
No
Yes
Its an opinion based and scenario based , but in my suggestion i would prefer
things be loaded separately with its own backend API because :
1.Single api will be heavy and UX will be badly impacted
2.User may not change all field when form opens so only changing fields will be using api

How to use requests in Python 3 to fetch data from website that utilizes JavaScript and jQuery

I have been playing around with the requests library in Python 3 for quite some time now, and have decided to create a test program. For this program, I'm using the website https://ytmp3.cc/ as an example. But it turns out that a lot is going on, on the client-side it seems.
Some keys and other stuff are being generated, and I have been using Firefox's built-in network monitor, to figure out in which requests this is being made, but without luck.
As far as I know, the requests-library can't keep a "page" open and modify the DOM and content, by making more requests.
Anyone whom could take a look, and give a qualified guess on how the special keys are generated, and how I could possibly get these for my own requests.
Fx when loading the webpage, the first request made is for the root, and the response contains the webpage HTML. What I noticed is that at the bottom, there's an url containing some key and number.
<script id="cs" src="js/converter-1.0.js?o=7_1a-a&=_1519520467"></script>
id 7_1a-a
number _1519520467`
This is used for making the next request, but then a lot of following requests are being made, and some other keys are made as well. But I can't find where these come from since they are not returned by a request.
I know that when inserting a Youtube link, a request will be made to an url, as seen below.
https://d.ymcdn.cc/check.php?callback=jQuery33107639361236859977_1519520481166&v=eVD9j36Ke94&f=mp3&k=7_1a-a&_=1519520481168
This returns the following:
jQuery33107639361236859977_1519520481166({"sid":"21","hash":"2a6b2475b059101480f7f16f2dde67ac","title":"M\u00d8 - Kamikaze (Official Video)","ce":1,"error":""})
From this I can construct the download url, using the hash from above:
https://yyd.ymcdn.cc/ + 2a6b2475b059101480f7f16f2dde67ac (hash) + /eVD9j36Ke94 (youtube video id)
But how do I get
jQuery33107639361236859977_1519520481166&v=eVD9j36Ke94 and 1519520481168
Which I need to create the request?
You can probably save yourself and the operator of that website a lot of headache by just using youtube-dl, specifically with the --extract-audio --audio-format mp3 options. It's probably what that website itself uses.
youtube-dl is written in Python and can easily be used programatically.
If you insist on sending requests to that website for whatever reason, here's how I'd do it:
callback=jQuery33107639361236859977_1519520481166 specifies the name of the callback for the JSONP request. Any name you provide will be printed back out. For example, passing callback=foo will result in the following response:
foo({...})
You can omit it entirely and the server will serve just a JSON response in this case, which is nice.
_=1519520481168 is just to prevent the response being cached. It's randomly generated, just like the above parameter. The website checks for existence, however, so you have to at least pass something in.
The website, like many, checks for a valid Referer header.
Here's a minimal cURL command line to make a request to that website:
curl 'https://d.ymcdn.cc/check.php?v=eVD9j36Ke94&f=mp3&k=aZa4__&_=1' -H 'Referer: https://ytmp3.cc/'

Adding "deferred" members to the spring controller return

I have the following problem.
One of the endpoints for my spring-boot controller has to return a POJO with a couple of big BLOBs plus some metadata information about those BLOBs.
We need to be able to send first the metadata information (so that UI can display it without any delay), and then send the BLOBs, all part of the same RESTful request. If we send the metadata + BLOBs all in the same response, it would impact the performance and UI will seem sluggish.
I've been exploring the Future, DeferredResult etc, but I couldn't find how a JavaScript client can benefit from them.
Ideally, the response would contain the metadata plus some uniquely generated endpoints to fetch the BLOBs that controller would keep cached for the time-being, till they are requested.
I wonder what approach I can take here?
Add #JsonIgnore annotation on blob getter inside your entity class, to avoid serialization of this field to the response json. Then add separate controller method which will retrieve the same entity, but return back the contents of the blob only.

How can I chunk results (lazy load?) without rewriting my whole application (Laravel + jQuery, SPA style)

I've developed a web application with the concept of Single Page Application but none of the modern techs and frameworks.
So I have a jQuery page that dynamically requests data to localhost - a Laravel instance that compiles the entries in the DB (within a given time interval).
So the client wants to see all the entries for last week, the app works fine. But if he wants to see the results for the whole last month... well, they're so many that the default execution time of the php ins't enough to process all the data (30 seconds). I can easily override this, of course, but then the jQuery client will loop through these arrays of objects and do stuff with them (sort, find, sum...). So I'm not even sure jQuery can handle this many data.
So my question can be broken in two:
Can laravel ->paginate() be used so the ajax request of jQuery can also chunk the data? How does this work (hopefully in a manner that doesn't force me to rewrite all the code).
How could I store large amounts of information on the client? It's only temporary but the users will hang around for a considerable amount of time on my webpage, and I don't want them to wait 5 minutes every time they press a button
Thanks.
If you want to provide an interface to a large amount of data stored in a backed, you should paginate the data. This is a standard approach, so I'm sure your client will be ok with that.
Using pagination is pretty simple - see the docs for Laravel 5.0 here: http://laravel.com/docs/5.0/pagination
In order to paginate results in the backend, you need to call paginate($perPage) on your query instead of get() in your controller, like that:
$users = User::whereIsActive(true)->paginate(15);
This will return paginated result with 15 records per page. Page number will be taken from page parameter of the request. In order to get 3rd page of users, you'll need your frontend jQuery app to send a request to URL like:
/users?page=3
I don't recommend caching data in the frontend application. The data can be changed by some other user and you won't even know about it. And with pagination, your requests should be lightweight enough to stop worrying about a request sent to fetch every page of results.
Not sure if you're subscribed to laracasts but Jeffery Way is amazing in explaining features of Laravel and I highly recommend his videos.
In short you can paginate the results, then on the view when you call the foreach on your items you can array_chunk() the results to display them how you need to. But the paginated results are going to be fetched using a query in the URL, and i'm not sure that is what you want if you're already using a lot of jQuery to keep everything on the same page.
https://laracasts.com/lessons/crazy-simple-pagination
But assuming you're already paginating the results with whatever jQuery you've already written for the json data...
You could also use a query scope to get the data you need to for the amount of time to scope to create a simple api to use with ajax. I think that's probably what you're looking for.
So here's what I would do assuming you're already doing some pagination manually with your javascript.
Create a few query scopes to filter the data for different lengths of time
Create simple routes to fetch results from URI using the query scopes
Get the json data from the route preforming an ajax requests to the URIs created
More information on Query Scopes: http://laravel.com/docs/5.1/eloquent#query-scopes

how to form a DELETE REST URL for multiple items with angular ngresource

suppose my curl url for DELETE request looks like
website.com/res/resource_name?myid=(1,2,3,4)
How should I create an angular resource wherein I simply pass an array of IDs and get this rest URL?
note:
I am using restSQL, this is an example for deleting a single item
http://restsql.org/doc/ref/ResDeleteQuery.html
I can delete a single item, but I want to delete multiple at the same time. The above rest URL works for me via curl request.
This is not how REST works. One of the key principles of REST is that every resource has a unique URL. It implies you must perform a delete request for each resource. It's a drawback in this case, as it multiplies requests. But it allows caching response. And you read data much more frequently than you delete them ;)

Categories

Resources