HasHadBadge with JavaScript
This is a minimal example of calling the HasHadBadge 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 hasHadBadge(secretKey, profileEntity, badgeName, cbSuccess, cbFail) {
$.getJSON(connecterURL + "services/gamificationv1/HasHadBadge?jsoncallback=?", { secretKey: secretKey, profileEntity:profileEntity, badgeName:badgeName},
function (data) {
if (data.Result) {
cbSuccess(data);
} else {
cbFail(data);
}
});
}
//My Success Callback
function onMyhasHadBadgeSuccessHandler(data) {
//data is JSON response
renderItems(data)
}
//My Failure Callback
function onMyhasHadBadgeFailHandler(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 ever Had Badge???? : </td><td class='rcval'>" + HTMLEncode(data.HasHadBadge) + "</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="hasHadBadge($('#tSecretKey').val(), $('#tProfileEntity').val(), $('#tBadgeName').val(), onMyhasHadBadgeSuccessHandler, onMyhasHadBadgeFailHandler)" value="User Has ever had Badge?" /></td>
</tr>
</table>
<br />
<div id="results"></div>
</body>
</html>
HasHadBadge with C#
This is a minmal example of calling the HasHadBadge 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 HasHadBadgeResponse
{
public Boolean Result { get; set; }
public String HasHadBadge { get; set; }
}
public class HasHadBadge
{
public delegate void OnHasHadBadgeResponse(HasHadBadgeResponse HasHadBadgeResponse);
public event OnHasHadBadgeResponse onHasHadBadgeResponse;
public delegate void OnHasHadBadgeResponseError(HasHadBadgeResponse HasHadBadgeResponse);
public event OnHasHadBadgeResponseError onHasHadBadgeResponseError;
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 += onHasHadBadgeResponseReceived;
//Make the call
wc.DownloadStringAsync(new Uri(CONST_ConnecterBaseURL + "services/gamificationv1/HasHadBadge?" + queryString, UriKind.Absolute));
}
catch
{
//Something went wrong communicating with the server
if (onHasHadBadgeResponseError != null)
{
onHasHadBadgeResponseError(null);
}
}
}
private void onHasHadBadgeResponseReceived(object sender, DownloadStringCompletedEventArgs e)
{
try
{
//Create a JSON serializer
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(HasHadBadgeResponse));
//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
HasHadBadgeResponse HasHadBadgeResponse = (HasHadBadgeResponse)s.ReadObject(ms);
//Work with the populated response object
if (HasHadBadgeResponse.Result == true)
{
//Request was accepted, raise the success event
onHasHadBadgeResponse(HasHadBadgeResponse);
}
else
{
//Something went wrong
if (onHasHadBadgeResponseError != null)
{
//Request failed, raise the error event
onHasHadBadgeResponseError(HasHadBadgeResponse);
}
}
}
catch
{
//Something went wrong
onHasHadBadgeResponseError(null);
}
}
}
}