Sample code for 30+ languages & platforms
PureBasic

Populi Get Roles

See more Populi Examples

Demonstrates the Populi getRoles task.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkXml.pb"

Procedure ChilkatExample()

    success.i = 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.
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkXml::ckLoadXmlFile(xml,"qa_data/tokens/populi_token.xml")
    accessKey.s = CkXml::ckGetChildContent(xml,"access_key")
    If CkXml::ckLastMethodSuccess(xml) <> 1
        Debug "Did not find the access_key"
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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.
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"yourcollege.populi.co",443,1,bAutoReconnect)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkXml::ckDispose(xml)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::setCkAuthorization(rest, accessKey)

    CkRest::ckAddQueryParam(rest,"task","getRoles")

    responseBody.s = CkRest::ckFullRequestFormUrlEncoded(rest,"POST","/api/index.php")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkXml::ckDispose(xml)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ; We should expect a 200 response if successful.
    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "Request Header: "
        Debug CkRest::ckLastRequestHeader(rest)
        Debug "----"
        Debug "Response StatusCode = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Response StatusLine: " + CkRest::ckResponseStatusText(rest)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug responseBody
        CkXml::ckDispose(xml)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkXml::ckLoadXml(xml,responseBody)
    Debug CkXml::ckGetXml(xml)

    ; 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.i
    count_i.i
    id.i
    name.s
    inactive.i

    i = 0
    count_i = CkXml::ckNumChildrenHavingTag(xml,"role")
    While i < count_i
        CkXml::setCkI(xml, i)
        id = CkXml::ckGetChildIntValue(xml,"role[i]|id")
        name = CkXml::ckGetChildContent(xml,"role[i]|name")
        inactive = CkXml::ckGetChildIntValue(xml,"role[i]|inactive")
        i = i + 1
    Wend


    CkXml::ckDispose(xml)
    CkRest::ckDispose(rest)


    ProcedureReturn
EndProcedure