Public/Get-SignDocument.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
<#
.SYNOPSIS
Gets document information or downloads document
 
.DESCRIPTION
Gets document information or downloads document to specified path
#>

function Get-SignDocument {
    [CmdletBinding(DefaultParameterSetName='Search')]

    param (
        # id of document
        [Parameter(Mandatory=$true, ParameterSetName='Get by id')]
        [Parameter(Mandatory=$true, ParameterSetName='Download')]
        [String]
        $id,
        # id of request
        [Parameter(Mandatory=$true, ParameterSetName='Get by request')]
        [String]
        $request,

        # download document by id or all documents from request
        [Parameter(Mandatory=$true, ParameterSetName='Download')]
        [Parameter(Mandatory=$false, ParameterSetName='Get by request')]
        [Switch]
        $download,

        # Path to save document(s) to
        [Parameter(Mandatory=$false, ParameterSetName='Download')]
        [Parameter(Mandatory=$false, ParameterSetName='Get by request')]
        [String]
        $Path = '.',

        # Keywords
        [Parameter(Mandatory=$false, ParameterSetName='Search')]
        [String]
        $keywords
    )

    begin {
        $api = '/document'
        if ($id) { $api = "$api/$id"}

        $Body = . Get-ParameterValue -Parameters $MyInvocation.MyCommand.Parameters -BoundParameters $PSBoundParameters -DefaultExcludeParameter 'id', 'download', 'request', 'Path', 'FileName','Debug', 'Verbose'
        if ($request) {
            $api = "/request/$request"
            $body = @{ "mode" = 0
                        "status" = 0
                    }
        }

    }

    process {
        #get document metadata results
        $result = Invoke-SignApi -api $api -method GET -body $Body

        if ($result.documents) {
            $result = $result.documents
        } elseif ($result.document) {
            $result= $result.document
        } elseif ($result.request.documents) {
            $result= $result.request.documents
        }

        if ($download) {
            #download and save files
            foreach ($doc in $result) {
                Write-Verbose "Downloading file id: $($doc.id) , title : $($doc.title) to path : $path"
                $fullfilename = Join-Path -Path $Path -ChildPath $doc.title
                Write-Verbose "Downloading file id: $($doc.id) , title : $($doc.title) to path : $fullfilename"

                Invoke-SignApi -api "/document/$($doc.id)/download" -method GET | Out-File -FilePath $fullfilename

            }
        } else {
            $result
        }
    }

    end {

    }
}