Sample code for 30+ languages & platforms
Xbase++

Populi Get Roles

See more Populi Examples

Demonstrates the Populi getRoles task.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oXml
LOCAL cAccessKey
LOCAL oRest
LOCAL nBAutoReconnect
LOCAL cResponseBody
LOCAL i
LOCAL nCount_i
LOCAL nId
LOCAL cName
LOCAL nInactive

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  First load the previously obtained API token.
//  See Get Populi Access Token for sample code showing how to get the API token.
oXml := CreateObject("Chilkat.Xml")
nSuccess := oXml:LoadXmlFile("qa_data/tokens/populi_token.xml")
cAccessKey := oXml:GetChildContent("access_key")
IF (oXml:LastMethodSuccess != 1)
    ? "Did not find the access_key"
    oXml:destroy()
    RETURN
ENDIF

oRest := CreateObject("Chilkat.Rest")

//  Connect using TLS.
//  A single REST object, once connected, can be used for many Populi REST API calls.
//  The auto-reconnect indicates that if the already-established HTTPS connection is closed,
//  then it will be automatically re-established as needed.
nBAutoReconnect := 1
nSuccess := oRest:Connect("yourcollege.populi.co", 443, 1, nBAutoReconnect)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oXml:destroy()
    oRest:destroy()
    RETURN
ENDIF

oRest:Authorization := cAccessKey

oRest:AddQueryParam("task", "getRoles")

cResponseBody := oRest:FullRequestFormUrlEncoded("POST", "/api/index.php")
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oXml:destroy()
    oRest:destroy()
    RETURN
ENDIF

//  We should expect a 200 response if successful.
IF (oRest:ResponseStatusCode != 200)
    ? "Request Header: "
    ? oRest:LastRequestHeader
    ? "----"
    ? "Response StatusCode = " + Str(oRest:ResponseStatusCode)
    ? "Response StatusLine: " + oRest:ResponseStatusText
    ? "Response Header:"
    ? oRest:ResponseHeader
    ? "Response Body:"
    ? cResponseBody
    oXml:destroy()
    oRest:destroy()
    RETURN
ENDIF

oXml:LoadXml(cResponseBody)
? oXml:GetXml()

//  Sample response:
//  Use this online tool to generate parsing code from sample XML: 
//  Generate Parsing Code from XML

//  <?xml version="1.0" encoding="ISO-8859-1"?>
//  <response>
//      <role>
//          <id>7</id>
//          <name>Academic Admin</name>
//          <inactive>0</inactive>
//      </role>
//      <role>
//          <id>19</id>
//          <name>Financial Aid</name>
//          <inactive>0</inactive>
//      </role>
//      <role>
//          <id>15</id>
//          <name>Registrar</name>
//          <inactive>0</inactive>
//      </role>
//      <role>
//          <id>4</id>
//          <name>Staff</name>
//          <inactive>0</inactive>
//      </role>
//      <role>
//          <id>1</id>
//          <name>Everyone</name>
//          <inactive>0</inactive>
//      </role>
//  </response>

i := 0
nCount_i := oXml:NumChildrenHavingTag("role")
DO WHILE i < nCount_i
    oXml:I := i
    nId := oXml:GetChildIntValue("role[i]|id")
    cName := oXml:GetChildContent("role[i]|name")
    nInactive := oXml:GetChildIntValue("role[i]|inactive")
    i := i + 1
ENDDO

oXml:destroy()
oRest:destroy()