Private/Helper/Format-BDUri.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
function Format-BDUri {
    [OutputType([string])]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)] $uri,
        [Parameter(Mandatory = $false)] [hashtable] $IsExactly,
        [Parameter(Mandatory = $false)] [hashtable] $StartsWith,
        [Parameter(Mandatory = $false)] [hashtable] $Contains,
        [Parameter(Mandatory = $false)] [array] $Select,
        [Parameter(Mandatory = $false)] [int] $Top,
        [Parameter(Mandatory = $false)] [int] $Skip,
        [Parameter(Mandatory = $false)] [string] $CustomFilter,

        [switch] $AllRecords
    )

    process {
        #build filter
        if($CustomFilter) {
            $_Filter = '&$filter=' + $CustomFilter
        } else {
            if($IsExactly -or $StartsWith -or $Contains){
                $_Filter = New-BDFilter -IsExactly $IsExactly -StartsWith $StartsWith -Contains $Contains
            }
        }
        #build select
        if($Select){
            $_Select = '&$select=' + [System.Web.HttpUtility]::UrlEncode($Select -join ",")
        }
        #add top
        if(!$AllRecords){
            if($Top){
                $_Top = '&$top=' + "$Top"
            }
        } else {
            $_Top = '&$top=' +  999
        }
        #add skip
        if($Skip){
            $_Skip = '&$skip=' + "$skip"
        }
    }
    end {
        $uriParams = "$_Filter$_Select$_Top$_Skip"
        Return ("$Uri" + ($uriParams -replace '^&','?' ))
    }
}