HasBadge with JavaScript
This is a minimal example of calling the HasBadge API via HTML and Javascript. Remember to never use your Secret Keys on the client side.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<style type='text/css'>
body {
font-family: "Arial";
}
.rctable{
padding-top:20px;
padding-bottom:10px;
border-bottom:solid 1px #ccc;
width:600px;
}
.rclabel {
font-size:14px;
font-weight:bold;
}
.rcval{
font-size:14px;
padding-left:10px;
}
</style>
<script type="text/javascript">
var connecterURL = "https://www.redcritterconnecter.com/";
function hasBadge(secretKey, profileEntity, badgeName, cbSuccess, cbFail) {
$.getJSON(connecterURL + "services/gamificationv1/HasBadge?jsoncallback=?", { secretKey: secretKey, profileEntity:profileEntity, badgeName:badgeName},
function (data) {
if (data.Result) {
cbSuccess(data);
} else {
cbFail(data);
}
});
}
//My Success Callback
function onMyhasBadgeSuccessHandler(data) {
//data is JSON response
renderItems(data)
}
//My Failure Callback
function onMyhasBadgeFailHandler(data) {
//Something went wrong
alert("Something went wrong");
}
//Sample function to generate the list output in html
function renderItems(data) {
var h = "";
var h = "<table class='rctable'>";
h += "<tr><td class='rclabel'>Has Badge???? : </td><td class='rcval'>" + HTMLEncode(data.HasBadge) + "</td></tr>"
h += "</table>";
$("#results").html(h);
//display the results
$("#results").html(h);
}
function HTMLEncode(str) {
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
</script>
</head>
<body>
<table>
<tr>
<td>Secret Key</td>
<td>
<input type="text" id="tSecretKey" style="width: 500px" />
</td>
</tr>
<tr>
<td>Profile Entity</td>
<td>
<input type="text" id="tProfileEntity" style="width: 500px" />
</td>
</tr>
<tr>
<td>Badge Name</td>
<td>
<input type="text" id="tBadgeName" style="width: 500px" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" onclick="hasBadge($('#tSecretKey').val(), $('#tProfileEntity').val(), $('#tBadgeName').val(), onMyhasBadgeSuccessHandler, onMyhasBadgeFailHandler)" value="User Has Badge?" /></td>
</tr>
</table>
<br />
<div id="results"></div>
</body>
</html>
HasBadge with C#
This is a minmal example of calling the HasBadge API and parsing the JSON result into a populated C# object. This example uses asynchronous techniques to raise a callback when the response is received.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web; //Add reference to System.Web
using System.Runtime.Serialization.Json; //Add reference to System.Runtime.Serialization
namespace RedCritterConnecter.Samples
{
//Create a class to contain the response
public class HasBadgeResponse
{
public Boolean Result { get; set; }
public String HasBadge { get; set; }
}
public class HasBadge
{
public delegate void OnHasBadgeResponse(HasBadgeResponse HasBadgeResponse);
public event OnHasBadgeResponse onHasBadgeResponse;
public delegate void OnHasBadgeResponseError(HasBadgeResponse HasBadgeResponse);
public event OnHasBadgeResponseError onHasBadgeResponseError;
const string CONST_ConnecterBaseURL = "https://www.redcritterconnecter.com/";
public void Execute(String SecretKey, String ProfileEntity, String BadgeName )
{
try
{
//Create url encoded parameters in query string
String queryString = "secretkey=" + System.Web.HttpUtility.UrlEncode(SecretKey) + "&profileentity=" + System.Web.HttpUtility.UrlEncode(ProfileEntity) + "&badgename=" + System.Web.HttpUtility.UrlEncode(BadgeName);
//Create a new instance of a WebClient
WebClient wc = new System.Net.WebClient();
//Prevent this request from caching in order to ensure that it is sent to server
wc.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
//Attach an event handler to receive the response
wc.DownloadStringCompleted += onHasBadgeResponseReceived;
//Make the call
wc.DownloadStringAsync(new Uri(CONST_ConnecterBaseURL + "services/gamificationv1/HasBadge?" + queryString, UriKind.Absolute));
}
catch
{
//Something went wrong communicating with the server
if (onHasBadgeResponseError != null)
{
onHasBadgeResponseError(null);
}
}
}
private void onHasBadgeResponseReceived(object sender, DownloadStringCompletedEventArgs e)
{
try
{
//Create a JSON serializer
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(HasBadgeResponse));
//Copy the string into a memory stream
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
//Read the stream into an object matching the response's type
HasBadgeResponse HasBadgeResponse = (HasBadgeResponse)s.ReadObject(ms);
//Work with the populated response object
if (HasBadgeResponse.Result == true)
{
//Request was accepted, raise the success event
onHasBadgeResponse(HasBadgeResponse);
}
else
{
//Something went wrong
if (onHasBadgeResponseError != null)
{
//Request failed, raise the error event
onHasBadgeResponseError(HasBadgeResponse);
}
}
}
catch
{
//Something went wrong
onHasBadgeResponseError(null);
}
}
}
}