fastly / fastly-php

A Fastly API client for PHP
https://packagist.org/packages/fastly/fastly
MIT License
24 stars 34 forks source link

Creating VCL Snippets via the PHP Library throws error: Invalid value '0' for 'dynamic', must be one of '0', '1' #50

Open deriknelvaimo opened 4 months ago

deriknelvaimo commented 4 months ago

Version

5.3.0

What happened

Trying to add VCL Snippets to a service throws: Exception when calling SnippetApi->createSnippet: Invalid value '0' for 'dynamic', must be one of '0', '1' even though dynamic is a string of '0'.

I am using this guide: https://www.fastly.com/documentation/reference/api/vcl-services/snippet/

Sample Code (sensitive bits removed):

<?php

require_once(__DIR__ . '/vendor/autoload.php');
ini_set('display_errors', 0);

$config = Fastly\Configuration::getDefaultConfiguration()->setApiToken('REDACTED');
$apiInstance = new Fastly\Api\SnippetApi(null, $config);

$options = [
    'service_id' => 'REDACTED',
    'version_id' => 1,
    'name' => 'add_detected_bot_headers_recv',
    'type' => 'recv',
    'dynamic' => '0',
    'content' => 'if (client.class.bot) { set req.http.X-Known-Bot = "1"; set req.http.X-Known-Bot-Name = client.bot.name; } else { set req.http.X-Known-Bot = "0"; }'
];

try {
    $result = $apiInstance->createSnippet($options);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling SnippetApi->createSnippet: ', $e->getMessage(), PHP_EOL;
    print_r($options);
}

Returns:

Exception when calling SnippetApi->createSnippet: Invalid value '0' for 'dynamic', must be one of '0', '1'

With Options Array:

Array
(
    [service_id] => REDACTED
    [version_id] => 1
    [name] => add_detected_bot_headers_recv
    [type] => recv
    [dynamic] => 0
    [content] => if (client.class.bot) { set req.http.X-Known-Bot = "1"; set req.http.X-Known-Bot-Name = client.bot.name; } else { set req.http.X-Known-Bot = "0"; }
)
deriknelvaimo commented 4 months ago

Checked in the Portal and it adds the snippet but throws an exception error so the code breaks but the snippet is actually added:

SCR-20240606-gqij