For at gøre brug af denne funktion skal man udfylde linjerne 2-4 med brugernavn, adgangskode og URL til det API man ønsker at kalde.

URL skal indeholde Main URL til det API man ønsker at kalde, fx. http://hartnet.dk/api/v2/

Der efter kan man kalde Get-APIService med følgende obligatoriske parametre:

  1. User
  2. Method – “GET”, “POST”, “PUT”, “DELETE”
  3. Endpoint

Fx. Get-APIService -User $credentials -Method GET -Endpoint “”

 

$credentials = @{
    "username" = "x"
    "password" = "x"
    "url"      = "x"
}

function Get-FreshService {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        $User,
        [Parameter(Mandatory)]
        $Endpoint,
        [Parameter(Mandatory)]
        [ValidateSet("GET", "POST", "PUT", "DELETE")]
        $Method,
        $Body
    )
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User.username, $User.password)))

    $Params = @{
        "Uri"         = $User.url + $Endpoint
        "Method"      = $Method
        "ContentType" = "application/json"
        "Headers"     = @{Authorization = ("Basic {0}" -f $base64AuthInfo) }
    }

    switch ($method) {
        "GET" { 
            Invoke-WebRequest @Params #| ConvertFrom-Json
        }
        "POST" {
            Invoke-WebRequest @Params -Body $Body
        }
        "PUT" {}
        "DELETE" {}
        Default {}
    }
}