Classes/DownloadManager.ps1
|
class DownloadManager { [LibraryContext]$Context DownloadManager([LibraryContext]$context) { $this.Context = $context } # currently not in use -- also not tested! [void]DownloadFile([string]$Url, [string]$Destination) { $this.Context.LogManager.Log("Starting download from $Url") $dir = Split-Path $Destination $this.Context.FolderManager.CreateFolder($dir) Invoke-WebRequest -Uri $Url -OutFile $Destination -UseBasicParsing $this.Context.LogManager.Log("Download completed: $Destination") } # Download config [object]DownloadConfig ([string]$url, [string]$format) { $this.Context.LogManager.Log("Downloading config from: $url") try { if ($format -eq "json") { # Invoke-RestMethod auto-parses JSON into a PSCustomObject $config = Invoke-RestMethod -Uri $url -UseBasicParsing -ErrorAction Stop } else { $config = Invoke-RestMethod -Uri $url -ErrorAction Stop } if (-not $config) { throw "Config is null or empty." } return $config } catch { $this.Context.LogManager.Log("Failed to download or parse config: $($_.Exception.Message)", "ERROR") return $null } } } |