hostname-service/examples/notebook_cli.ps1

69 lines
2.3 KiB
PowerShell

# Copyright 2024 Florian Beisel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Define API URLs
$loginUrl = "http://localhost:8080/api/v1/login"
$generateNotebookUrl = "http://localhost:8080/api/v1/notebook"
$docsUrl = "http://localhost:8080/swagger/doc.json" # URL to fetch the docs.json
# User credentials
$username = "admin"
$password = "defaultPassword" # Replace with the actual password
# Prepare login request body
$loginBody = @{
username = $username
password = $password
} | ConvertTo-Json
# Send login request
$response = Invoke-RestMethod -Method Post -Uri $loginUrl -Body $loginBody -ContentType "application/json"
# Extract token from login response
$token = $response.token
# Check if we got a token
if (-not $token) {
Write-Error "Authentication failed"
exit
}
# Prepare the request header with the received token
$headers = @{
Authorization = "Bearer $token"
}
# Fetch the docs.json
$docs = Invoke-RestMethod -Uri $docsUrl
# Extract NotebookRuleInput model
$notebookRuleInputModel = $docs.definitions."rules.NotebookRuleInput".properties
# Create newNotebookParams based on the NotebookRuleInput model
$newNotebookParams = @{}
foreach ($prop in $notebookRuleInputModel.PSObject.Properties) {
$userInput = Read-Host -Prompt "Enter value for $($prop.Name)"
$newNotebookParams[$prop.Name] = $userInput
}
# Convert newNotebookParams to JSON
$newNotebookParamsJson = $newNotebookParams | ConvertTo-Json
# Send request to generate a new notebook
$newNotebookResponse = Invoke-RestMethod -Method Post -Uri $generateNotebookUrl -Headers $headers -Body $newNotebookParamsJson -ContentType "application/json"
# Output the response
Write-Output "New Notebook Response:"
$newNotebookResponse