I'm building an app using Ember and trying to talk to a back end API using Ember Data and writing custom adapters.
The API I am using is not RESTful or conform to JSONAPI standard. It has many endpoints that does not take in id but returns data for current user.
For instance I have 'getAccountData' api that returns account data of the current user. The returned data has "id" associated with it (user_id of the current user) but you don't pass in "id" when calling the api to get the data.
I tried implementing findRecord method in my custom adapter but "id" parameter is required and it complains if I just call this.get('store').findRecord('account-data').
I can pass in dummy id like this.get('store').findRecord('account-data', 1) but this seems wrong since the 'id' of returned data won't be 1.
Should I be using findAll or query instead? is there a way to do this in clean way?
You should use queryRecord. From the Ember docs:
This method makes a request for one record, where the id is not known
beforehand (if the id is known, use findRecord instead).
Related
When making an API endpoint in Django Rest Framework for example, why would I ever use URL parameters to receive data rather than just putting everything in the request data?
I don't get the difference between the two.
Putting some query data in the URL allows the URL to store the "state" of your web application.
For example a state can be "I queried how do I make cheese on stackoverflow", and the URL would be https://stackoverflow.com/search?q=how+do+make+cheese.
This allows the web app to interact as expected with browser tools like Refresh, Go Back, etc. Without the state stored in the URL, refreshing the page might just take you back to the homepage, instead of showing you the same query results (the expected behaviour).
Additionally, you can copy & paste the URL. When someone clicks on it, they will be taken directly to that specific state.
On the other hand, you shouldn't use the URL to store/send sensitive data (as it can easily be seen, use the body instead), and you should make sure reloading an "action" URL won't execute the action again (like paying for a product twice!).
The URL parameters and body parameters server different purpose. The REST API grammar says
GET Method is used when you want to retrieve data back and don't want to update any of the record in system. The GET method will not pass body parameter and hence whatever filter parameters passed to API will be through URL parameters.
POST/PUT Method is used whenever you want to update your database. The value could be single parameter or even no input but you have to use POST/PUT method, if you are trying to update database record(s).
I am using ember member API actions and was wondering how can response be normalized in such cases.
I have a model "users" with member action "postAddress". It is a post request which returns me a response. Now the problem I am facing is data is not normalized as returned data does not map to any store model. I know when we do a findAll and give a model, then ember automatically normalizes the data returned from API call. But in case of member actions, can anyone suggest how can data be normalized? (snake case to camel case).
ember-api-actions addon does not provide any serializer integration. There is an open issue about it. As discussed there you could manually push the response to store using pushPayload method of store service provided by Ember Data.
If you don't want to push the data into the store but just use a Serializer to normalize the response, you could do so by combining serializerFor() method of DS.Store and normalizeResponse() method of DS.Serializer.
Whenever I call this.store.find('campaign', url), if the entry is not found in store it makes a request to API and creates a record in that store with ID being url. I need to be able to make sure that the record that is being pushed to the store uses id from the payload that is returned by API and not url that I pass in to find() function. What's the best way to go about it?
Override extractId() of your serializer, whether it's a RESTSerializer or JSONSerializer.
My understanding is that if a given route in an Ember app needs to grab data from the server to feed its model, we will either use Ember data with a RESTful API or just an AJAX request, and the request for model data will come from the Ember app to the server.
But what if I want the server itself, at the end of a server-side process, to ask the Ember app to display one of its routes by passing to it some data to be used as the route model?
Basically, a process on the Node-based server (with Express) ends up like so:
function(req,res) {
res.redirect("/#/someEmberAppRoute");
}
The req parameter carries some object that I want to pass to the Ember route in order to be used as a model for that route.
The way I ended up solving this problem was to store on the server the output of the server-side process and have the Ember route someEmberAppRoute's model send a GET request to the server to grab that output and feed its model, instead of trying to push it directly from the server to the client.
I'm implementing a simple API using a Django back-end and want to support access control using API keys.
As is the standard, the protocol will use a combination of a private key and a public identifier to authenticate the request. The public identifier, data and hash(data+private key) will be sent to the server. The server will then duplicate the hashing function using the private key stored in the database and if the hashes match then the request is authenticated.
My question is regarding the 'data' portion. Of course, the client and the server need to agree on what 'data' is, otherwise the hashes won't match. The protocol can simply dictate that the 'data' is the raw query string as sent by the browser. In that case, how do I retrieve the raw query-string from the Django view (it parses it in a QueryDict thus losing the sequence information)?
Or are there any other best practices that I am missing?
You can always get the raw query data via request.body (or request.raw_post_data in versions before 1.4).