Chilkat Online Tools

GetRecommendations DataFlex Example

Amazon Wisdom

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iSuccess
    Variant vAuthAws
    Handle hoAuthAws
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Integer iRespStatusCode
    Handle hoJResp
    String sContentArn
    String sContentId
    String sKnowledgeBaseArn
    String sKnowledgeBaseId
    String sText
    String sTitleText
    String sRecommendationId
    String sRelevanceLevel
    Integer iRelevanceScore
    Integer j
    Integer iCount_j
    Integer iBeginOffsetInclusive
    Integer iEndOffsetExclusive
    Integer i
    Integer iCount_i
    String sTemp1
    Integer iTemp1

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

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    Get Create (RefClass(cComChilkatAuthAws)) To hoAuthAws
    If (Not(IsComObjectCreated(hoAuthAws))) Begin
        Send CreateComObject of hoAuthAws
    End
    Set ComAccessKey Of hoAuthAws To "AWS_ACCESS_KEY"
    Set ComSecretKey Of hoAuthAws To "AWS_SECRET_KEY"

    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    Set ComRegion Of hoAuthAws To "us-west-2"
    Set ComServiceName Of hoAuthAws To "wisdom"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    Get pvComObject of hoAuthAws to vAuthAws
    Get ComSetAuthAws Of hoRest vAuthAws To iSuccess

    // URL: https://wisdom.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    Get ComConnect Of hoRest "wisdom.us-west-2.amazonaws.com" 443 True True To iSuccess
    If (iSuccess <> True) Begin
        Get ComConnectFailReason Of hoRest To iTemp1
        Showln "ConnectFailReason: " iTemp1
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "Content-Type" "application/x-amz-json-1.1" To iSuccess
    Get ComAddHeader Of hoRest "X-Amz-Target" "GetRecommendations" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComFullRequestNoBodySb Of hoRest "GET" "/assistants/{assistantId}/sessions/{sessionId}/recommendations" vSbResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iRespStatusCode
    Showln "response status code = " iRespStatusCode
    If (iRespStatusCode <> 200) Begin
        Showln "Response Header:"
        Get ComResponseHeader Of hoRest To sTemp1
        Showln sTemp1
        Showln "Response Body:"
        Get ComGetAsString Of hoSbResponseBody To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    Move 0 To i
    Get ComSizeOfArray Of hoJResp "recommendations" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJResp To i
        Get ComStringOf Of hoJResp "recommendations[i].document.contentReference.contentArn" To sContentArn
        Get ComStringOf Of hoJResp "recommendations[i].document.contentReference.contentId" To sContentId
        Get ComStringOf Of hoJResp "recommendations[i].document.contentReference.knowledgeBaseArn" To sKnowledgeBaseArn
        Get ComStringOf Of hoJResp "recommendations[i].document.contentReference.knowledgeBaseId" To sKnowledgeBaseId
        Get ComStringOf Of hoJResp "recommendations[i].document.excerpt.text" To sText
        Get ComStringOf Of hoJResp "recommendations[i].document.title.text" To sTitleText
        Get ComStringOf Of hoJResp "recommendations[i].recommendationId" To sRecommendationId
        Get ComStringOf Of hoJResp "recommendations[i].relevanceLevel" To sRelevanceLevel
        Get ComIntOf Of hoJResp "recommendations[i].relevanceScore" To iRelevanceScore
        Move 0 To j
        Get ComSizeOfArray Of hoJResp "recommendations[i].document.excerpt.highlights" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJResp To j
            Get ComIntOf Of hoJResp "recommendations[i].document.excerpt.highlights[j].beginOffsetInclusive" To iBeginOffsetInclusive
            Get ComIntOf Of hoJResp "recommendations[i].document.excerpt.highlights[j].endOffsetExclusive" To iEndOffsetExclusive
            Move (j + 1) To j
        Loop

        Move 0 To j
        Get ComSizeOfArray Of hoJResp "recommendations[i].document.title.highlights" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJResp To j
            Get ComIntOf Of hoJResp "recommendations[i].document.title.highlights[j].beginOffsetInclusive" To iBeginOffsetInclusive
            Get ComIntOf Of hoJResp "recommendations[i].document.title.highlights[j].endOffsetExclusive" To iEndOffsetExclusive
            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop

    // A sample JSON response body parsed by the above code:

    // {
    //   "recommendations": [
    //     {
    //       "document": {
    //         "contentReference": {
    //           "contentArn": "string",
    //           "contentId": "string",
    //           "knowledgeBaseArn": "string",
    //           "knowledgeBaseId": "string"
    //         },
    //         "excerpt": {
    //           "highlights": [
    //             {
    //               "beginOffsetInclusive": number,
    //               "endOffsetExclusive": number
    //             }
    //           ],
    //           "text": "string"
    //         },
    //         "title": {
    //           "highlights": [
    //             {
    //               "beginOffsetInclusive": number,
    //               "endOffsetExclusive": number
    //             }
    //           ],
    //           "text": "string"
    //         }
    //       },
    //       "recommendationId": "string",
    //       "relevanceLevel": "string",
    //       "relevanceScore": number
    //     }
    //   ]
    // }


End_Procedure