Api Platform with Swift Mailer - api

I would know if API Platform is reliable to Swift Mailer. In fact, I woul use Swift Mailer to send a email when a new task has add. It works good when I use my controller, but not working with Postman or Swagger UI.
//mail sending
$message = (new \Swift_Message('New task has done !'))
->setContentType('text/html')
->setFrom("fromAdresse#sample.fr")
->setTo('sample#email.com')
->setBody(
$this->renderView('email/new.html.twig',
['work' => $work]));
$mailer->send($message);
I would use the same thing when users pass by the API UI, it's possible to handle the request ? Because controller and API hasn't the same route.
API : /api/works (POST) to add an element
Controller : work/new (POST) to add an element too
How I can use Swift Mailer on the API platform Request ? Or How I can handle the request with custom controller ?
Thx for advice and answers, I'm beginer with both.

The best way is using an Entity listener (see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html) - you can detect create/update/delete actions there and do other actions like send e-mails. It is also better to have message logic at one place for all controller/api/other requests :-)
Just be careful to select the right Doctrine event. And read details about modifying entities inside these listeners (i.e. to store "email sent" flag or any other values to other entities. It is not straightforward :-) ).

Related

Consuming an API from a Salesforce Commerce Cloud controller

Inside a SFCC custom controller, at a certain point I need to consume an API and wait for its response before continuing. In fact, the my controller's response depends on the API's response itself.
Unless I'm mistaken, the SFCC documentation doesn't provide any code sample about that.
Express apparently provides a solution (see the very beginning of https://zellwk.com/blog/async-await-express/ for example), but SFCC middleware doesn't seem to handle the async keyword when defining a request handler.
Can anybody help on this one?
Thanks
There is no way to handle async code on SFCC natively.
And you need to change your code's structure according to this restriction.
But you can use some jobs as a workaround.
Please check this link for more information about this concept:
https://medium.com/#osapishchuk/asynchronous-execution-within-sfcc-9cc6226e1ac6

Communication between Podio and WebService using hook

we need to do a communication between Podio and a WebService, and we suppose that the solution is to use hooks.
We have a WebService with one function "createInvoice" with some parameters, and we need to invoke this when we create an item on Podio.
We have read the Podio documentantion but we don't find the manner to do this.
http://podio.github.io/podio-dotnet/webhooks/
As the above link suggests, is it necessary to use a ".ashx" class (Handler) to receive the item from Podio and send the correct information to the WebService or exists another solution?
Thanks!
Its not necessary to be an ashx file.
You just need to have a public URL that expects POST parameters from Podio.
As given in the example code, you need to check the "type" parameter and verify the webhook to enable it.

How to specify request methods on actions in struts1?

I have some actions in a Struts(1) application. The actions currently are open to both GET/ POST Methods. We want to restrict the visibility of individual Actions to GET/ POST requests. Where do we specify that? Is it done in struts-config.xml
No, it isn't.
The two easiest options for Struts 1 apps are:
Make a custom request processor that introspects actions (or calls a method, or...) on the request and check if the request type is allowed.
Use a base action class that calls subclass GET- and POST-specific methods based on the request type.

Can Webapi be used in an application which is not excessed by any external application?

I'd read it somewhere that whenever one needs to do data intensive work then Webapi could be used. Ex: autocomplete textbox where we get data from using ajax on key press.
Now someone told me that Webapi shouldn't be used within applications which are not externally accessed. Rather action should be used to the same work as it is capable of returning the data back in a similar fashion to webapi.
I'd like to know your suggestions over it.
Depends on how you look at it. If all you need is ajax-ification of your controller actions, then you really don't need Web-API. Your actions can return a JsonResult and it is very easy to consume that from your client side through an AJAX call.
Web-API makes it easy for you to expose you actions to external clients. It supports HTTP protocol and Json and XML payloads automatically, out of the box, without you writing the code for it. Now, there is nothing preventing you from consuming the same Web-API actions from your own internal clients in an AJAX manner.
So the answer to your question depends on your design. If you don't have external clients, then there is no string need for you to have Web-API. Your standard controller actions can do the job.

What is an efficient way to create/manage RESTful API with grails?

I've built my first grails application. My URL mappings are what the default application provides:
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
Senario
I have a controller called ColorController with actions save and list. It simply does something like this:
def save () {
def colorInstance = new Color(params)
colorInstance.save(flush: true)
}
def list () {
[colorList: Color.list, colorTotal: Color.count()]
}
I would like to build a simple API for these actions.
The save action should accept parameters as JSON and provide a successful message if the records save.
The list action should provide the list as JSON
Questions
Should I make a separate URL mapping for api? (e.g. http://<domain>/<app>/rest/controller/action)
Should I be making a separate controller for my API's
I am using spring security plugin for authentication. But at some point I might want to authenticate the restful api as well. What are some solutions for that?
If I use the same controller, how can I modify these simple actions to do what I need.
Before even looking below for my opinion/answers I would suggest to visit this SO Question for the basic understanding of RESTful WS in Grails.
Opinions:
"The save action should accept parameters as JSON and provide a successful message if the records save" - Save is mapped to POST RESTful. Instead of binding a JSON body to params it is bound to the request. In order to access the JSON object you just need to use request.JSON in the action method.
request.JSON instanceof JSONObject
"The list action should provide the list as JSON" - list() action is mapped to a GET Request and you can render the map as JSON in the list() as below
//Controller list()
import grails.converter.JSON
def list () {
[colorList: Color.list, colorTotal: Color.count()] as JSON
}
Answers to Questions:-
Should I make a separate URL mapping for api?
Abiding by the basics of REST, the client should only access the resource (Color in this case) and should not bother about the underlying controller or action. The server side logic should be abstracted from the client. URL Mapping is what the client would use to as form of request. I would have something like this in my url mapping for Color Resource.
/color/$id?(resource: "color")
or
/color/$id?(controller: 'color'){
action = [GET: "list", POST: "save"]
}
Should I be making a separate controller for my API's? - Depends on the way the App is designed. You also can have the above controller as the API. For example, currently I am working on a grails app which used AngularJS in the front End which connects to the Grails APP RESTFully. In order to achieve I had a RestClientController which works as an API to Angular. The rationale behind having a REST api in the same app is that in future we can expose the underlying service to external clients other than the Angular client present in the app itself.
I am using spring security plugin for authentication. But at some point I might want to authenticate the restful api as well. What are some solutions for that? - You can use Spring Security here as well. In my case I am using the plugin and I secure the controller by using the plugin's annotated component #Secured. I have custom OAuth enabled as well for authorization which interacts to the company wide LDAP and AD Groups.
If I use the same controller, how can I modify these simple actions to do what I need. - I think you would have got the answer to this question by now (after going through the SO question I mentioned above). Here is my opinion, controller actions can route to appropriate service classes which does the business implementations based on the request parameters.
For example,
//Action
def show(){
if(params.id){
colorService.getColor()
} else {
colorService.searchColor()
}
}
In the above example, the url mapping would be /color/123 or /color. In the former case, it will get the color and in the later it will search the colors