Private/Types.psm1

#!/usr/bin/env pwsh
using namespace System
using namespace System.IO
using namespace System.Text
using namespace System.Net.Http
using namespace System.Security

class TwilioEndpoint {
  [string] $Value
  Endpoint() {}
  Endpoint([string]$value) { $this.Value = $value }
  [string] ToString() {
    return $this.Value
  }
}

class PhoneNumber : TwilioEndpoint {
  [string] $_number
  PhoneNumber([string]$number) : base($number) { $this._number = $number }
  [string] ToString() { return $this._number }
}

class TwilioClientEndpoint : TwilioEndpoint {
  [string] $Identity
  Client([string]$identity) { $this.Identity = $identity }
  [string] ToString() { return 'client:' + $this.Identity }
}

class PhoneNumberCapabilities {
  [bool] $Voice
  [bool] $Sms
  [bool] $Mms
  [bool] $Fax
}

class PhoneNumberPrice {
  [string] $BasePrice
  [string] $CurrentPrice
}

class InboundCallPrice {
  [string] $BasePrice
  [string] $CurrentPrice
}
class OutboundCallPrice {
  [string] $BasePrice
  [string] $CurrentPrice
}
class InboundSmsPrice {
  [string] $BasePrice
  [string] $CurrentPrice
}
class OutboundSmsPrice {
  [string] $BasePrice
  [string] $CurrentPrice
}


class AccessTokenResource {
  [string] $ServiceSid
  [string] $Identity
  [string] $Token

  static [AccessTokenResource] Create([string]$pathServiceSid, [string]$identity) {
    $r = [AccessTokenResource]::new()
    $r.ServiceSid = $pathServiceSid
    $r.Identity = $identity
    $r.Token = "stub-token-$identity"
    return $r
  }
}

class AccountReport {
  [string] $Sid
  [string] $AccountSid
  [string] $Status
  [DateTime] $DateCreated
}

class Application {
  [string] $Sid
  [string] $FriendlyName
  [string] $ApiVersion
}

class AuthenticationAction {
  [string] $Type
  [string] $Value
}

class Resource {
}

class IOptions {
  [void] Validate() {
    foreach ($p in $this.PSObject.Properties) {
      if ($p.Name -like 'Path*' -and $null -eq $p.Value) {
        throw "Required option '$($p.Name)' cannot be null."
      }
    }
  }
}

class TwilioPage {
  [object[]] $Records
  [int] $PageSize

  [string] $Uri
  [string] $Url
  [string] $FirstPageUri
  [string] $FirstPageUrl
  [string] $NextPageUri
  [string] $NextPageUrl
  [string] $PreviousPageUri
  [string] $PreviousPageUrl

  TwilioPage([object[]]$records, [int]$pageSize) {
    $this.Records = $records
    $this.PageSize = $pageSize
  }

  [bool] HasNextPage() {
    return ![string]::IsNullOrEmpty($this.NextPageUrl) -or ![string]::IsNullOrEmpty($this.NextPageUri)
  }
}

class ResourceSetResponse {
  [TwilioPage] $Page
  [object[]] $Records

  ResourceSetResponse([TwilioPage]$page) {
    $this.Page = $page
    $this.Records = $page.Records
  }
}

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseUsingScopeModifierInNewRunspaces", "")]
class ResourceSet {
  [bool] $AutoPaging
  [TwilioPage] $Page
  [object[]] $Records
  [scriptblock] $PageFetcher

  ResourceSet([TwilioPage]$page) {
    $this.Page = $page
    $this.Records = $page.Records
    $this.AutoPaging = $true
  }

  [void] SetPageFetcher([scriptblock]$fetcher) {
    $this.PageFetcher = $fetcher
  }

  [TwilioPage] FetchNextPage() {
    if (!$this.Page.HasNextPage()) { return $null }
    if ($null -eq $this.PageFetcher) { throw 'ResourceSet PageFetcher is not configured.' }

    $next = & $this.PageFetcher $this.Page
    if ($next -is [TwilioPage]) {
      $this.Page = $next
      $this.Records = $this.Records + $next.Records
      return $next
    }

    return $null
  }

  [void] FetchAll() {
    while ($this.AutoPaging -and $this.Page.HasNextPage()) {
      $next = $this.FetchNextPage()
      if ($null -eq $next) { break }
    }
  }

  [System.Management.Automation.Job] FetchAllAsync() {
    $set = $this
    return Start-Job -ScriptBlock {
      param($resourceSet)
      while ($resourceSet.AutoPaging -and $resourceSet.Page.HasNextPage()) {
        $next = $resourceSet.FetchNextPage()
        if ($null -eq $next) { break }
      }
      return $resourceSet.Records
    } -ArgumentList $set
  }
}