Billomat.psm1
$baseUrl = "https://guidnew.billomat.net/api"; <# .SYNOPSIS Connects to Billomat. .DESCRIPTION Credentials can be persisted using https://www.powershellgallery.com/packages/CredentialsManager/ by setting the parameter UseCredentialsManager to $True. o $Credential -eq $Null -and $UseCredentialsManager -eq $Null -> credentials are requested and are not persisted o $Credential -ne $Null -and $UseCredentialsManager -eq $Null -> passed credentials are used and not persisted o $Credential -ne $Null -and $UseCredentialsManager -ne $Null -> passed credentials are used and persisted o $Credential -eq $Null -and $UseCredentialsManager -ne $Null -> Persisted credentials are used .PARAMETER Credential Credential to access Billomat with. I not provided they get requested. .PARAMETER UseCredentialsManager If $True, credentials are read and written using the CredentialsManager. Only Password part, containing the APIKey, is used. #> function Connect-BMT { [CmdletBinding()] Param( [PSCredential]$Credential, [Boolean]$UseCredentialsManager = $False ) #end param # check module prerequisites if($UseCredentialsManager) { $module = Get-Module -ListAvailable -Name "CredentialsManager"; if (!$module) { throw "Module 'CredentialsManager' needed. Please install executing 'Install-Module -Name CredentialsManager' as Administrator."; } } if($UseCredentialsManager -and $Credential -eq $Null) { $Credential = Read-Credential -ListAvailable | Where { $_.Environment -eq "Billomat" } } if(!$Credential) { $Credential = Get-Credential -Message "Please enter API Key for Billomat as password (username can be anything)."; } if($UseCredentialsManager) { Write-Credential "Billomat" -Credential $Credential; } $script:key = $Credential.GetNetworkCredential().password ; } <# .SYNOPSIS Get Client by Name or ClientNumber from Billomat .DESCRIPTION If no client is found, an empty array is returned. .PARAMETER Name Name of client to get. Returns all clients having the given value in their name. .PARAMETER ClientNumber Number of client to get. #> function Get-BMTClients { [CmdletBinding()] Param( [string]$Name, [string]$ClientNumber ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } if($Name) { $parameters.Add("name", $Name) } if($ClientNumber) { $parameters.Add("client_number", $ClientNumber) } $uri = New-HttpQueryString "${baseUrl}/clients" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; if($response.clients."@total" -eq 0) { return @(); } return $response.clients.client } <# .SYNOPSIS Get Client by Id .DESCRIPTION .PARAMETER Id Id of client to get. #> function Get-BMTClient { [CmdletBinding()] Param( [int]$Id ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/clients/${Id}" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; return $response.client } <# .SYNOPSIS Get Contacts by client .DESCRIPTION If no contact is found, an empty array is returned. .PARAMETER ClientId Id of client to receive contacts for. #> function Get-BMTContacts { [CmdletBinding()] Param( [int]$ClientId ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; client_id=$ClientId } $uri = New-HttpQueryString "${baseUrl}/contacts" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; if($response.contacts."@total" -eq 0) { return @(); } return $response.contacts.contact } <# .SYNOPSIS Get Contact by Id .DESCRIPTION .PARAMETER Id Id of contact to get. #> function Get-BMTContact { [CmdletBinding()] Param( [int]$Id ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/contacts/${Id}" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; return $response.contact } ## Private Functions function New-HttpQueryString { [CmdletBinding()] param ( [Parameter(Mandatory = $true)][String]$Uri, [Parameter(Mandatory = $true)][Hashtable]$QueryParameter ) # Add System.Web Add-Type -AssemblyName System.Web # Create a http name value collection from an empty string $nvCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) foreach ($key in $QueryParameter.Keys) { $nvCollection.Add($key, $QueryParameter.$key) } # Build the uri $uriRequest = [System.UriBuilder]$uri $uriRequest.Query = $nvCollection.ToString() return $uriRequest.Uri.OriginalString } |