add function to trim and concat homeserver url and path

This commit is contained in:
James 2022-03-27 15:29:44 +01:00
parent e7355a5474
commit 366bde4897
3 changed files with 15 additions and 4 deletions

11
private/New-MatrixUrl.ps1 Normal file
View File

@ -0,0 +1,11 @@
function New-MatrixUrl {
param(
[Parameter(Mandatory)]
[string]$ServerUrl,
[Parameter(Mandatory)]
[string]$ApiPath
)
return $ServerUrl.Trim("/") + $ApiPath
}

View File

@ -31,7 +31,7 @@ function New-MatrixAccessToken {
[string]$DeviceDisplayName="PSMatrix" [string]$DeviceDisplayName="PSMatrix"
) )
$apiPath = "_matrix/client/v3/login" $url = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/login"
$apiMethod = "Post" $apiMethod = "Post"
$reqBody = @{ $reqBody = @{
@ -44,7 +44,7 @@ function New-MatrixAccessToken {
initial_device_display_name = $DeviceDisplayName initial_device_display_name = $DeviceDisplayName
} | ConvertTo-Json } | 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 $token = $res.access_token | ConvertTo-SecureString -AsPlainText

View File

@ -24,7 +24,7 @@ function Remove-MatrixAccessToken {
[SecureString]$AccessToken [SecureString]$AccessToken
) )
$apiPath = "_matrix/client/v3/logout" $url = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/logout"
$apiMethod = "Post" $apiMethod = "Post"
$headers = @{ $headers = @{
@ -32,7 +32,7 @@ function Remove-MatrixAccessToken {
} }
Try { Try {
Invoke-RestMethod -Uri "$ServerUrl/$apiPath" -Method $apiMethod -Headers $headers Invoke-RestMethod -Uri $url -Method $apiMethod -Headers $headers
return $true return $true
} }
Catch { Catch {