PowerShell Brickset Module: Updated for API v3 and PS 7.1
Brickset recently updated their API to v3 and stopped supporting the previous API v2. After receiving a number of requests to update my PowerShell module for Brickset , (which was developed against v2 and consequently had stopped working), both via a GitHub issue and some direct contact (including, surprisingly to me, internal email at work) I decided I’d better put some effort into updating it.
I’d had a quick look at the v3 API not long after it had been made available, and as far as I remember it was still only SOAP based. This had put me off updating the PowerShell module, since I had wanted to also support PowerShell core and there is still no SOAP support in PowerShell core, i.e. no New-WebServiceProxy available - see here and here. The previous version of the module was built entirely around using New-WebServiceProxy .
However, when I looked again in December 2020 following more external prompting I noticed on the API documentation page that there was support for REST as well as SOAP:
This meant that it was possible to use Invoke-RestMethod instead of New-WebServiceProxy and after some basic experimenting I went with the second option from the above screenshot to use HTTP POST and Content-Type set to application/x-www-form-urlencoded
- which by the way I discovered as part of this exercise is the default when specifying the POST method with Invoke-RestMethod and no Content-Type set:
So the Brickset Module now contains a wrapper for Invoke-RestMethod, Invoke-BricksetRestMethod, which makes it easy to work with the API. For example to getThemes :
The rest of the functions are built using a similar pattern, some more complex where a more complicated body section is required to be sent.
There is also no more $Global:bricksetConnection to contain the API key and UserHash we need to make REST calls, it’s now available to the module via $Script:bricksetConnection. A security improvement I learnt about from an issue raised during PowervRA development - using script scope in a module is effectively module scope.
In addition I took the opportunity to re-use and update the Azure Pipelines CI/CD work Craig had done for us in PowervRA and apply it to this module which brings the following benefits:
- Builds the Module psd1 and psm1 files from the src folder
- Carries out PowerShell Script Analyzer tests
- Publishes the module to the PowerShell Gallery
- Creates a GitHub release
This means I can carry out any future updates to this module quicker and easier. Publishing a new version is as simple as creating a tag in the repo and the pipeline does everything else.
Version 2.0.1 of the Brickset module is now available from GitHub or the PowerShell Gallery via: