Shared/DqConnectionTypes.ps1

Class DqConnection
{
  [String] $PortalUrl

  [String] $ApiUrl

  [String] $StsUrl

  DqConnection() 
  {
  }

  DqConnection([String]$ApiUrl, [String]$StsUrl)
  {
    $this.ApiUrl = $ApiUrl;
    $this.StsUrl = $StsUrl;
  }

  DqConnection([String]$PortalUrl, [String]$ApiUrl, [String]$StsUrl)
  {
    $this.PortalUrl = $PortalUrl;
    $this.ApiUrl = $ApiUrl;
    $this.StsUrl = $StsUrl;
  }
}

Class DqConnectionContext
{
  [DqConnection] $Connection
  [String] $TokenType
  [String] $Token
  [HashTable] $Headers 

  DqConnectionContext([DqConnection]$Connection, [String] $TokenType, [String] $Token)
  {
    If ($Connection -eq $null)
    {
      Write-Error "connection cannot be null."
      Return;
    }

    If ([String]::IsNullOrEmpty($TokenType))
    {
      Write-Error "tokenType cannot be null."
      Return;
    }

    If ([String]::IsNullOrEmpty($Token))
    {
      Write-Error "token cannot be null."
      Return;
    }

    $this.Connection = $Connection;
    $this.TokenType = $TokenType;
    $this.Token = $Token;
    $this.Headers = [DqConnectionContext]::CreateWebRequestHeaders($this.Connection, $this.TokenType, $this.Token, $this.Connection.PortalUrl)
  }

  [String] GetEntityControllerUrl()
  {
    Return "$($this.Connection.ApiUrl)remote-management/entity"
  }

  [String] GetCheckControllerUrl()
  {
    Return "$($this.Connection.ApiUrl)remote-management/check"
  }

  [String] GetVersionControllerUrl()
  {
    Return "$($this.Connection.ApiUrl)remote-management/version"
  }

  static [HashTable] CreateWebRequestHeaders([DqConnection]$Connection, [String] $TokenType, [String] $Token, [String] $PortalUrl)
  {
    $Authorization = [string]::Format("{0} {1}", $TokenType, $Token)
    $RequestHeaders = @{
      "Accept"        = "application/json"
      "Content-Type"  = "application/json;charset=UTF-8"
      "Authorization" = $Authorization
      "Referer"       = $PortalUrl
    }

    Return $RequestHeaders;
  }
}