Skip to content
Tabrez Shaikh edited this page Feb 24, 2022 · 3 revisions

Example of upload and download using swagger generated php client

Upload example

<?php

require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: api_key
$config = ListcleanSwagger\Client\Configuration::getDefaultConfiguration()->setApiKey('X-Auth-Token', 'YOUR-API-KEY');

$apiInstance = new ListcleanSwagger\Client\Api\UploadsApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);

//$filename = 'example.csv';//your filename (whole path if not in same dir)
$filename = 'example_498kb_invalid.csv';//your filename (whole path if not in same dir)
if(!file_exists($filename)){
    die('no file found with filename('.$filename.')');
}
$filesize = filesize($filename);
$max_chunk_size = 64000;
$max_chunk_count = ceil($filesize / $max_chunk_size);

$body = new ListcleanSwagger\Client\Model\Body();
$body->setFilename($filename);
$body->setFileType('csv');
$body->setTotalChunkCount($max_chunk_count);
$body->setMaxChunkSize($max_chunk_size);

try {
    $result = $apiInstance->uploadsPost($body);
    /*
    //print_r($result);
    Swagger\Client\Model\InlineResponse2001 Object
    (
        [container:protected] => Array
            (
                [success] => 1
                [message] => Upload created successfully
                [error_code] => 0
                [data] => Swagger\Client\Model\InlineResponse2001Data Object
                    (
                        [container:protected] => Array
                            (
                                [upload_id] => 494
                            )
    
                    )
    
            )
    
    )
    */
    if($result['success']){
        $upload_id = $result['data']['upload_id'];
        print "uploaded started successfully with upload id(".$upload_id.")\n";
        try{
            $fh = fopen($filename, "r");
        }
        catch(Exception $e){
            die('exception occurred exception:' . $e->getMessage());
        }
        if(empty($fh)){
            die('failed to open filename('.$filename.')');
        }
        print "file open successful\n";
        //starting chunk uploads
        $start = 0;
        for($i=1; $i<=$max_chunk_count; $i++){
            print "chunk sequence number: $i  start:$start  \n";
            $chunk = new ListcleanSwagger\Client\Model\Body1();
            $chunk->setChunkSequenceNumber($i);
            fseek($fh, $start);
            $content = fread($fh, $max_chunk_size);
            $start += $max_chunk_size;
            print "content:$content  \n";
            $content = base64_encode($content);
            print "encoded content:$content  \n";
            $chunk->setContent($content);
            //$md5_checksum = md5($content); 
            //print "md5_checksum:$md5_checksum \n";

            //note the md5sum should be of file data and not the chunk :0)
            //$md5_checksum = md5(file_get_contents($filename));
            //print "md5_checksum:$md5_checksum \n";

            //$chunk->setMd5Checksum($md5_checksum);
            print "calling chunk api \n";
            try {
                $chunk_result = $apiInstance->uploadsUploadIdPost($upload_id, $chunk);
                print "chunk upload results:\n";
                print_r($chunk_result);
            }
            catch (Exception $e) {
                echo 'Exception when calling UploadsApi->uploadsUploadIdPost: ', $e->getMessage(), PHP_EOL;
            }
            //upload completed
            echo 'Upload completed with all the chunks with upload_id:' . $upload_id;
            //get request id
            try {
                $upload_details = $apiInstance->uploadsUploadIdGet($upload_id);
                print "upload_details:\n";
                print_r($upload_details);
                $request_id = $upload_details['data']['request_id'];
                print "request_id($request_id)\n";
            }
            catch (Exception $e) {
                echo 'Exception when calling UploadsApi->uploadsUploadIdGet: ', $e->getMessage(), PHP_EOL;
            }
        }

    }
}
catch (Exception $e) {
    echo 'Exception when calling UploadsApi->uploadsPost: ', $e->getMessage(), PHP_EOL;
}

Download example

#format
GET https://api.listclean.xyz/v1/downloads/{request_id}/{type}?X-Auth-Token={x-auth-token}

#example for downloading clean for request id 430
wget https://api.listclean.xyz/v1/downloads/430/clean?X-Auth-Token=YOUR-API-KEY -O ./430-clean.csv

#example for downloading dirty for request id 430
wget https://api.listclean.xyz/v1/downloads/430/dirty?X-Auth-Token=YOUR-API-KEY -O ./430-dirty.csv

#example for downloading unknown for request id 430
wget https://api.listclean.xyz/v1/downloads/430/unknown?X-Auth-Token=YOUR-API-KEY -O ./430-unknown.csv

Clone this wiki locally