Private/Select-ClientToken.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
function Select-ClientToken {
    # helper function that selects the appropriate token for the client action
    # read options should use 'read_token' if available
    # write options should use 'write_token' if available
    # otherwise default to 'token'
    param (
        [Parameter(Mandatory = $true)]  
        [string]$Token,

        [Parameter(Mandatory = $false)]
        [ValidateSet('Read', 'Write')]
        [string]$RequestType = "Read"
    )

    if ($Token -ne $Script:CIF3.Token) {
        # $Token defaults to $Script:CIF3.Token in the public functions
        # use explicitly provided token from the public function if provided
        Write-Verbose "Selecting explicitly passed token"
        return $Token
    }
    else {
        if ($Script:CIF3.ReadToken -and $RequestType -eq "Read") {
            # use the read token if configured
            Write-Verbose "Selecting read_token"
            return $Script:CIF3.ReadToken
        }
        elseif ($Script:CIF3.WriteToken -and $RequestType -eq "Write") {
            # use the write token if configured
            Write-Verbose "Selecting write_token"
            return $Script:CIF3.WriteToken
        }
        else {
            Write-Verbose "Selecting token"
            return $Script:CIF3.Token
        }
    }
}