KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Extending packages - api datasource #2684

Open saiful opened this topic on on May 19, 2022 - 22 comments

saiful commented on May 19, 2022

Hi,

I just know that koolreport has some new features. I’m interested using koolreport API DataSource and Restful DataSource. I’ve been read the docs, and tried to use it. But i’m still have some difficulties to use it. Here is my code:

<?php 
namespace App\Reports\CustomAPI;

use koolreport\KoolReport;

class TestCustomDatasource extends \koolreport\KoolReport
{
    use \koolreport\laravel\friendship;
    use \koolreport\excel\ExcelExportable;
    use \koolreport\export\Exportable;
    use \koolreport\cloudexport\Exportable;

// this function calls API datasource?
// i get this parameters from API datasource class
// i've tested to run the report but there's no result 
    protected function settings()
    {
        return array(
            "dataSources"=>array(
                "api_datasource"    =>array(
                    'curlOptions'   =>[],
                    'method'        =>'get',
                    'url'           =>'http://localhost/test-api-server.php',
                    'reqHeaders'    =>'',
                    'reqData'       =>[
                                        "cmp_id"=>"DEMO01",
                                        "order_no"=>"TEST0001",
                                        "seq_no"=>1
                                    ],
                    'iteration' =>'',
                    'metaData'  =>[
                                    "columns"=>[
                                        "cmp_id"=>["type"=>"string"],
                                        "order_no"=>["type"=>"string"],
                                        "seq_no"=>["type"=>"number"],
                                    ]
                                ],
                    'class'     => "\koolreport\datasources\APIDataSource"  
                ),
            )
        );
    }

// i use the minimum piping method just for testing this new feature
    protected function setup()
    {
        $this->src('api_datasource')
        ->pipe($this->dataStore('api_datastore'));
    }
}

?>

Can you please give me an example of API DataSource or Restful DataSource, like basic usage to call API and pipe it into DataStore?

Sebastian Morales commented on May 23, 2022

Pls try setting 'iteration' =>1 and let us know the result when you execute the report. Pls turn on php error reporting to E_ALL as well. Tks,

saiful commented on May 23, 2022

here is the result

Sebastian Morales commented on May 23, 2022

Pls try 'iteration" => [] and let us know the result.

saiful commented on May 23, 2022

here is the result

Sebastian Morales commented on May 23, 2022

Pls open the file koolreport/core/src/datasources/APIDataSource.php and replace these lines:

        curl_setopt_array($curl, $newOptions);
        $result = curl_exec($curl);

with these ones:

        curl_setopt_array($curl, $newOptions);        
        $result = curl_exec($curl);
        echo "newOptions = "; print_r($newOptions); echo "<br>";
        echo "result = "; print_r($result); echo "<br>";

Then open the report page and let us know the output. Tks,

saiful commented on May 23, 2022

this is the output

Sebastian Morales commented on May 23, 2022

It looks like the url together with such parameters doesn't return result, only http error 404 (not found) response. Pls make sure the url with the parameters are working.

saiful commented on May 23, 2022

my apologies, i use the wrong url, here is the result with new url:

Sebastian Morales commented on May 23, 2022

Ok, pls try commenting out these lines like this:

            // foreach ($rep as $placeholder => $replace) {
                // $k = str_replace($placeholder, $replace, $k);
                // $v = str_replace($placeholder, $replace, $v);
            // }
saiful commented on May 23, 2022

here is the result:

Sebastian Morales commented on May 23, 2022

Pls open the file koolreport/core/src/datasources/APIDataSource.php and replace these lines:

        curl_setopt_array($curl, $newOptions);
        $result = curl_exec($curl);

with these ones:

        curl_setopt_array($curl, $newOptions);        
        $result = curl_exec($curl);
        echo "newOptions = "; print_r($newOptions); echo "<br>";
        echo "result = "; print_r($result); echo "<br>";

Then open the report page and let us know the output. Tks,

saiful commented on May 24, 2022

im still get this error result:

Sebastian Morales commented on May 24, 2022

Ok, pls add an exit command like this:

        curl_setopt_array($curl, $newOptions);        
        $result = curl_exec($curl);
        echo "newOptions = "; print_r($newOptions); echo "<br>";
        echo "result = "; print_r($result); echo "<br>";
        exit; // add this command to exit processing
saiful commented on May 24, 2022

this is the result:

Sebastian Morales commented on May 24, 2022

Ok, I see the problem now. Pls open the file APIDataSouce.php, remove the exit command and replace this line:

    $row0 = $this->mapRow($data[0]);

with this one:

    $row0 = $this->mapRow(array_values($data)[0]);

Then try your report again. Rgds,

saiful commented on May 25, 2022

here is the result:

saiful commented on May 30, 2022

the result data has some row of column, but when it displayed to table, the data is only 1

Sebastian Morales commented on May 30, 2022

Looking into your API result carefully I think there's a problem with the response's JSON. The first key's value is an object while later keys' values are arrays of objects. It's not consistent in structure so the ApiDataSource can not guess. I would suggest you change your API result's JSON structure if possible. Rgds,

saiful commented on May 31, 2022

this is the api json structure that i have

are there any standard of json / array that can be used? can it use multidimensional array more than 2?

Sebastian Morales commented on May 31, 2022

It's not consistent. Your first key's value is an object (i.e array of scalar values) while later keys' values are arrays of objects. The best solution would be returning a list of objects. Otherwise, you would have to customize your ApiDataSource php code to work only with your JSON structure.

saiful commented on Jun 8, 2022

hi, i'm already tested with some json structure, but i still get error blank result:

Sebastian Morales commented on Jun 9, 2022

Pls post your DataTables create code and print out your datastore data like this:

//MyReport.view.php
\koolreport\core\Utility::prettyPrint($this->dataStore(...)->data());
...

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None