Last active
May 1, 2019 10:08
-
-
Save DmitriySmirnov/0beda14e93bed7b5dc2f to your computer and use it in GitHub Desktop.
Access public API from JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
YOU HAVE TO BE LOGGED IN IN ORDER TO USE THIS PAGE. IF YOU WANT TO TRY THIS FUNCTIONALITY ON YOUR OWN SITE, PLEASE USE <A HREF="https://gist.github.com/DmitriySmirnov/6902ba719fc17b9467c2f8633831dba3">THIS CODE SAMPLE</A> | |
</div> | |
<h3>How to get current user info?</h3> | |
<div> | |
<a id="whoAmI" href="#">Who am I?</a> | |
</div> | |
<h3>How to create new contact (make any POST request)?</h3> | |
<div> | |
email: <input type="textbox" id="emailEntry"></input> | |
<a id="createContact" href="#">Create new contact</a> | |
</div> | |
<h3>How to update existing contact (make any PUT request)?</h3> | |
<div> | |
contact Id: <input type="textbox" id="contactIdEntry"></input> | |
new phone: <input type="textbox" id="phoneEntry"></input> | |
<a id="updatePhone" href="#">Update phone</a> | |
</div> | |
<h3>How to list all members by keyword</h3> | |
<div> | |
search for: <input type="textbox" id="simpleSearchText"></input> | |
<a id="listMembersButton" href="#">List members</a> | |
<table id="membersTable" style="background:#dddddd;margin:20px;border:#ccc 1px solid;width:90%"> | |
<thead> | |
<tr style="padding:21px 25px 22px 25px;border-top:1px solid #fafafa;border-bottom:1px solid #e0e0e0;background: #aabbcc;"> | |
<td width="100">ID</td> | |
<td>Display name</td> | |
<td>Level</td> | |
</tr> | |
</thead> | |
<tbody> | |
</tbody> | |
</table> | |
</div> | |
<script type="text/javascript" src="https://rawgit.com/WildApricot/ApiSamples/master/JavaScript/waPublicApi.js"></script> | |
<script type="text/javascript"> | |
var clientId = "o86qi1vtcq"; | |
var api = new WApublicApi(clientId); | |
$(document).ready(function(){ | |
$.when(api.init()).done(initButtons); | |
}); | |
function initButtons() | |
{ | |
$("#whoAmI").click(function(){ | |
api.apiRequest({ | |
apiUrl: api.apiUrls.me(), | |
success: function (data, textStatus, jqXhr) { | |
$("#contactIdEntry").val(data.Id); | |
alert("Hello " + data.FirstName + " " + data.LastName + " !\r\nSpirits say that your ID is '" + data.Id + "' and your email is '" + data.Email + "'."); } | |
}); | |
return false; | |
}); | |
$("#emailEntry").val("user" + Math.floor(Date.now() / 1000) + "@invaliddomain.com"); | |
$("#createContact").click(function(){ | |
api.apiRequest({ | |
apiUrl: api.apiUrls.contacts(), | |
method: "POST", | |
data: { email: $("#emailEntry").val() }, | |
success: function(data, textStatus, jqXhr){ | |
$("#contactIdEntry").val(data.Id); | |
alert("New contact #" + data.Id + " with email " + data.Email + "was successfully created"); | |
}, | |
error: function(data, textStatus, jqXhr){ | |
alert("Failed to create new contact. See console for details " + data.responseText); | |
} | |
}); | |
return false; | |
}); | |
$("#updatePhone").click(function(){ | |
var contactId = $("#contactIdEntry").val(); | |
var phone = $("#phoneEntry").val(); | |
api.apiRequest({ | |
apiUrl: api.apiUrls.contact(contactId), | |
method: "PUT", | |
data: { id: contactId, fieldValues: [ { fieldName:"Phone", value: phone } ] }, | |
success: function(data, textStatus, jqXhr){ | |
alert("Contact phone was successfully updated."); | |
}, | |
error: function(data, textStatus, jqXhr){ | |
alert("Failed to update contact. See console for details " + data.responseText); | |
} | |
}); | |
return false; | |
}); | |
$("#listMembersButton").click(function(){ | |
$('#membersTable tbody').empty(); | |
$('#membersTable tbody').append('<tr><td colspan="3">Loading, please wait...</td></tr>'); | |
api.apiRequest({ | |
apiUrl: api.apiUrls.contacts({ | |
simpleQuery: $("#simpleSearchText").val(), | |
$filter: "'Membership status' ne 'Lapsed'", | |
$top: 500, | |
$select: "'First Name','Last Name'" | |
}), | |
method: "GET", | |
success: function(data, textStatus, jqXhr){ | |
$('#membersTable tbody').empty(); | |
for( i=0; i< data.Contacts.length; i++){ | |
var contact = data.Contacts[i]; | |
$('#membersTable tbody').append( | |
'<tr><td>' + contact.Id + '</td><td>' + contact.DisplayName + '</td><td>' + contact.MembershipLevel.Name + '</td></tr>'); | |
} | |
}, | |
error: function(data, textStatus, jqXhr){ | |
alert("Failed to get a list of members. See console for details " + data.responseText); | |
} | |
}); | |
return false; | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello
Non-administrative can not make use of this page, am I right?