Currently in the process of developing a language translating messaging application using Node and Angular. I have decided to utilize the Yandex API since Google Translate is not free. You can find more information about the API at www.yandex.com
I am uncertain if my approach is correct as this is the first time I am integrating APIs into my project.
My implementation involves a simple FORM
<div ng-controller="messageController">
<h4>Send Message</h4>
<form>
<textarea name="message" ng-model='message.msg'></textarea>
<input type="submit" value="Send" ng-click='sendMessage()' class="btn btn-info">
</form>
</div>
This form sends data to the messageController
myapp.controller('messageController', function($scope, messageFactory, $location, $rootScope) {
$scope.sendMessage = function(){
console.log("Inside sendMessage()" + $scope.message.msg)
messageFactory.translateMessage($scope.message.msg, function (data){
console.log(data);
})
}
})
Following that, there is a Factory responsible for making the API call with an assigned api key.
myapp.factory('messageFactory', function($http, $location) {
var factory = {};
factory.translateMessage = function(info, callback) {
console.log("Inside Message Factory-Traslate", info)
$http.get('https://translate.yandex.net/api/v1.5/tr.json/translate?key=<ENTER_KEY_HERE>&text='<info>'&lang=<ga>&[format=<plain>]').success(function (output){
callback(output);
})
}
return factory;
});
Note: The actual API key has been omitted here for security reasons.
You can refer to this link for detailed information .
Despite following this guide, I suspect there may be a better way to make the API call.
What is the proper method to send the API request and handle the response?
Thank you in advance.