Server to ask Ember app to display one of its route while passing model data - ember.js

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.

Related

Consume an API in Django REST, server side, and serve it ,client side, in Angular

I have an Angular app that consume API client side. Since it's bad practice to do so (I don't want to expose my API credentials), I decided to split into backend/ frontend before myapp get to big.
I succeed to implement my Angular into a Django REST framework app and everything is working fine.
But now I need to change my API logic and have DRF consume the external API
I had:
Angular <---API---> ext data
Now I have:
Django/ Angular <---API---> ext data
What I need:
Angular <---API---> Django <---API---> ext data
But I am very confused about how to accomplish it. I have experience with setting up API endpoints in DRF, but only for data from models within DRF. I know as well how to consume API in Django.
But how can I chain two API calls?
Do I have to create a model to store the queries and then the response?
Do I need to write serializers even if the returned data is json all the way?
How my frontend will now that the data from the external API is available?
What I need is someone to explain me what is the pattern for this task.
Say you have a FBV mapped to an URL in django like this:
url: /api/animals/<str:key>
#add_decorators_as_needed
def animals_view(request, key=None):
data = requests.get(f'{API_URL}/{key}?api_key={API_KEY}') # grab data from ext provider
json_data = ... # convert to json or do other manipulations
return json_data # return data to the frontend
Then in the frontend, your Angular app, you can execute a get request to this /api/animals/cow url of your django app and retrieve the data for a cow from the external provider without exposing your API_KEY.
So the flow would be like this:
Angular requests data from Django, Django gets that data from the external provider, does some data processing if required and then returns that data to Angular. You do not have to store anything in the database (but you can of course do it or for example log things, it's optional).

In the backend, can you access data from previous requests?

This is more of a theory question, so I'm not going to post any code.
On the frontend, the user types in a search command. On the backend (Django in my case), it hits an API, the results of the search are saved into a Django View in views.py. On the frontend, the user interacts with this returned data and sends another request. On the backend, is the data from the first Django View still available for use? How do you access it?
(The data is also in the frontend and I can send it with the second request. But if it's still stored on the backend then I wouldn't need to.)
HTTP by it's own nature is a stateless protocol. It does mean that protocol doesn't know what or when should happen any request. Request comes and your API just reacts to this request by your implemented logic.
If you want to persist/save any state/data on your API side, you can do it by persisting them to database or saving to any local/global variable. Then you can access this saved state/data while recieving other requests to your back-end and implement the logic to use of previous state with the new incoming data.

setting up django server for receiving callbacks from jira and other apis

how can I start receiving and parsing callback api's responds in further with Django server?
I wanna setup my DRF server to start working with JIRA webhooks, but also it might be useful for other apis such as telegram and etc. In this case I need to provide them my server's url where I would expect new events/data, but atm I don't realise what it means exactly. Where do I need to start digging in?
Not sure about the apis you are talking about but if the api sends some callbacks you'll give them a url, mysite.com/whatever
make your url config like normal
and in your view you can parse the result
def my_callback_view(request):
# assuming it's a POST request
data = request.POST
... do whatever with the data

Ember Mirage Fake API test with Postman

I have my mirage setup which returns data on my defined models, for example if I call /api/users: it returns me all the fake users I need. If I take the same call and test it on Postman, it returns nothing? I thought mirage is acting like a fake API end point server and testing it with Postman will work. Am I missing anything here?
Mirage is only intercepting AJAX and fetch requests in the current browser window. It's using pretender library therefor, which replaces the native XMLHttpRequest object to achieve that one. This is not affecting any other browser window or addon. Therefore you can't query the mock API using Postman or any other client outside of current window. For the same reason you can't debug the requests in network tab of developer tools. However Mirage could be configured to log intercepted requests to console.

Salesforce: SOAP Login from Salesforce TO Salesforce

I implemented a batch job which makes a webservice call within the same salesforce instance, which then is supposed to send emails with a pdf attachment,
since you cannot send pdf attachments directly from a batch job. My webservice call looks like this:
public static void callOut(List ids){
InvoiceAttachmentConnector.InvoiceAttachmentService ws = new InvoiceAttachmentConnector.InvoiceAttachmentService();
ws.SessionHeader = new InvoiceAttachmentConnector.SessionHeader_element();
ws.SessionHeader.sessionId = UserInfo.getSessionId();
ws.handleInvoicePdfAttachment(ids);
}
However in batch jobs UserInfo.getSessionId() returns null, therefore i get a INVALID_SESSION_ID exception.
How can i log in to get a SessionId? So far I found no solution to login from salesforce to salesforce. If u can help I would appreciate it! Thanks!
You cannot get a session Id like this in batch apex as it runs under the system context and so has no specific user info for retrieval.
UPDATE:
You have the following options:
Try running the web services wsdl from your Salesforce org through the wsdl to apex generator in your org to generate some classes that may allow you to login. You are only allowed one web service request per execute call.
You could create a sites page that you make a HTTP get request to in your batch apex. This needs to retrieve the Ids of the items you want to send the PDFs for and a particular user to run as for you to use the System.runAs(user) method. You could pass these parameters in the HTTPRequest header or in a custom setting.
Note that neither of these solutions are ideal, you may want to reconsider why you are using Batch apex first of all and see whether you could reimplement it in a different way.