jpsider / RestPS

Simple ReST Framework for Powershell
MIT License
114 stars 30 forks source link

Add an exposed helper function for argument parsing #32

Closed grumpy-admin99 closed 2 years ago

grumpy-admin99 commented 4 years ago

currently each of the example ps routines handles conversion of arguments into an object separately. It would make sense to have a single function to handle this - and also maybe handle simple validation (ie making sure required parameters are present). please see below:

function Get-RestPSArguments { param ( [String] $RequestArgs, [String] $Body, [Array] $RequiredParams ) $RequestObj = New-Object System.Object if ($RequestArgs) {

Split the Argument Pairs by the '&' character

    $ArgumentPairs = $RequestArgs.split('&')
    foreach ($ArgumentPair in $ArgumentPairs) {
        # Split the Pair data by the '=' character
        $Property, $Value = $ArgumentPair.split('=')
        $RequestObj | Add-Member -MemberType NoteProperty -Name $Property -value $Value
    }
}
if ($RequiredParams) {
    $ErrorList=@()
    foreach($item in $RequiredParams) {
        if (![bool]($RequestObj.PSobject.Properties.name -match $item)) {
            $ErrorList += $item
         }
    }
    if ($ErrorList) {
        $Errors = [system.String]::Join(",", $ErrorList)
        $RequestObj | Add-Member -MemberType NoteProperty -Name "ArgumentsMissing" -Value $Errors
        $RequestObj | Add-Member -MemberType NoteProperty -Name "ArgumentsRAW" -Value $RequestArgs
    }
}
return $RequestObj

}

--- to call you would do something like $RequestObj=Get-RestPSArguments -RequestArgs $RequestArgs -RequiredParams @("Name") if ($RequestObj.ArgumentsMissing) {return $RequestObj.ArgumentsMissing}

jpsider commented 4 years ago

I would love a more elegant way of handling the arguments in the URL. Have you tested this locally? if so, do you want to submit a Pull Request?