Shared/DqConnectionTypes.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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;
  }
}