Compressing and extracting data

Example 1 (using HTTPClient)

The HTTPClient object sends a request with a header "Accept-Encoding:gzip" which informs the RESTFul Web service that the client can extract data; then the Web service returns a compressed package and a response header "Content-Encoding: gzip" which indicates that the data is compressed; and then the ExtractorObject object extracts data from the package.

HttpClient   lhc_Client
ExtractorObject lnv_extractor
String     ls_Url, ls_id, ls_Method, ls_Body, ls_Respose
Long       ll_rtn
Blob       lb_body, lb_Extr
Boolean     ib_comp

lnv_extractor = Create ExtractorObject
lhc_Client = Create HttpClient

ls_Url = "https://demo.appeon.com/pb/webapi_client/department"
ls_Method = "GET"
lhc_Client.timeout = 10
lhc_Client.SetRequestHeader ( "Content-Type", "application/json" )
lhc_Client.SetRequestHeader("Accept-Encoding", "gzip")

ll_rtn = lhc_Client.sendrequest( ls_Method, ls_Url )
If ll_rtn = 1 Then
  ls_Respose = lhc_Client.Getresponseheaders( )
  If Pos (ls_Respose, "Content-Encoding: gzip"  ) > 0 Or Pos ( ls_Respose, "gzip" ) > 0 Then
    ib_comp = true
  End If
  If ib_comp Then
    // Extract the package
    ll_rtn = lhc_Client.GetResponsebody(lb_Body)
    If ll_rtn = 1 Then
      ll_rtn = lnv_extractor.Extract(lb_body, lb_Extr, ArchiveFormatGZip!)
      If ll_rtn = 1 Then
        ls_Body = String ( lb_Extr,EncodingUTF8! ) 
        MessageBox ( "Extract  Success", ls_Body )
      Else
        MessageBox ( "Extract Failed", "return:" + String (ll_rtn) )
      End If
    Else
      MessageBox ( "GetResponsebody Failed", "return:" + String (ll_rtn) )
    End If
  else
    // Extraction did not happen
    ll_rtn = lhc_Client.GetResponsebody( ls_Body,EncodingUTF8!)
    MessageBox ( "No Extract", ls_Body )
  End IF
Else
  MessageBox ( "SendRequest Failed", "Return:" + String ( ll_rtn ) )
End If

If IsValid (lnv_extractor) Then Destroy ( lnv_extractor )
If IsValid (lhc_Client) Then Destroy ( lhc_Client )

Example 2 (using RESTClient)

The RESTClient object sends a request with a header "Accept-Encoding:gzip" which informs the RESTFul Web service that the client can extract data; then the Web service returns a compressed package and a response header "Content-Encoding: gzip" which indicates that the data is compressed; and then the RESTClient object retrieves data from the compressed package.

RestClient   lrc_Client
String     ls_Url, ls_Method
Long     ll_rtn

lrc_Client = Create RestClient
ls_Url = "https://demo.appeon.com/pb/webapi_client/department"
ls_Method = "GET"
lrc_Client.SetRequestHeader ("Content-Type", "application/json")
lrc_Client.SetRequestHeader("Accept-Encoding", "gzip")

ll_rtn = lrc_Client.Retrieve( dw_1,ls_Url )
If ll_rtn >= 0 And lrc_Client.GetResponseStatusCode() = 200 Then
  MessageBox( "Retrieve Success","Rows:" + String ( ll_rtn ))
Else
  MessageBox( "Retrieve Failed","Rows:" + String ( ll_rtn ))
End If

If IsValid (lrc_Client) Then Destroy ( lrc_Client )

Example 3 (using OAuthClient)

The OAuthClient object sends a request with a header "Accept-Encoding:gzip" which informs the RESTFul Web service that the client can extract data; then the Web service returns a compressed package and a response header "Content-Encoding: gzip" which indicates that the data is compressed; and then the ExtractorObject object extracts data from the package.

OAuthRequest   loa_Request
OAuthClient loa_Client
ExtractorObject lnv_extractor
ResourceResponse   lrr_Response
Integer     li_rtn
Long       ll_rtn
String     ls_Body,ls_Response,ls_Token
Blob       lb_body, lb_Extr
Boolean     ib_comp

lnv_extractor = Create ExtractorObject
loa_Client = Create OAuthClient

ls_Token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE1NDZjNzliNzkyODVmYTJmMzZjY2Q3Mzg1OGE4MjY3IiwidHlwIjoiSldUIn0.eyJuYmYiOjE1NDQ0MzMzMzAsImV4cCI6MTU0NDQzNjkzMCwiaXNzIjoiaHR0cDovL2NzaGFycHNlcnZlci5hcHBlb24uY29tOjYwMDAiLCJhdWQiOlsiaHR0cDovL2NzaGFycHNlcnZlci5hcHBlb24uY29tOjYwMDAvcmVzb3VyY2VzIiwiQXBwZW9uQXBpIl0sImNsaWVudF9pZCI6ImNsaWVudCIsInN1YiI6ImN1c3RvbV9jb2RlIiwiYXV0aF90aW1lIjoxNTQ0NDMzMzMwLCJpZHAiOiJsb2NhbCIsInNjb3BlIjpbIkFwcGVvbkFwaSJdLCJhbXIiOlsiY3VzdG9tX2NyZWRlbnRpYWxzIl19.XlGwMqVRwJ_4gkIbNaK_HX6_0hvWE0EJXciurkNjqdOZegF_QQYTJp3jBA1idtMC_lB24TurZM1JSfbTXv4ZSQVdCTk3p5kyV8UTqpDUKbu73HQoPNDlXuTyQb58rGVRGC4bp7weLlpUqrQT2OB8PT2N_JCWtTnrwNToNsc4H1e2NNvNUoe90May7ICs2ovofQ37FQG7IwLSoe_aUsS-8togNxQ1SxsdR7__Amb0G0Asu8QaRTIzomerDGX9Ct_yt6cgz-3Z7jR9Eb1QFaZxr_PALwMVIHVmHJK58GCePGQ0nivJCYMO4WEhysme_Thics4cx_EKl4T8t0VHmcqCNw"
loa_Request.SetHeader("Accept-Encoding", "gzip")
li_rtn = loa_Request.SetAccessToken (ls_Token)
loa_Request.Method = "GET"
loa_Request.Url = "https://demo.appeon.com/pb/webapi_client/identity/departments"
li_rtn =loa_Client.RequestResource( loa_Request, lrr_Response )
If li_rtn = 1 Then
  ls_Response = lrr_Response.getheaders( )
  If Pos (ls_Response, "Content-Encoding: gzip"  ) > 0 Or Pos ( ls_Response, "gzip" ) > 0 Then
    ib_comp = true
  End If
  If ib_comp Then
    // Extract the package
    ll_rtn = lrr_Response.getbody( lb_body)
    If ll_rtn = 1 Then
      ll_rtn = lnv_extractor.Extract(lb_body, lb_Extr,ArchiveFormatGZip!)
      If ll_rtn = 1 Then
        ls_body = String ( lb_Extr,EncodingUTF8! ) 
        MessageBox( "Extract Success", String ( Len ( ls_body ) ) )
      Else
        MessageBox( "Extract Failed", "return:" + String (ll_rtn) )
      End If
    Else
      MessageBox( "Getbody Failed", "return:" + String (ll_rtn) )
    End If
    
  else
    // Extraction did not happen
    ll_rtn = lrr_Response.getbody( ls_Body)
    MessageBox( "No Extract", ls_Body )
  End IF
Else
  MessageBox ( "RequestResource Failed","RequestResource Return:" + String ( li_Rtn ) )
End If

If IsValid ( loa_Client ) Then Destroy ( loa_Client )
If IsValid ( lnv_extractor ) Then Destroy ( lnv_extractor )