KpPwpush.psm1

#Region './public/Connect-Pwpush.ps1' 0
function Connect-Pwpush
{
    param(
        [string]$Server = "https://pwpush.com/"
    )

    if ($Server[-1] -eq "/")
    {
        $Server = $server.Substring(0, $Server.Length - 1) 
    }

    $Script:Connection = @{
        Server = $server
    }
}
#EndRegion './public/Connect-Pwpush.ps1' 16
#Region './public/Get-Pwpush.ps1' 0
function Get-Pwpush
{
    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )
    
    $endpoint = "p/$($UrlToken).json"

    $params = @{
        Endpoint = $endpoint 
        Method   = "Get" 
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Get-Pwpush.ps1' 17
#Region './public/Get-PwpushPreview.ps1' 0
function Get-PwpushPreview
{
    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )
    
    $endpoint = "p/$($UrlToken)/preview.json"

    $params = @{
        Endpoint = $endpoint 
        Method   = "Get" 
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Get-PwpushPreview.ps1' 17
#Region './public/Invoke-PwpushRequest.ps1' 0
function Invoke-PwpushRequest
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Endpoint,

        [object]$Body,

        [ValidateSet("Get", "Post", "Delete")]
        [string]$Method = "Get",

        [switch]$ReturnRaw
    )

    if ( -not ($Script:Connection))
    {
        Throw "Please connect to Pwpush via Connect-Pwpush"
    }

    $url = "$($Script:Connection.Server)/$Endpoint"

    $params = @{
        UseBasicParsing = $true
        Method = $Method
        Uri = $Url
    }

    if ($PSBoundParameters.ContainsKey('Body'))
    {
        $params.Add("Body", $Body)
        $params.Add("ContentType", "application/json")
    }

    $reposne = Invoke-WebRequest @params
    if ($ReturnRaw.IsPresent)
    {
        return $reposne
    }

    return $reposne.Content | ConvertFrom-Json
}
#EndRegion './public/Invoke-PwpushRequest.ps1' 43
#Region './public/New-Pwpush.ps1' 0
function New-Pwpush
{
    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Payload,

        [string]$Note = "",
            
        [ValidateRange(1, 90)]
        [int]$ExpireAfterDays = 7,
        
        [ValidateRange(1, 100)]
        [int]$ExpireAfterViews = 5,
        
        [bool]$DeletableByViewer = $true,
        
        [bool]$RetrievalStep = $false
    )
    
    $endpoint = "p.json"

    $body = @{
        password = @{
            "payload"             = $Payload
            "note"                = $Note
            "expire_after_days"   = $ExpireAfterDays
            "expire_after_views"  = $ExpireAfterViews
            "deletable_by_viewer" = $DeletableByViewer
            "retrieval_step"      = $RetrievalStep
        }
    } | ConvertTo-Json

    $params = @{
        Endpoint = $endpoint 
        Method   = "Post" 
        Body     = $body
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/New-Pwpush.ps1' 41
#Region './public/Remove-Pwpush.ps1' 0
function Remove-Pwpush
{
    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )
    
    $endpoint = "p/$($UrlToken).json"

    $params = @{
        Endpoint = $endpoint 
        Method   = "Delete" 
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Remove-Pwpush.ps1' 17