Public/ps1/Job/Get-ApprxrResult.ps1

<#
    .SYNOPSIS
    Retrieves a result from a remote API endpoint using Apprxr authentication.
 
    .DESCRIPTION
    Sends a GET or POST request to the specified API endpoint, using a bearer token for authentication. Handles token renewal if the request fails due to authentication.
 
    .PARAMETER request
    The API endpoint or path to request.
 
    .PARAMETER body
    The request body to send for POST requests (optional).
 
    .EXAMPLE
    Get-ApprxrResult -request 'api/data' -body $jsonBody
 
    .NOTES
    Used for interacting with remote APIs in Apprxr jobs and automation.
#>

function Get-ApprxrResult{
    [CmdletBinding()]
    param($request, $body)
    $token = Get-ApprxrToken
    $headers = @{"Authorization"="Bearer "+$token;}
    $requestUrl = (Get-ApprxrUrlHost) + $request
    Log("Requesting url: $requestUrl")
    try {
        if ($body) {
            $answer = Invoke-RestMethod $requestUrl -Method Post -Body $body -ContentType "application/json; charset=utf-8" -Headers $headers
        } else {
            $answer = Invoke-RestMethod $requestUrl -Headers $headers
        } } catch {
            write-host "Token not accepected. Try renewing"
            if ($global:token) {
                Remove-Variable token -Scope Global
            }
            $token = Get-ApprxrToken -force $true
            $headers = @{"Authorization"="Bearer "+$token;}
        if ($body) {
            $answer = Invoke-RestMethod $requestUrl -Method Post -Body $body -ContentType "application/json; charset=utf-8" -Headers $headers
        } else {
            $answer = Invoke-RestMethod $requestUrl -Headers $headers
        }
    }

    return $answer
}