Functions/Get-Invoices.ps1
|
function Get-Invoices { <# .SYNOPSIS Retrieves the PDF for a specific invoice via the Gorelo API. .DESCRIPTION - Requires an -ID parameter. - Retrieves the PDF for the specified invoice from /invoices/{ID}/pdf. - The PDF content is returned as binary data (you can save it to a file). .PARAMETER BaseUrl Base API URL (default: https://api.usw.gorelo.io) .PARAMETER ApiVersion API version (default: v1) .PARAMETER ApiKey API key for authentication. .PARAMETER ID The unique invoice ID to retrieve the PDF for. Mandatory. .PARAMETER OutputPath Optional path to save the PDF file (e.g., C:\Invoices\INV-00123.pdf). If not specified, the function will return the raw PDF bytes. .EXAMPLE # Save the invoice PDF to a file Get-Invoices -ApiKey "12345" -ID "INV-00123" -OutputPath "C:\Temp\INV-00123.pdf" .EXAMPLE # Retrieve the PDF as a byte array $pdfBytes = Get-Invoices -ApiKey "12345" -ID "INV-00123" #> [CmdletBinding()] param( [string]$BaseUrl = "https://api.usw.gorelo.io", [string]$ApiVersion = "v1", [Parameter(Mandatory = $true)] [string]$ApiKey, [Parameter(Mandatory = $true)] [string]$ID, [string]$OutputPath ) if (-not $ApiKey) { throw "API key not provided. Use -ApiKey" } if (-not $ID) { throw "Invoice ID is required. Use -ID" } # Build API endpoint $uri = "$($BaseUrl.TrimEnd('/'))/$($ApiVersion.Trim('/'))/invoices/$ID/pdf" $headers = @{ "X-API-Key" = $ApiKey "Accept" = "application/pdf" } try { Write-Verbose "Sending GET request to $uri" $response = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers -ErrorAction Stop } catch { $err = $_ $response = $err.Exception.Response if ($response) { $statusCode = $response.StatusCode.value__ $statusDesc = $response.StatusDescription Write-Warning "API call failed: $statusCode ($statusDesc)" } else { Write-Warning "API call failed: $($err.Exception.Message)" } throw } # Handle output if ($OutputPath) { try { Write-Verbose "Saving PDF to $OutputPath" [IO.File]::WriteAllBytes($OutputPath, $response.Content) Write-Host "Invoice PDF saved to: $OutputPath" -ForegroundColor Green return $OutputPath } catch { throw "Failed to save PDF file to $OutputPath. $($_.Exception.Message)" } } else { # Return raw PDF bytes if no output path specified return $response.Content } } <# $APIKey = 'xxx' Get-Invoices -ApiKey $APIKey -ID 'xxx' # Gets Invoice by ID Get-Invoices -ApiKey $APIKey -ID "xxx" -OutputPath "C:\xxx\xxx.pdf" # Gets Invoice by ID and saves locally #> |