bnomei / kirby3-janitor

Kirby Plugin for running commands like cleaning the cache from within the Panel, PHP code, CLI or a cronjob
https://forum.getkirby.com/t/kirby3-janitor-plugin/23573
MIT License
89 stars 8 forks source link

How to call an API via blueprint #63

Closed gryzzly closed 2 years ago

gryzzly commented 2 years ago

Hey, I’m using this plugin: https://github.com/thathoff/kirby-git-content which creates an endpoint like /git-content/push and I’d like to add a button in site.yml that calls that endpoint.

This example https://github.com/bnomei/kirby3-janitor/wiki/Example:-Trigger-jons-with-JS-and-Panel-Vue shows how to call endpoints but I am not sure where would I place this code. Can you advice?

Thanks!

gryzzly commented 2 years ago

Hey, got a little bit more further, I‘m trying to call the git endpoint like this:

in config.php:

'bnomei.janitor.jobs' => [
    'contentPush' => function (Kirby\Cms\Page $page = null, string $data = null) {
        // $page => page object where the button as pressed
        $kirby->call('/git-content/push?secret=foobar');
        return [
            'status' => 200,
            'label' => $page->title() . ' ' . $data,
        ];
    },
  ],
  'thathoff' => [
    'git-content' => [
      'commit' => true,
      'cronHooksEnabled' => true,
      'cronHooksSecret' => 'foobar',
      'displayErrors' => true
    ],
  ]

But pressing the button is always showing a dialog saying: No route found for path: "git-content/push" and request method: "GET".

However, I can see that the route is indeed installed at http://localhost:8000/git-content/push – if I visit it I receive the appropriate error message in JSON like this:

{"status":"forbidden","message":"Invalid secret passed"}

Do you see something obviously wrong with what I am doing?

Thanks!

bnomei commented 2 years ago

Since the lib you are using is exposing simple helper calls to routes you could just call them manually.

'bnomei.janitor.jobs' => [
    'contentPush' => function (Kirby\Cms\Page $page = null, string $data = null) {
        // $page => page object where the button as pressed
        $git = new \Thathoff\GitContent\KirbyGitHelper();
        $status = $git->push(); // does not work yet
        $status = true; 
        return [
            'status' => status ? 200 : 204,
            'label' => 'Pushed content',
        ];
    },
  ],
];

https://github.com/thathoff/kirby-git-content/blob/72e079438e0af861a75074d95c7ee24643bff5f9/src/KirbyGit.php#L41

maybe @thathoff could add some return status values to the helpers push and pull methods.

thathoff commented 2 years ago

Great idea, thanks @bnomei. That seems like a good Idea, i’ve just created an issue for this.

PS: @gryzzly we’ve started working on a Panel Area (supported since kirby 3.6) with push and pull buttons. You can try this by using the panel branch (or dev-panel version if you use composer).