Public/Get-RyverFileInfo.ps1

function Get-RyverFileInfo {
    <#
        .SYNOPSIS
        Get Ryver file info
 
        .DESCRIPTION
        Get Ryver file info
 
        We query the first 100 files unless you specify -Paging
 
        .INPUTS
        System.Management.Automation.PSCredential
 
        .NOTES
        - Troy Lindsay
        - Twitter: @troylindsay42
        - GitHub: tlindsay42
 
        .EXAMPLE
        Get-RyverFileInfo
        Lists up to 100 files
 
        .EXAMPLE
        Get-RyverFileInfo -Paging
        Lists all files, querying 100 at a time
 
        .EXAMPLE
        Get-RyverFileInfo -User wframe -Type csv -Paging
        Lists all CSV files uploaded by wframe
 
        .EXAMPLE
        Get-RyverFileInfo -User wframe -Channel C58AHBEPJ
        Lists up to 100 files from channel C58AHBEPJ
 
        .EXAMPLE
        Get-RyverFileInfo -Before (Get-Date).AddDays(-7) -After (Get-Date).AddDays(-14) -Paging
        Get all files from a week ago
 
        .FUNCTIONALITY
        Ryver
    #>

    [CmdletBinding(
        HelpUri = 'https://tlindsay42.github.io/PSRyver/Public/Get-RyverFileInfo/',
        DefaultParameterSetName = 'Default'
    )]
    param (
        # If specified, search for files in this channel (ID).
        [Parameter(
            ParameterSetName = 'Default',
            Position = 0
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 0
        )]
        [ValidateNotNullOrEmpty()]
        [String]
        $Forum,

        # If specified, search for files created before this date.
        [Parameter(
            ParameterSetName = 'Default',
            Position = 1
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 1
        )]
        [ValidateNotNull()]
        [DateTime]
        $Before,

        # If specified, search for files created after this date.
        [Parameter(
            ParameterSetName = 'Default',
            Position = 2
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 2
        )]
        [ValidateNotNull()]
        [DateTime]
        $After,

        # If specified, search for files by this user
        [Parameter(
            ParameterSetName = 'Default',
            Position = 3
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 3
        )]
        [ValidateNotNullOrEmpty()]
        [String]
        $User,

        <#
        If specified, search for files of this type:
        - all: All files
        - spaces: Posts
        - snippets: Snippets
        - images: Image files
        - videos: Video files
        - gdocs: Google docs
        - zips: Zip files
        - pdfs: PDF files
        #>

        [Parameter(
            ParameterSetName = 'Default',
            Position = 4
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 4
        )]
        [ValidateSet(
            'all',
            'spaces',
            'snippets',
            'images',
            'videos',
            'gdocs',
            'zips',
            'pdfs'
        )]
        [String[]]
        $Types,

        # Number of messages to return per query.
        [Parameter(
            ParameterSetName = 'Default',
            Position = 5
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 5
        )]
        [ValidateRange( 1, 1000 )]
        [UInt16]
        $Count = 100,

        <#
        If specified, and more data is available, continue querying Ryver until we
        have retrieved all the data available.
        #>

        [Parameter(
            ParameterSetName = 'Paging',
            Position = 6
        )]
        [Switch]
        $Paging,

        # Limit the count of API queries to this number.
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 7
        )]
        [ValidateNotNull()]
        [UInt16]
        $MaxQueries = [UInt16]::MaxValue,

        # Return raw output. If specified, Name parameter is ignored
        [Parameter(
            ParameterSetName = 'Default',
            Position = 6
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 8
        )]
        [ValidateNotNull()]
        [Switch]
        $Raw,

        <#
        Credentials to use for the Ryver API.
 
        Default value is the value set by Set-PSRyverConfig.
        #>

        [Parameter(
            ParameterSetName = 'Default',
            Position = 7
        )]
        [Parameter(
            ParameterSetName = 'Paging',
            Position = 9
        )]
        [PSCredential]
        $Credential
    )

    begin {
        $function = $MyInvocation.MyCommand.Name

        Write-Verbose -Message (
            "Beginning: '${function}' with ParameterSetName '$( $PSCmdlet.ParameterSetName )' and Parameters: " +
            ( $PSBoundParameters | Remove-SensitiveData | Format-Table -AutoSize | Out-String )
        )

        if ( $PSBoundParameters.ContainsKey( 'Credential' ) ) {
            $Script:PSRyver.Authorization = ConvertTo-Authorization -Credential $Credential
            Remove-Variable -Name 'Credential'
        }
    }

    process {
        #region init
        $beforeTS = $null
        $afterTS = $null
        $queries = 1
        $hasMore = $false

        $body = @{
            count = $Count
        }
        #endregion

        if ( $PSBoundParameters.ContainsKey( 'User' ) ) {
            $userMap = $null

            if ( $Script:PSRyverUserMap.ContainsKey( $User ) ) {
                $userMap = $Script:PSRyverUserMap[$User]
            }
            else {
                $map = Get-RyverUserMap -Update

                if ( $map.ContainsKey( $User ) ) {
                    $userMap = $map[$User]
                }
                else {
                    Write-Warning -Message "Could not find user '${User}'. Check 'Get-RyverUserMap' for valid names."
                }
            }

            if ( $userMap ) {
                $body.Add( 'user', $userMap )
            }
        }

        if ( $PSBoundParameters.ContainsKey( 'Forum' ) ) {
            $body.Add( 'forum', $Forum )
        }

        if ( $PSBoundParameters.ContainsKey( 'Types' ) ) {
            $body.Add( 'types', $( $Types -join ',' ) )
        }

        if ( $PSBoundParameters.ContainsKey( 'Before' ) ) {
            $beforeTS = ConvertTo-UnixTime -Date $Before
            $body.Add( 'ts_to', $beforeTS )
        }

        if ( $PSBoundParameters.ContainsKey( 'After' ) ) {
            $afterTS = ConvertTo-UnixTime -Date $After
            $body.Add( 'ts_from', $afterTS )
        }

        $splat = @{
            Method = 'files.list'
            Body   = $body
        }

        do {
            $response = Invoke-RyverRestMethod @splat

            $response |
                Format-List -Property '*' |
                Out-String |
                Write-Debug

            if ( $response.OK ) {
                if ( $PSCmdlet.ParameterSetName -eq 'Paging' -and $response.Paging.Page -lt $response.Paging.Pages ) {
                    Write-Debug 'Paging engaged!'
                    $hasMore = $true
                    $splat.Body.Page = 1 + $response.Paging.Page
                }
                elseif ( $PSCmdlet.ParameterSetName -eq 'Paging' -and $response.Paging.Page -like $response.Paging.Pages ) {
                    $hasMore = $false
                }
                else {
                    # Might need this case later - is paging always included? Is this an error?
                    $hasMore = $false
                }
                if ( $Raw ) {
                    $response
                }
                else {
                    Format-RyverV1FileObject -InputObject $response
                }
            }
            else {
                $response
            }

            $queries++
        }
        until (
            -not $Paging -or
            -not $hasMore -or
            ( $MaxQueries -and $queries -gt $MaxQueries)
        )
    }

    end {
        Write-Verbose -Message "Ending: '${function}'."
    }
}