classes/AtlassianContext.psm1

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
class AtlassianContext {
    #####################
    # HIDDEN PROPERTIES #
    #####################
    
    #####################
    # PUBLIC PROPERTIES #
    #####################

    [hashtable]
    $AuthHeader

    [string]
    $HostName

    [ValidateRange(1,10)]
    [int32]
    $Retries
    
    [ValidateRange(1, 60)]
    [int32]
    $RetryDelay

    ################
    # CONSTRUCTORS #
    ################

    AtlassianContext(
        [string]$UserName,
        [string]$ApiKey,
        [string]$HostName
    ){
        $this.Init($UserName,$ApiKey,$HostName,1,1)
    }

    AtlassianContext(
        [string]$UserName,
        [string]$ApiKey,
        [string]$HostName,
        [int32]$Retries,
        [int32]$RetryDelay
    ){
        $this.Init($UserName,$ApiKey,$HostName,$Retries,$RetryDelay)
    }

    ##################
    # HIDDEN METHODS #
    ##################

    hidden
    [void]
    Init(
        [string]$UserName,
        [string]$ApiKey,
        [string]$HostName,
        [int32]$Retries,
        [int32]$RetryDelay
    ){
        # create the unencoded string
        $credentialsText = "$UserName`:$ApiKey"

        # encode the string in base64
        $credentialsBytes = [System.Text.Encoding]::UTF8.GetBytes($credentialsText)
        $encodedCredentials = [Convert]::ToBase64String($credentialsBytes)

        # format the host name
        $formattedHost = (&{If($HostName.EndsWith("/")) {$HostName.Substring(0,$HostName.Length-1)} else {$HostName}})

        # set the object properties
        $this.AuthHeader = @{Authorization="Basic $encodedCredentials"}
        $this.HostName = $formattedHost
        $this.Retries = $Retries
        $this.RetryDelay = $RetryDelay
    }

    ##################
    # PUBLIC METHODS #
    ##################

    
}