mattmcnabb / OneLogin

A PowerShell module for automating components of a OneLogin account
7 stars 3 forks source link

more reliable object output #34

Closed mattmcnabb closed 7 years ago

mattmcnabb commented 7 years ago

Right now .NET objects are instantiated via casting from the custom objects outputted by Invoke-RestMethod. This is flimsy because if the api begins returning a new property for an object, then the cast will fail silently and no error will be returned.

Constructors could be used to build the objects but these could be rather large as the objects like events have a large number of properties. I'm not sure if there is a shortcut way to construct these objects except using New-Object or a .NET constructor. Will need test to see which one is more reliable.

Some thoughts:

If New-Object is used and we can pass the PSCustomObject directly to the -Properties parameter, then we'll still likely get an error if a property exists that is not defined in the class. At least we'll get an error, though.

If we use constructors, we can control exactly which custom object properties will be passed in to create the object, and objects should always be instantiated. One possible drawback to this is that we'll never be alerted to the fact that new properties exist on the API objects. This shouldn't be a problem if the API docs are kept up to date.

mattmcnabb commented 7 years ago

Update: may simply use type adapter instantion instead of casting with -as. This provides the benefit of casting arrays all at once, and also throws a terminating error when the properties returned by the REST api don't match those of the class we're casting to. This will allow for full error handling scenarios that weren't previously possible.