I’m adding pipeline support to a module which consumes a REST API, providing parent and student records from a school admissions system. My work is mostly cobbled together from watching Don Jones toolmaking series on YouTube, and snippets from around the web, and I would appreciate input from anybody familiar with this type of design pattern.
The module has private functions to handle OAuth and generic API requests via a wrapper function for Invoke-RestMethod
. Public functions expose each endpoint, e.g. Get-OAStudent
for /students
, Get-OAParent
for /parents
and so on. In turn, each endpoint supports /xxx/id
to retrieve a single record. For example, Get-OAParent
has 2 parameter sets, reflecting /parents/id
(single record) and /parents
(multiple records). For /parents/id,
the id parameter is supplied as below, ideally accepting an array of IDs via the pipeline:
[Parameter(ParameterSetName="ByID",ValueFromPipeline)]
[int]$ID
The wrapper function Invoke-OAAPIResquest
is then called within the PROCESS{}
block, to support pipeline input:
PROCESS{
foreach($i in $ID){
$RESTParams['Resource'] = "parents/$i"
try{
$Data = Invoke-OAAPIRequest @RESTParams -ErrorAction Stop
}catch{
Write-Warning "Failed to retrieve ID $i. $($_.Exception.Message)"
}
$Parent = $Data.parent
Write-Output $Parent
}
}
END{}
My question(s): Should the $Parent
object(s) be output individually as above, or in the END{}
block as an array? Should it be turned into a PSCustomObject first? Any design / best practice / performance reasons?
TIA