How to integrate Mashape APIs with Parse as Cloud Modules @ParseIt

Parse Cloud Module

Photo credit: Parse

Parse is a backend-as-a-service (BAAS) platform that lets you focus on building your applications without worrying about server and infrastructure maintenance and complexities.  If you haven’t heard or tried Parse before, I encourage you to try their Quickstart here to get an idea of how the BAAS paradigm works.

This post shows an example of a Mashape API integrated with Parse through Parse Modules.  Parse Modules are libraries that Parse developers import into their app to use certain functionality offered by 3rd-party APIs.  Once your API is turned into a Parse Module, it will be easier for developers to use it in their applications.

There’s a whole tutorial from the Parse documentation that shows how to turn your APIs into Cloud Modules, so we won’t go through those steps.  Instead we will show a finished working code here, that you can use to pattern your Modules after.  (It is however essential to have an understanding of how Cloud Code and Cloud Modules work, so reading them first would be great).

Integrate Mashape APIs with Parse!

This is a very simple representation of how each component interacts with one  another.  (Although we are using HTML and Javascript for our app, you can access the Cloud Code from any language supported by Parse).  For this project, we will integrate the Yoda Speak API to Parse as a Cloud Module.

Integrate Mashape APIs to Parse as Cloud Module

Integrate Mashape APIs to Parse as Cloud Module

Step 1)  Application (index.html) initializes the Parse Javascript SDK and calls our Cloud Code “yodaSpeakFunction”:

<!doctype html>
<head>
  <title>My Parse App</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.12.min.js"></script>
</head>
 
<body>
 
  <script type="text/javascript">
  
    // After installing the Parse SDK above, intialize it with your Parse application ID and key below
    Parse.initialize("<Parse application ID>", "<Parse key>");
    
    // Call the Cloud Code 'yodaSpeakFunction'
    Parse.Cloud.run('yodaSpeakFunction', { sentence: "Hi there my name is Chris." }, {
       success: function(result) {
         
         // Show result as an alert
          alert(result);
       },
       error: function(error) {
       }
       
    });     
  </script>
 
</body>
</html>

Source: https://gist.github.com/ismaelc/7241875

Step 2) Define the Cloud Code “yodaSpeakFunction” (main.js in the /cloud folder) and initialize/call the Cloud Module “yodaSpeak”:

// Cloud Code 'yodaSpeakFunction'
 
// Import Cloud Module from /cloud folder
// Initialize Cloud Code with your Mashape key (http://mashape.com/keys)
var yodaClient = require('cloud/yoda-1.0.0.js');
yodaClient.initialize('<Enter your Mashape key here>');
 
Parse.Cloud.define("yodaSpeakFunction", function(request, response) {
 
// Call 'yodaSpeak' Cloud Module passing in 'sentence' parameter
// Please refer to https://www.mashape.com/ismaelc/yoda-speak for parameter details
   yodaClient.yodaSpeak({
    	sentence: request.params.sentence
	}, 
	{
		success: function(httpResponse) {
    			console.log(httpResponse);
			response.success(httpResponse.text);
		},
  		error: function(httpResponse) {
    			console.error(httpResponse);
    			response.error("Uh oh, something went wrong");
    		}
    	});
});

Source: https://gist.github.com/ismaelc/7242075
Step 3) The Cloud Module “yodaSpeak” (yoda-1.0.0.js in the /cloud folder) where the Mashape API is called:

// Cloud Module yoda-1.0.0.js
 
// Set url of Mashape API to call
var url = 'https://yoda.p.mashape.com/yoda';
var key = '';
 
module.exports = {
 
// Method to initialize module
  initialize: function(mashapeKey) {
    key = mashapeKey;
    return this;
  },
 
// 'yodaSpeak' method to make an HTTP request call to Mashape API
// Uses Javascript Promises ('then') to execute success or error callback through 'options'
  yodaSpeak: function(params, options) {
  return Parse.Cloud.httpRequest({
    url:  url,
    headers: {
       'X-Mashape-Authorization': key
    },
    params: params,
    }).then(function(httpResponse) {
      if (options && options.success) {
        options.success(httpResponse);
      }
    }, function(httpResponse) {
      if (options && options.error) {
        options.error(httpResponse);
      }
    });
  }
}

Source: https://gist.github.com/ismaelc/7242220

Our Parse Cloud Module ‘yoda-1.0.0.js‘ is now finished! You can drop it in your own Parse Cloud Code and start using it.

What’s next?

There’s a ton more Mashape APIs there to expose as Parse libraries.  Let me know if you’ve turned your API to a Parse Cloud Module so we can gather them here and promote them to other developers! – chris@mashape.com

What are you waiting for? CONSUME API NOW