From 366bde489754047917e4ac1e3f923cc5eae559ae Mon Sep 17 00:00:00 2001 From: Thumbscrew Date: Sun, 27 Mar 2022 15:29:44 +0100 Subject: [PATCH] add function to trim and concat homeserver url and path --- private/New-MatrixUrl.ps1 | 11 +++++++++++ public/New-MatrixAccessToken.ps1 | 4 ++-- public/Remove-MatrixAccessToken.ps1 | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 private/New-MatrixUrl.ps1 diff --git a/private/New-MatrixUrl.ps1 b/private/New-MatrixUrl.ps1 new file mode 100644 index 0000000..cec4597 --- /dev/null +++ b/private/New-MatrixUrl.ps1 @@ -0,0 +1,11 @@ +function New-MatrixUrl { + param( + [Parameter(Mandatory)] + [string]$ServerUrl, + + [Parameter(Mandatory)] + [string]$ApiPath + ) + + return $ServerUrl.Trim("/") + $ApiPath +} \ No newline at end of file diff --git a/public/New-MatrixAccessToken.ps1 b/public/New-MatrixAccessToken.ps1 index c025423..3ca3952 100644 --- a/public/New-MatrixAccessToken.ps1 +++ b/public/New-MatrixAccessToken.ps1 @@ -31,7 +31,7 @@ function New-MatrixAccessToken { [string]$DeviceDisplayName="PSMatrix" ) - $apiPath = "_matrix/client/v3/login" + $url = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/login" $apiMethod = "Post" $reqBody = @{ @@ -44,7 +44,7 @@ function New-MatrixAccessToken { initial_device_display_name = $DeviceDisplayName } | ConvertTo-Json - $res = Invoke-RestMethod -Uri "$ServerUrl/$apiPath" -Method $apiMethod -Body $reqBody + $res = Invoke-RestMethod -Uri $url -Method $apiMethod -Body $reqBody $token = $res.access_token | ConvertTo-SecureString -AsPlainText diff --git a/public/Remove-MatrixAccessToken.ps1 b/public/Remove-MatrixAccessToken.ps1 index a436669..c58211e 100644 --- a/public/Remove-MatrixAccessToken.ps1 +++ b/public/Remove-MatrixAccessToken.ps1 @@ -24,7 +24,7 @@ function Remove-MatrixAccessToken { [SecureString]$AccessToken ) - $apiPath = "_matrix/client/v3/logout" + $url = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/logout" $apiMethod = "Post" $headers = @{ @@ -32,7 +32,7 @@ function Remove-MatrixAccessToken { } Try { - Invoke-RestMethod -Uri "$ServerUrl/$apiPath" -Method $apiMethod -Headers $headers + Invoke-RestMethod -Uri $url -Method $apiMethod -Headers $headers return $true } Catch {