DSCResources/Grani_GitHubApiContent/Grani_GitHubApiContent.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
#region Initialize

function Initialize {
    # Load Assembly to use HttpClient
    try {
        Add-Type -AssemblyName System.Net.Http
    }
    catch {
    }

    # cache Location Variable
    # MSFT using this path, but this always clear when LCM runs. It means whenever you run "Get" you can't refer cache.
    # => need to change to persistence path to match with cache.
    # $script:cacheLocation = "$env:ProgramData\Microsoft\Windows\PowerShell\Configuration\BuiltinProvCache\Grani_Download"
    $script:cacheLocation = "$env:ProgramData\Microsoft\Windows\PowerShell\Configuration\CustomProvCache\Grani_GitHubApiContent"

    # GitHub API template string : RepositoryOwner / Repository / ContentPath / Branch
    $script:githubApiString = "https://api.github.com/repos/{0}/{1}/contents/{2}?ref={3}"

    # Enum for Item Type
    Add-Type -TypeDefinition @"
        public enum GraniDonwloadItemTypeEx
        {
            FileInfo,
            DirectoryInfo,
            Other,
            NotExists
        }
"@

}

Initialize

#endregion

#region Message Definition

$debugMessage = DATA {
    ConvertFrom-StringData -StringData "
        AddRequestHeader = Adding Request Header. Key : '{0}', Value : '{1}'
        AddContentType = Adding ContentType : '{0}'
        AddKeepAliveToRequestHeader = Adding Keep-Alive as true to the Request Header.
        AddOAuth2Token = Adding OAuth2Token for Basic Authentication.
        AddUserAgent = Adding UserAgent : '{0}'
        ContentTypeDetectedAsJson = ContentType passed to Request header as '{0}'. Treat response as JSON, content encoded as base64.
        ContentTypeDetectedAsRaw = ContentType passed to Request header as '{0}'. Treat response as RAW, content directly pick from result.
        ContentTypeDetectedAsHtml = ContentType passed to Request header as '{0}'. Treat response as HTML, content directly pick from result.
        ConvertBase64String = Convert base64 string to UTF8 string.
        ConvertstringToJsonAndGetContent = Convert json string to PSCustomObject, then picking up base64 encoded string from content property.
        DownloadComplete = Download content complete.
        GetRawContent = Getting raw content from response result
        IsDestinationPathExist = Checking Destination Path is existing and Valid as a FileInfo
        IsDestinationPathAlreadyUpToDate = Matching FileHash to verify file is already exist/Up-To-Date or not.
        IsFileAlreadyUpToDate = CurrentFileHash : CachedFileHash -> {0} : {1}
        IsFileExists = File found from DestinationPath. Checking already up-to-date.
        ItemTypeWasFile = Destination Path found as File : '{0}'
        ItemTypeWasDirectory = Destination Path found but was Directory : '{0}'
        ItemTypeWasOther = Destination Path found but was neither File nor Directory: '{0}'
        ItemTypeWasNotExists = Destination Path not found : '{0}'
        SetCacheLocationPath = CacheLocation Value detected. Setting Custom CacheLocation Path : '{0}'
        TestUriConnection = Testing connection to the URI : {0}
        UpdateFileHashCache = Updating cache path '{1}' for current File hash SHA256 '{0}'.
        ValidateUri = Cast URI string '{0}' to System.Uri.
        ValidateFilePath = Check DestinationPath '{0}' is FileInfo and Parent Directory already exist.
        WriteStream = Start writing downloaded string to File Path : '{0}'
    "

}

$verboseMessage = DATA {
    ConvertFrom-StringData -StringData "
        alreadyUpToDate = Current DestinationPath FileHash and Cache FileHash matched. File already Up-To-Date.
        DownloadStream = Status Code returns '{0}'. Start download stream from URI : '{1}'
        DownloadString = Status Code returns '{0}'. Start download string from URI : '{1}'
        notUpToDate = Current DestinationPath FileHash and Cache FileHash not matched. Need to download latest file.
    "

}
$exceptionMessage = DATA {
    ConvertFrom-StringData -StringData "
        ContentNotFoundFromResponce = Content not exist in GitHub API response json string.
        ResultNotFountFromResponce = Result not exist in GitHub API response raw string.
        InvalidCastURI = Uri : '{0}' casted to [System.Uri] but was invalid string for URI. Make sure you have passed valid URI string.
        InvalidUriSchema = Specified URI is not valid: '{0}'. Only https is accepted.
        InvalidResponce = Status Code returns '{0}'. Stop download content from URI : '{1}'
        DestinationPathAlreadyExistAsNotFile = Destination Path '{0}' already exist but not a file. Found itemType is {1}. Windows not allowed exist same name item.
    "

}

#endregion

#region *-TargetResource

function Get-TargetResource {
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$DestinationPath,

        [parameter(Mandatory = $true)]
        [System.String]$Repository,

        [parameter(Mandatory = $true)]
        [System.String]$RepositoryOwner,

        [parameter(Mandatory = $true)]
        [System.String]$ContentPath,

        [parameter(Mandatory = $false)]
        [System.String]$Branch = "master",

        [parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]$OAuth2Token = [PSCredential]::Empty,

        [parameter(Mandatory = $false)]
        [Microsoft.Management.Infrastructure.CimInstance[]]$Header = $null,

        [parameter(Mandatory = $false)]
        [ValidateSet("application/json", "application/vnd.github+json", "application/vnd.github.v3.raw", "application/vnd.github.v3.html")]
        [System.String]$ContentType = "application/json",

        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [System.String]$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer,

        [parameter(Mandatory = $false)]
        [System.Boolean]$AllowRedirect = $true,

        [parameter(Mandatory = $false)]
        [System.String]$CacheLocation = [string]::Empty
    )

    # Set Custom Cache Location
    if ($CacheLocation -ne [string]::Empty) {
        Write-Debug -Message ($debugMessage.SetCacheLocationPath -f $CacheLocation)
        $script:cacheLocation = $CacheLocation
    }

    # Setup GitHub API Uri string from GitHub parameters
    $uri = ParseGitHubApiUri -RepositoryOwner $RepositoryOwner -Repository $Repository -ContentPath $ContentPath -Branch $Branch

    # validate Uri can be parse to [URI] and Schema is http|https|file
    $validUri = ValidateUri -Uri $uri

    # Initialize return values
    # Header and OAuth2Token will never return as TypeConversion problem
    $returnHash = 
    @{
        DestinationPath = $DestinationPath
        Repository = $Repository
        RepositoryOwner = $RepositoryOwner
        ContentPath = $ContentPath
        Branch = $Branch
        ContentType = $ContentType
        UserAgent = $UserAgent
        AllowRedirect = $AllowRedirect
        OAuth2Token = New-CimInstance -ClassName MSFT_Credential -Property @{Username = [string]$OAuth2Token.UserName; Password = [string]$null} -Namespace root/microsoft/windows/desiredstateconfiguration -ClientOnly
        CacheLocation = $CacheLocation
        Ensure = "Absent"
    }

    # Destination Path check
    Write-Debug -Message $debugMessage.IsDestinationPathExist
    $itemType = GetPathItemType -Path $DestinationPath

    $fileExists = $false
    switch ($itemType.ToString()) {
        ([GraniDonwloadItemTypeEx]::FileInfo.ToString()) {
            Write-Debug -Message ($debugMessage.ItemTypeWasFile -f $DestinationPath)
            $fileExists = $true
        }
        ([GraniDonwloadItemTypeEx]::DirectoryInfo.ToString()) {
            Write-Debug -Message ($debugMessage.ItemTypeWasDirectory -f $DestinationPath)
        }
        ([GraniDonwloadItemTypeEx]::Other.ToString()) {
            Write-Debug -Message ($debugMessage.ItemTypeWasOther -f $DestinationPath)
        }
        ([GraniDonwloadItemTypeEx]::NotExists.ToString()) {
            Write-Debug -Message ($debugMessage.ItemTypeWasNotExists -f $DestinationPath)
        }
    }

    # Already Up-to-date Check
    Write-Debug -Message $debugMessage.IsDestinationPathAlreadyUpToDate
    if ($fileExists -eq $true) {
        Write-Debug -Message $debugMessage.IsFileExists
        $currentFileHash = GetFileHash -Path $DestinationPath
        $cachedFileHash = GetCache -DestinationPath $DestinationPath -Uri $validUri

        Write-Debug -Message ($debugMessage.IsFileAlreadyUpToDate -f $currentFileHash, $cachedFileHash)
        if ($currentFileHash -eq $cachedFileHash) {
            Write-Verbose -Message $verboseMessage.alreadyUpToDate
            $returnHash.Ensure = "Present"
        }
        else {
            Write-Verbose -Message $verboseMessage.notUpToDate
        }
    }

    return $returnHash
}


function Set-TargetResource {
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$DestinationPath,

        [parameter(Mandatory = $true)]
        [System.String]$Repository,

        [parameter(Mandatory = $true)]
        [System.String]$RepositoryOwner,

        [parameter(Mandatory = $true)]
        [System.String]$ContentPath,

        [parameter(Mandatory = $false)]
        [System.String]$Branch = "master",

        [parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]$OAuth2Token = [PSCredential]::Empty,

        [parameter(Mandatory = $false)]
        [Microsoft.Management.Infrastructure.CimInstance[]]$Header = $null,

        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("application/json", "application/vnd.github+json", "application/vnd.github.v3.raw", "application/vnd.github.v3.html")]
        [System.String]$ContentType = "application/json",

        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [System.String]$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer,

        [parameter(Mandatory = $false)]
        [System.Boolean]$AllowRedirect = $true,

        [parameter(Mandatory = $false)]
        [System.String]$CacheLocation = [string]::Empty
    )

    # Set Custom Cache Location
    if ($CacheLocation -ne [string]::Empty) {
        Write-Debug -Message ($debugMessage.SetCacheLocationPath -f $CacheLocation)
        $script:cacheLocation = $CacheLocation
    }

    # Setup GitHub API Uri string from GitHub parameters
    $uri = ParseGitHubApiUri -Repository $Repository -RepositoryOwner $RepositoryOwner -ContentPath $ContentPath -Branch $Branch

    # validate Uri can be parse to [URI] and Schema is http|https|file
    $validUri = ValidateUri -Uri $Uri

    # validate DestinationPath is valid
    ValidateFilePath -Path $DestinationPath

    # Convert CimInstance to HashTable
    $headerHashtable = ConvertKCimInstanceToHashtable -CimInstance $Header

    # Start Download
    Invoke-HttpClient -Uri $validUri -Path $DestinationPath -Header $headerHashtable -ContentType $ContentType -UserAgent $UserAgent -OAuth2Token $OAuth2Token -AllowRedirect $AllowRedirect

    # Update Cache for FileHash
    UpdateCache -DestinationPath $DestinationPath -Uri $validUri
}


function Test-TargetResource {
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$DestinationPath,

        [parameter(Mandatory = $true)]
        [System.String]$Repository,

        [parameter(Mandatory = $true)]
        [System.String]$RepositoryOwner,

        [parameter(Mandatory = $true)]
        [System.String]$ContentPath,

        [parameter(Mandatory = $false)]
        [System.String]$Branch = "master",

        [parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]$OAuth2Token = [PSCredential]::Empty,

        [parameter(Mandatory = $false)]
        [Microsoft.Management.Infrastructure.CimInstance[]]$Header = $null,

        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("application/json", "application/vnd.github+json", "application/vnd.github.v3.raw", "application/vnd.github.v3.html")]
        [System.String]$ContentType = "application/json",

        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [System.String]$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer,

        [parameter(Mandatory = $false)]
        [System.Boolean]$AllowRedirect = $true,

        [parameter(Mandatory = $false)]
        [System.String]$CacheLocation = [string]::Empty
    )

    $param = @{
        DestinationPath = $DestinationPath
        RepositoryOwner = $RepositoryOwner
        Repository = $Repository
        ContentPath = $ContentPath
        Branch = $Branch
        OAuth2Token = $OAuth2Token
        Header = $Header
        ContentType = $ContentType
        UserAgent = $UserAgent
        AllowRedirect = $AllowRedirect
        CacheLocation = $CacheLocation
    }
    return (Get-TargetResource @param).Ensure -eq "Present"
}


#endregion

#region HttpClient Helper

function Invoke-HttpClient {
    [OutputType([void])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [uri]$Uri,

        [parameter(Mandatory = $true)]
        [string]$Path,

        [parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]$OAuth2Token = [PSCredential]::Empty,

        [parameter(Mandatory = $false)]
        [System.Collections.Hashtable]$Header = @{},

        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("application/json", "application/vnd.github+json", "application/vnd.github.v3.raw", "application/vnd.github.v3.html")]
        [System.String]$ContentType = "application/json",

        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [System.String]$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer,

        [parameter(Mandatory = $false)]
        [System.Boolean]$AllowRedirect = $true
    )

    begin {
        #region Initialize

        # Should support Timeout? : Default -> 1:40 min
        # Should support MaxResponseContentBufferSize? : Default -> 2147483647

        $httpClientHandler = New-Object System.Net.Http.HttpClientHandler
        $httpClientHandler.AllowAutoRedirect = $AllowRedirect
        
        $httpClient = New-Object System.Net.Http.HttpClient ($httpClientHandler)

        # Request Header
        if ($Header.Keys.Count -ne 0) {
            foreach ($item in $Header.GetEnumerator()) {
                Write-Debug -Message ($debugMessage.AddRequestHeader -f $item.Key, $item.Value)
                $httpClient.DefaultRequestHeaders.Add($item.Key, $item.Value)
            }   
        }

        # Request Header : Keep-Alive
        if (($httpClient.DefaultRequestHeaders.GetEnumerator() | where Key -eq "Keep-Alive" | measure).Count -eq 0) {
            Write-Debug -Message ($debugMessage.AddKeepAliveToRequestHeader)
            $httpClient.DefaultRequestHeaders.Add("Keep-Alive", "true")
        }

        # ContentType
        if ($ContentType -ne [string]::Empty) {
            Write-Debug -Message ($debugMessage.AddContentType -f $ContentType)
            $private:mediaType = New-Object System.Net.Http.Headers.MediaTypeWithQualityHeaderValue($ContentType)
            $httpClient.DefaultRequestHeaders.Accept.Add($mediaType)
        }

        # UserAgent
        if ($UserAgent -ne [string]::Empty) {
            Write-Debug -Message ($debugMessage.AddUserAgent -f $UserAgent)
            $httpClient.DefaultRequestHeaders.UserAgent.ParseAdd($UserAgent)
        }

        # Credential
        if ($OAuth2Token.GetNetworkCredential().Password -ne [string]::Empty) {
            # Credential on Handler does not work with Basic Authentication : http://stackoverflow.com/questions/25761214/why-would-my-rest-service-net-clients-send-every-request-without-authentication
            # $httpClientHandler.Credential = $Credential

            $private:authorizationHeaderKey = "Authorization"
            $private:authorizationHeaderValue = "token {0}" -f $OAuth2Token.GetNetworkCredential().Password

            Write-Debug -Message ($debugMessage.AddOAuth2Token)
            $httpClient.DefaultRequestHeaders.Add($private:authorizationHeaderKey, $private:authorizationHeaderValue) # Basic Authentication Only
        }

        #endregion
    }

    end {
        try {
            #region Test Connection

            Write-Debug -Message ($debugMessage.TestUriConnection -f $Uri.ToString())
            $res = $httpClient.GetAsync($Uri)
            $res.ConfigureAwait($false) > $null
            if ($res.Exception -ne $null) { throw $res.Exception }
            if ($res.Result.StatusCode -ne [System.Net.HttpStatusCode]::OK) { throw ($exceptionMessage.InvalidResponce -f $res.Result.StatusCode.value__, $Uri) }
            
            #endregion

            #region Execute Download

            switch ($ContentType) {
                "application/json" {
                    Write-Verbose -Message ($verboseMessage.DownloadString -f $res.Result.StatusCode.value__, $Uri)
                    [System.Threading.Tasks.Task`1[string]]$requestResult = GetStringAsync -Uri $Uri

                    Write-Debug -Message $debugMessage.ContentTypeDetectedAsJson
                    WriteJson -Path $Path -RequestResult $requestResult
                }
                "application/vnd.github+json" {
                    Write-Verbose -Message ($verboseMessage.DownloadString -f $res.Result.StatusCode.value__, $Uri)
                    [System.Threading.Tasks.Task`1[string]]$requestResult = GetStringAsync -Uri $Uri
                    
                    Write-Debug -Message $debugMessage.ContentTypeDetectedAsJson
                    WriteJson -Path $Path -RequestResult $requestResult
                }
                "application/vnd.github.v3.raw" {
                    Write-Verbose -Message ($verboseMessage.DownloadStream -f $res.Result.StatusCode.value__, $Uri)
                    [System.Threading.Tasks.Task`1[System.IO.Stream]]$stream = GetStreamAsync -Uri $Uri

                    Write-Debug -Message $debugMessage.ContentTypeDetectedAsRaw
                    WriteStream -Path $Path -Stream $stream
                }
                "application/vnd.github.v3.html" {
                    Write-Verbose -Message ($verboseMessage.DownloadStream -f $res.Result.StatusCode.value__, $Uri)
                    [System.Threading.Tasks.Task`1[System.IO.Stream]]$stream = GetStreamAsync -Uri $Uri

                    Write-Debug -Message $debugMessage.ContentTypeDetectedAsHtml
                    WriteStream -Path $Path -Stream $stream
                }
            }
                        
            #endregion
            
            Write-Verbose -Message ($debugMessage.DownloadComplete)
        }
        catch [System.Exception] {
            throw $_
        }
        finally {
            if (($null -ne $res) -and ($res.IsCompleted -eq $true)) { $res.Dispose() }
            if ($null -ne $httpClient) { $httpClient.Dispose() }
            if ($null -ne $httpClientHandler) { $httpClientHandler.Dispose() }
        }
    }
}

function GetStringAsync {
    [OutputType([System.Threading.Tasks.Task`1[string]])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [uri]$Uri
    )

    [System.Threading.Tasks.Task`1[string]]$requestResult = $httpClient.GetStringAsync($Uri)
    $requestResult.ConfigureAwait($false) > $null
    if ($requestResult.Exception -ne $null) { throw $requestResult.Exception }
    return $requestResult
}

function GetStreamAsync {
    [OutputType([System.Threading.Tasks.Task`1[System.IO.Stream]])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [uri]$Uri
    )

    [System.Threading.Tasks.Task`1[System.IO.Stream]]$stream = $httpClient.GetStreamAsync($Uri)
    $stream.ConfigureAwait($false) > $null
    if ($stream.Exception -ne $null) { throw $stream.Exception }
    return $stream
}

function WriteJson {
    [OutputType([void])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$Path,

        [parameter(Mandatory = $true)]
        [System.Threading.Tasks.Task`1[string]]$RequestResult
    )

    begin {
        function GetContentBase64String {
            [OutputType([string])]
            [CmdletBinding()]
            param
            (
                [parameter(Mandatory = $true)]
                [System.Threading.Tasks.Task`1[string]]$RequestResult
            )

            # Get Content base64 string
            Write-Debug -Message $debugMessage.ConvertstringToJsonAndGetContent
            $content = ($RequestResult.Result | ConvertFrom-Json).Content

            if ($content -eq [string]::Empty) { throw New-Object System.NullReferenceException $exceptionMessage.ContentNotFoundFromResponce }
            return $content
        }

        function ConvertFromBase64ToUTF8 {
            [OutputType([string])]
            [CmdletBinding()]
            param
            (
                [parameter(Mandatory = $true)]
                [string]$String
            )

            try {
                # convert bse64 to UTF8
                Write-Debug -Message $debugMessage.ConvertBase64String
                $utf8String = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($String))
                return $utf8String
            }
            catch {
                throw $_
            }
        }
    }

    process {
        try {
            # Get base64 string from Content Property of response json
            $content = GetContentBase64String -RequestResult $RequestResult
        
            # decode base64 to utf8 string
            $decodedString = ConvertFromBase64ToUTF8 -String $Content

            # Write content to the file.
            [System.IO.File]::WriteAllText($Path, $decodedString, [System.Text.Encoding]::UTF8)
            
        }
        finally {
            if (($null -ne $RequestResult) -and ($RequestResult.IsCompleted -eq $true)) { $RequestResult.Dispose() }
        }
    }
}

function WriteStream {
    [OutputType([void])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$Path,

        [parameter(Mandatory = $true)]
        [System.Threading.Tasks.Task`1[System.IO.Stream]]$Stream
    )

    try {
        # Write stream to the File
        Write-Debug -Message ($debugMessage.WriteStream -f $Path)
        $fileStream = [System.IO.File]::Create($Path)
        $Stream.Result.CopyTo($fileStream)
    }
    finally {
        if ($null -ne $fileStream) { $fileStream.Dispose() }
        if (($null -ne $Stream) -and ($Stream.IsCompleted -eq $true)) { $Stream.Dispose() }
    }
}

#endregion

#region Parse Helper

function ParseGitHubApiUri {
    [OutputType([string])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$RepositoryOwner,

        [parameter(Mandatory = $true)]
        [System.String]$Repository,

        [parameter(Mandatory = $true)]
        [System.String]$ContentPath,

        [parameter(Mandatory = $false)]
        [System.String]$Branch = "master"
    )

    $uriString = $script:githubApiString -f $RepositoryOwner, $Repository, $ContentPath, $Branch
    return $uriString
}

#endregion

#region Validation Helper

function ValidateUri {
    [OutputType([uri])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$Uri
    )
    
    Write-Debug -Message ($debugMessage.ValidateUri -f $Uri)
    [uri]$result = $Uri -as [uri]
    if ($result.AbsolutePath -eq $null) { throw New-Object System.NullReferenceException ($exceptionMessage.InvalidCastURI -f $Uri)}
    if ($result.Scheme -ne "https") {
        $errorId = "UriValidationFailure";
        $errorMessage = $exceptionMessage.InvalidUriSchema -f ${Uri}
        ThrowInvalidDataException -ErrorId $errorId -ErrorMessage $errorMessage
    }
    return $result
}

function ValidateFilePath {
    [OutputType([Void])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$Path
    )
    
    Write-Debug -Message ($debugMessage.ValidateFilePath -f $Path)
    $itemType = GetPathItemType -Path $Path
    switch ($itemType.ToString()) {
        ([GraniDonwloadItemTypeEx]::FileInfo.ToString()) {
            return;
        }
        ([GraniDonwloadItemTypeEx]::NotExists.ToString()) {
            # Create Parent Directory check
            $parentPath = Split-Path $Path -Parent
            if (-not (Test-Path -Path $parentPath)) {
                [System.IO.Directory]::CreateDirectory($parentPath) > $null
            }
        }
        Default {
            $errorId = "FileValidationFailure"
            $errorMessage = $exceptionMessage.DestinationPathAlreadyExistAsNotFile -f $Path, $itemType.ToString()
            ThrowInvalidDataException -ErrorId $errorId -ErrorMessage $errorMessage
        }
    }

}

#endregion

#region Cache Helper

function GetFileHash {
    [OutputType([string])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$Path
    )

    return (Get-FileHash -Path $Path -Algorithm SHA256).Hash
}

function GetCacheKey {
    [OutputType([string])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$DestinationPath,

        [parameter(Mandatory = $true)]
        [uri]$Uri
    )

    $key = [string]::Join("", @($DestinationPath, $Uri.AbsoluteUri.ToString())).GetHashCode().ToString()
    return $key
}

function GetCache {
    [OutputType([string])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$DestinationPath,

        [parameter(Mandatory = $true)]
        [uri]$Uri
    )

    $cacheKey = GetCacheKey -DestinationPath $DestinationPath -Uri $Uri
    $path = Join-Path $script:cacheLocation $cacheKey
    
    # Test Cache Path is exist
    if (-not (Test-Path -Path $path)) { return [string]::Empty }

    # Get FileHash from Cache File
    $fileHash = (Import-CliXml -Path $path).FileHash
    return $fileHash    
}

function UpdateCache {
    [OutputType([void])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$DestinationPath,

        [parameter(Mandatory = $true)]
        [uri]$Uri
    )

    $cacheKey = GetCacheKey -DestinationPath $DestinationPath -Uri $Uri
    $path = Join-Path $script:cacheLocation $cacheKey

    # create cacheLocaltion Directory
    if (-not (Test-Path -Path $script:cacheLocation)) {
        [System.IO.Directory]::CreateDirectory($script:cacheLocation) > $null
    }

    # Create Cache Object
    $fileHash = GetFileHash -Path $DestinationPath
    $obj = NewXmlObject -DestinationPath $DestinationPath -Uri $Uri -FileHash $fileHash

    # export cache to CliXML
    Write-Debug ($debugMessage.UpdateFileHashCache -f $fileHash, $Path)
    $obj | Export-CliXml -Path $path -Force
}

function NewXmlObject {
    [OutputType([PSCustomObject])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [string]$DestinationPath,

        [parameter(Mandatory = $true)]
        [uri]$Uri,

        [parameter(Mandatory = $true)]
        [string]$FileHash
    )
    
    $obj = @{}
    $obj.FileHash = $FileHash
    $obj.WriteTime = [System.IO.File]::GetLastWriteTimeUtc($DestinationPath)
    $obj.Path = $DestinationPath
    $obj.Uri = $Uri.AbsoluteUri.ToString()
    return [PSCustomObject]$obj
}

#endregion

#region ItemType Helper

function GetPathItemType {
    [OutputType([GraniDonwloadItemTypeEx])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("FullName", "LiteralPath", "PSPath")]
        [System.String]$Path = [string]::Empty
    )

    $type = [string]::Empty

    # Check type of the Path Item
    if (-not (Test-Path -Path $Path)) {
        return [GraniDonwloadItemTypeEx]::NotExists
    }
    
    $pathItem = Get-Item -Path $path
    $pathItemType = $pathItem.GetType().FullName
    $type = switch ($pathItemType) {
        "System.IO.FileInfo" {
            [GraniDonwloadItemTypeEx]::FileInfo
        }
        "System.IO.DirectoryInfo" {
            [GraniDonwloadItemTypeEx]::DirectoryInfo
        }
        Default {
            [GraniDonwloadItemTypeEx]::Other
        }
    }

    return $type
}

#endregion

#region Converter from Microsoft.Management.Infrastructure.CimInstance[] (KeyValuePair) to HashTable

function ConvertKCimInstanceToHashtable {
    [OutputType([hashtable[]])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $false)]
        [AllowNull()]
        [Microsoft.Management.Infrastructure.CimInstance[]]$CimInstance
    )

    if ($null -eq $CimInstance) {
        return @{}
    }

    $hashtable = New-Object System.Collections.Generic.List[hashtable]
    foreach ($item in $CimInstance.GetEnumerator()) {
        $hashtable.Add(@{$item.Key = $item.Value})
    }

    return $hashtable
}

#endregion

#region Exception Helper

function ThrowInvalidDataException {
    [OutputType([Void])]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]$ErrorId,

        [parameter(Mandatory = $true)]
        [System.String]$ErrorMessage
    )
    
    $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidData
    $exception = New-Object System.InvalidOperationException $ErrorMessage 
    $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $ErrorId, $errorCategory, $null
    throw $errorRecord
}

#endregion

Export-ModuleMember -Function *-TargetResource