Framework/Helpers/RemoteApiHelper.ps1

Set-StrictMode -Version Latest

class RemoteApiHelper {
    # TODO: Move to config
    hidden static [string] $ApiBaseEndpoint = [ConfigurationManager]::GetAzSdkConfigData().AzSDKApiBaseURL; #https://localhost:44380/api
    <#
    hidden static [Newtonsoft.Json.JsonSerializerSettings] $SerializerSettings;
 
    static RemoteApiHelper() {
        [RemoteApiHelper]::SerializerSettings = [Newtonsoft.Json.JsonSerializerSettings]::new()
        [RemoteApiHelper]::SerializerSettings.Converters.Add([Newtonsoft.Json.Converters.StringEnumConverter]::new())
        [RemoteApiHelper]::SerializerSettings.Formatting = [Newtonsoft.Json.Formatting]::None
        [RemoteApiHelper]::SerializerSettings.NullValueHandling = [Newtonsoft.Json.NullValueHandling]::Ignore
        [RemoteApiHelper]::SerializerSettings.ReferenceLoopHandling = [Newtonsoft.Json.ReferenceLoopHandling]::Ignore
    }
    #>



    hidden static [string] GetAccessToken() {
        #TODO: Move resource to config
        return [Helpers]::GetAccessToken("https://management.core.windows.net/");
    }

    hidden static [psobject] PostContent($uri, $content, $type) {
        try {
            $accessToken = [RemoteApiHelper]::GetAccessToken()
            $result = Invoke-WebRequest -Uri $([RemoteApiHelper]::ApiBaseEndpoint + $uri) `
                -Method Post `
                -Body $content `
                -ContentType $type `
                -Headers @{"Authorization" = "Bearer $accessToken"} `
                -UseBasicParsing
            return $result
        }
        catch {
            return "ERROR"
        }
    }

    hidden static [psobject] PostJsonContent($uri, $obj) {
        $postContent = [Helpers]::ConvertToJsonCustomCompressed($obj)
        return [RemoteApiHelper]::PostContent($uri, $postContent, "application/json")
    }

    static [void] PostSubscriptionScanResult($scanResult) {
        [RemoteApiHelper]::PostJsonContent("/scanresults/subscription", $scanResult) | Out-Null
    }

    static [void] PostServiceScanResult($scanResult) {
        [RemoteApiHelper]::PostJsonContent("/scanresults/service", $scanResult) | Out-Null
    }

    static [void] PostResourceInventory($resources) {
        [RemoteApiHelper]::PostJsonContent("/inventory/resources", $resources) | Out-Null
    }
}