Bulk update api problem

sdohmensdohmen Posts: 12Member
Hello,

About a month ago i tried tackling the use of the variant bulk update. After this did not work i created a ticket and got some solutions. Because of the vacation time i did not have a lot of time to test them out properly.

A couple days ago i decided to give it a go again and i created the script (part of the complete one) below. The problem however is that nothing gets updated even though there are changes in pricing or quantity.

INPUT=$PAD/verwerking/combine_2.csv
OLDIFS=$IFS
IFS=";"
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read productid productname deliveryid image url variantid ean1 sku price1 cost1 qty1 ean2 qty2 erp_supply_delay price2 cost2
do

echo \n"\nProductID=$productid Variant=$variantid sku=$sku Name=$productname stockLevel=$qty2 priceIncl=$price2 ean=$ean2 Delay=$erp_supply_delay"

if [ $COUNTVAR -eq '50' ]
then
echo "We have lift off!!"
#echo $VARIANTUPDATES
curl -g https://api.webshopapp.com/nl/variants/bulk.json -u "$KEY:$SECRET" -X "PUT" $VARIANTUPDATES
COUNTVAR=1
VARIANTUPDATES=""
else
VARIANTUPDATES="${VARIANTUPDATES}-d \"variant[$COUNTVAR][id]=$variantid\" -d \"variant[$COUNTVAR][stockLevel]=$qty2\" -d \"variant[$COUNTVAR][priceIncl]=$price2\" -d \"variant[$COUNTVAR][priceCost]=$cost2\" -d \"variant[$COUNTVAR][unitPrice]=0\" -d \"variant[$COUNTVAR][oldPriceIncl]=0\" -d \"variant[$COUNTVAR][ean]=$ean2\" "
COUNTVAR=$((COUNTVAR+1))
echo $VARIANTUPDATES
fi

## Onderstaande is om minder updates te hebben
case $deliveryid in
$erp_supply_delay)
echo "SKU: " $sku " Levertijd OUD: " $deliveryid " Levertijd NIEUW: " $erp_supply_delay " is hetzelfde. Wordt niet geupdate."
;;
*)
echo "SKU: " $sku " Levertijd OUD: " $deliveryid " Levertijd NIEUW: " $erp_supply_delay " is anders. Wordt geupdate"
curl -g https://api.webshopapp.com/nl/products/$productid.xml -u "$KEY:$SECRET" -X "PUT" -d product[deliverydate]=$erp_supply_delay
;;
esac

done < $INPUT
IFS=$OLDIFS



I tried several things but so far i havent find a way to do this properly. Does anyone know what i am doing wrong?

Best regards,
Sven Dohmen

10 comments

  • jamesratcliffejamesratcliffe Posts: 160Administrator, Lightspeed Staff moderator
    edited September 2018
    Check

    the response that you're getting. If the call was successful, but the API didn't update any fields, you'll get this response:

    {
        "variants": [],
        "errors": []
    }

    You can also get errors if your response was well formatted, but Lightspeed can't make the updates for some reason. For example:

    {
        "variants": [],
        "errors": [
            {
                "message": "Variant not found. ",
                "id" : 13
            },
            {
                "message": "Variant not found. ",
                "id": 12
            }
        ]
    }

    It could also be a problem with the quoting in your curl command. Getting the quotes right in shell scripts can be challenging. It's much easier in other languages like PHP or Python that have stricter syntax.

    It may be easier in the long run to learn a bit of PHP or another language. You can build your request data as a native object (associative array, dictionary, etc.), then easily convert to JSON and sent it as the request body.

    James Ratcliffe
    Lightspeed HQ
  • sdohmensdohmen Posts: 12Member
    Hello James,

    I am working on learning php but at the moment i only have the tools available that i use now.

    Since i do not get a error i dont think the quoting is wrong. It seems more that it ignores any updates it will make. I am getting your first response that it did not update anything even though at least 75 of the 100 have 1 or more fields different like price or qty.

    Do you perhaps have any other idea what i could try?

    Best regards,
    Sven
  • jamesratcliffejamesratcliffe Posts: 160Administrator, Lightspeed Staff moderator
    @sdohmen What's the response you're getting?
    James Ratcliffe
    Lightspeed HQ
  • sdohmensdohmen Posts: 12Member
    Hello James,

    The response i am getting is:

    {
        "variants": [],
        "errors": []
    }

    I have been trying to use the PHP client btw but for some reason i am getting timeouts with that one. I downloaded the main php file (WebshopappApiClient.php) and with that i created a file test.php with the following data:

    <?php
    include_once 'WebshopappApiClient.php';

    //cluster_id is the location where the shop is hosted

    $api = new WebshopappApiClient('https://api.webshopapp.com/', 'xxx', 'xxx', 'nl');

    $shopInfo = $api->shop->get();

    echo '<pre>';
    print_r($shopInfo);
    echo '</pre>';

    ?>

    But this did not work either:

    PHP Fatal error:  Uncaught WebshopappApiException: Curl error: Failed to connect to api.webshopapp.net port 443: Connection timed out in /srv/test/WebshopappApiClient.php:840
    Stack trace:
    #0 /srv/test/WebshopappApiClient.php(790): WebshopappApiClient->handleCurlError(Resource id #5)
    #1 /srv/test/WebshopappApiClient.php(864): WebshopappApiClient->sendRequest('shop', 'get', Array)
    #2 /srv/test/WebshopappApiClient.php(5404): WebshopappApiClient->read('shop')
    #3 /srv/test/test.php(8): WebshopappApiResourceShop->get()
    #4 {main}
      thrown in /srv/test/WebshopappApiClient.php on line 840

    Any idea what that could be? If i can get that to work i might be able to push all products to the php file and then run that one.

    Best regards,
    Sven


  • Alex LugoAlex Lugo Posts: 123Administrator, Lightspeed Staff moderator
    Hello Sven,

    You need to use the clustedid, in your case eu1.
    Alex Lugo
    API Support Specialist
    Lightspeed HQ
  • sdohmensdohmen Posts: 12Member
    Hello Alex,

    I tried entering eu1 instead of the direct link but i still got the same connection time out:

    PHP Fatal error:  Uncaught WebshopappApiException: Curl error: Failed to connect to api.webshopapp.net port 443: Connection timed out in /srv/lightspeed/test/WebshopappApiClient.php:840
    Stack trace:
    #0 /srv/lightspeed/test/WebshopappApiClient.php(790): WebshopappApiClient->handleCurlError(Resource id #5)
    #1 /srv/lightspeed/test/WebshopappApiClient.php(864): WebshopappApiClient->sendRequest('shop', 'get', Array)
    #2 /srv/lightspeed/test/WebshopappApiClient.php(5404): WebshopappApiClient->read('shop')
    #3 /srv/lightspeed/test/test.php(8): WebshopappApiResourceShop->get()
    #4 {main}
      thrown in /srv/lightspeed/test/WebshopappApiClient.php on line 840

    Any idea what could cause this?
  • jamesratcliffejamesratcliffe Posts: 160Administrator, Lightspeed Staff moderator
    @sdohmen This code works:
    <?php
    require 'vendor/autoload.php';
    $api = new WebshopappApiClient('eu1', 'API_KEY', 'API_SECRET', 'nl');
    $shopInfo = $api->shop->get();
    echo '<pre>';print_r($shopInfo);echo '</pre>';
    My require statement is different because I'm using Composer, but I don't think that's related to your issue.

    From the error, it looks like the cluster isn't correct.

    James Ratcliffe
    Lightspeed HQ
  • JaivyDaamJaivyDaam Posts: 29Lightspeed Staff Lightspeed
    edited September 2018
    Hi @sdohmen
    After checking your error, it says api.webshopapp.net which should be api.webshopapp.com
    Could you change this and try again?
  • sdohmensdohmen Posts: 12Member
    Hello,

    I think the answer from James actually solved it. It seems i had EU1/ instead of just EU1.

    Besides that i decided to check on building a JSON string out of the data and it seems that solved the issue also.

    I am using the following syntax for the script now for anyone interested:


    echo "$VARIANTUPDATES" | jq . | curl -g -H "Content-Type: application/json" -i "https://api.webshopapp.com/nl/variants/bulk.json" -u "$KEY:$SECRET" -X "PUT" $VARIANTUPDATES -d @-

    From about a day of testing this seems to update just fine. I will check some more on this and possible still switch to PHP when i get the time to learn it some more.

    Thanks for helping.

    Best regards,
    Sven


  • jamesratcliffejamesratcliffe Posts: 160Administrator, Lightspeed Staff moderator
    You’re welcome.
    James Ratcliffe
    Lightspeed HQ
Sign In or Register to comment.