Skip to content

Instantly share code, notes, and snippets.

@jorgeasaurus
Created January 26, 2025 03:15
Show Gist options
  • Save jorgeasaurus/896bdd06fa0c1aa66b7bc9e9edb5d1d9 to your computer and use it in GitHub Desktop.
Save jorgeasaurus/896bdd06fa0c1aa66b7bc9e9edb5d1d9 to your computer and use it in GitHub Desktop.
Fetches a random Stoicism quote from a free API.
function Get-StoicQuote {
<#
.SYNOPSIS
Fetches a random Stoicism quote from the Stoic API.
.DESCRIPTION
This function retrieves a random Stoicism quote from the API and displays the
author and quote to the user. The API does not require authentication.
.EXAMPLE
Get-StoicQuote
Fetches and displays a random Stoicism quote.
#>
try {
# Define the API endpoint
$url = "https://stoic.tekloon.net/stoic-quote"
# Make the API request
$response = Invoke-RestMethod -Uri $url -Method Get
# Parse and display the quote
if ($response.data) {
$quote = $response.data.quote
$author = $response.data.author
Write-Output "Quote: '$quote'"
Write-Output "Author: $author"
} else {
Write-Output "No quote was retrieved. The response format might have changed."
}
} catch {
# Handle any errors that occur during the API call
Write-Error "An error occurred while fetching the quote: $_"
}
}
@jorgeasaurus
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment