Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
830 stars 92 forks source link

Webhook #1346

Closed GHRoss closed 2 months ago

GHRoss commented 2 months ago

Is there an example of a webhook server? I'm looking to have an endpoint that accepts anything that's thrown at it with auth in the header that writes the request to a file or database.

mdaneri commented 2 months ago

You can find one here. It's not yet committed because it's part of https://github.com/Badgerati/Pode/pull/1333 https://github.com/Badgerati/Pode/blob/f16cc3a61a502485f3f2713b4c0aafd4d618def6/examples/WebHook.ps1

GHRoss commented 2 months ago

You can find one here. It's not yet committed because it's part of #1333 https://github.com/Badgerati/Pode/blob/f16cc3a61a502485f3f2713b4c0aafd4d618def6/examples/WebHook.ps1

Wow! What a happy coincidence! What would be the best way to add simple auth to this?

GHRoss commented 2 months ago

Also, would the best place to do the actual storing of the data be on line 141?

mdaneri commented 2 months ago

the easy way is to save on the file system using Save-PodeState -Path './state.json' on line 114

$state:subscriptions.Remove($data.url)
# Saving to ./state.json
Save-PodeState -Path './state.json'`

# Respond with a status message
Write-PodeJsonResponse -Value @{ message = 'Unsubscribed successfully!' } -StatusCode 200

and on line 79

$state:subscriptions[$data.url] = $true
# Saving to ./state.json
Save-PodeState -Path './state.json'`

# Respond with a status message
Write-PodeJsonResponse -Value @{ message = 'Subscribed successfully!' }
mdaneri commented 2 months ago

for the authentication you can grab as example petstore https://github.com/Badgerati/Pode/blob/f16cc3a61a502485f3f2713b4c0aafd4d618def6/examples/PetStore/Petstore-openApi.ps1 Line 129

 New-PodeAuthScheme -ApiKey -LocationName 'api_key' | Add-PodeAuth -Name 'api_key' -Sessionless -ScriptBlock {
GHRoss commented 2 months ago

the easy way is to save on the file system using Save-PodeState -Path './state.json' on line 114

$state:subscriptions.Remove($data.url)
# Saving to ./state.json
Save-PodeState -Path './state.json'`

# Respond with a status message
Write-PodeJsonResponse -Value @{ message = 'Unsubscribed successfully!' } -StatusCode 200

and on line 79

$state:subscriptions[$data.url] = $true
# Saving to ./state.json
Save-PodeState -Path './state.json'`

# Respond with a status message
Write-PodeJsonResponse -Value @{ message = 'Subscribed successfully!' }

Interesting! So you wouldn't use the /store endpoint?

mdaneri commented 2 months ago

Sorry, I was saving the state, not the store. The store you can save it anywhere you like S3, File, DB, or if you want to use $state: you can do it too

GHRoss commented 2 months ago

Sorry, I was saving the state, not the store. The store you can save it anywhere you like S3, File, DB, or if you want to use $state: you can do it too

Great, thank you. Just to be clear, best to add to datastore on line 141 as validation would have passed?

mdaneri commented 2 months ago

This is a sample theoretical; you don't need to print the content of your body. Sure, It can be moved after the validation. Validation is not something that many people use

GHRoss commented 2 months ago

Thank you!