Public/Set-MacadressConfiguration.ps1

function Set-MacadressConfiguration {
    <#
    .SYNOPSIS
        Set the API key, base URI and request timeout for the current session.

    .DESCRIPTION
        Values set here are held in memory for the life of the PowerShell
        session only; nothing is written to disk. Every cmdlet also accepts
        a per-call -ApiKey, and falls back to the MACADRESS_API_KEY
        environment variable when no key is configured.

    .PARAMETER ApiKey
        Your macadress.com API key. Accepts a plain string or a SecureString.
        Get a free key at https://macadress.com/signup.

    .PARAMETER BaseUri
        API root. Defaults to https://api.macadress.com. Change this only for
        a self-hosted deployment.

    .PARAMETER TimeoutSeconds
        Per-request timeout. Defaults to 30.

    .EXAMPLE
        Set-MacadressConfiguration -ApiKey 'mk_live_xxx'

    .EXAMPLE
        Set-MacadressConfiguration -ApiKey (Read-Host 'API key' -AsSecureString)

    .LINK
        https://github.com/sapisos/macadress-powershell
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
    [OutputType([void])]
    param(
        [Parameter()]
        [object] $ApiKey,

        [Parameter()]
        [ValidatePattern('^https?://')]
        [string] $BaseUri,

        [Parameter()]
        [ValidateRange(1, 600)]
        [int] $TimeoutSeconds
    )

    $cfg = Get-MacadressConfigStore

    if (-not $PSCmdlet.ShouldProcess('Macadress module configuration', 'Update')) { return }

    if ($PSBoundParameters.ContainsKey('ApiKey')) {
        if ($ApiKey -is [securestring]) {
            $cfg.ApiKey = [System.Net.NetworkCredential]::new('', $ApiKey).Password
        }
        elseif ($null -eq $ApiKey -or $ApiKey -eq '') {
            $cfg.ApiKey = $null
        }
        else {
            $cfg.ApiKey = [string] $ApiKey
        }
    }

    if ($PSBoundParameters.ContainsKey('BaseUri')) {
        $cfg.BaseUri = $BaseUri.TrimEnd('/')
    }

    if ($PSBoundParameters.ContainsKey('TimeoutSeconds')) {
        $cfg.TimeoutSeconds = $TimeoutSeconds
    }
}