jalendport / craft-fetch

Utilise the Guzzle HTTP client from within your Craft templates.
MIT License
23 stars 13 forks source link

Craft 3.6 & Guzzle 7.x Support #9

Open zackspear opened 3 years ago

zackspear commented 3 years ago

After updating to Craft 3.6.x along with it Guzzle was updated to 7.3. This plugin stopped working.

I created a proof of concept to help troubleshoot the issue.

# spun up a new project via
composer create-project craftcms/craft craft-latest
# added plugin
composer require jalendport/craft-fetch
# installed plugin via Control Panel > Settings → Plugins and click the “Install” button for Fetch.

Modified index.twig

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta charset="utf-8" />
    <title>three 66666666</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
    <meta name="referrer" content="origin-when-cross-origin" />
</head>
<body>
{% set client = {
    base_uri : 'https://api.chucknorris.io/jokes/random',
    timeout : 30
} %}
{% set request = fetch(client, 'GET', null) %}
<pre><code>{{ dump(client) }}</code></pre>
<pre><code>{{ dump(request) }}</code></pre>
</body>
</html>

Results in

/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=2)
  'base_uri' => string 'https://api.chucknorris.io/jokes/random' (length=39)
  'timeout' => int 30

/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=2)
  'error' => boolean true
  'reason' => string 'URI must be a string or UriInterface' (length=36)

Not sure why it's returning this error.

If I then install Guzzle 6.x

composer require guzzlehttp/guzzle:^6.5.5

Plugin works again

/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=2)
  'base_uri' => string 'https://api.chucknorris.io/jokes/random' (length=39)
  'timeout' => int 30

/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=3)
  'statusCode' => int 200
  'reason' => string 'OK' (length=2)
  'body' => 
    array (size=7)
      'categories' => 
        array (size=0)
          empty
      'created_at' => string '2020-01-05 13:42:24.40636' (length=25)
      'icon_url' => string 'https://assets.chucknorris.host/img/avatar/chuck-norris.png' (length=59)
      'id' => string '_v6XFMafT_yXl7hxHKaVIg' (length=22)
      'updated_at' => string '2020-01-05 13:42:24.40636' (length=25)
      'url' => string 'https://api.chucknorris.io/jokes/_v6XFMafT_yXl7hxHKaVIg' (length=55)
      'value' => string 'Chuck Norris took the dingo's baby.' (length=35)
zackspear commented 3 years ago

Guzzle 7 doesn't like the relative URI parameter being null || undefined

Previously in Twig

{% set client = {
    base_uri : 'https://api.chucknorris.io/jokes/random',
    timeout : 30
} %}
{% set request = fetch(client, 'GET', null) %}

If I tweak the Twig template fetch usage to

{% set request = fetch(client, 'POST', '') %}

everything works again.

Will have a PR to prevent this error shortly.

DavidOliver commented 3 years ago

I've just started using Fetch and am currently still on Craft 3.5. Am I right in thinking that as long as I use a string (empty or not) for the destination, updating to Craft 3.6 shouldn't pose a problem for Fetch? Thanks.

zackspear commented 3 years ago

@DavidOliver that's correct. It just doesn't like the destination parameter being null || undefined.