Scripts/Get-CoinbaseProductStat.ps1


Function Get-CoinbaseProductStat {

    <#
 
    .SYNOPSIS
 
    Gets 30 day and 24 hour stats for a product.
 
    .DESCRIPTION
 
    Gets 30 day and 24 hour stats for a product.
 
    .PARAMETER Product
 
    Product Id
 
    .EXAMPLE
 
    Get-CoinbaseProductStat -Product BTC-USD
 
    .EXAMPLE
 
    Get-CoinbaseProductStat -Product BTC-USD, ETH-USD
 
    .NOTES
 
    .LINK
 
    #>


    [CmdletBinding ()]

    Param ()

    DynamicParam {

        $ParameterNamePair = 'Product'
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.Mandatory = $True
        $ParameterAttribute.HelpMessage = 'Enter currency'
        $ParameterAttribute.Position = 0
        $AttributeCollection.Add($ParameterAttribute)
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $ArraySet = Get-Content -Path ("$env:ALLUSERSPROFILE\Coinbase\Pairs.txt" )
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ArraySet)
        $AttributeCollection.Add($ValidateSetAttribute)
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterNamePair, [String[]], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterNamePair, $RuntimeParameter)

        Return $RuntimeParameterDictionary

    }

    BEGIN {

        $Products = $PSBoundParameters[$ParameterNamePair]

        Function Show-Output ($Values) {

            [PSCustomObject]@{

                Name = $Coin.ToUpper()
                Open = $Values.Open
                High = $Values.High
                Low = $Values.Low
                Last = $Values.Last
                Volume = $Values.Volume
                Volume30Day = $Values.Volume_30Day
                RFQVolume24Hour = $Values.RFQ_Volume_24hour
                RFQVolume30Day = $Values.RFQ_Volume_30Day
                Status = $Status

            }

        }

    }

    PROCESS {

        Try {

            $Headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]'
            $Headers.Add('Content-Type', 'application/json')

            ForEach ($Coin In $Products) {

                $Response = Invoke-RestMethod "https://api.exchange.coinbase.com/products/$Coin/stats" -Method 'GET' -Headers $Headers

                ForEach ($Item In $Response) {

                    $Status = 'Ok'

                    Show-Output ($Item)

                }

                Start-Sleep -Milliseconds 100
            }

        }

        Catch {

            $Status = $PSItem.Exception.Message

            Show-Output ('')

        }

    }

    END {}

}