use invoke-webrequest, plan support for images

This commit is contained in:
Petri Asikainen 2021-06-21 10:13:43 +03:00
parent 95839398e9
commit 193f3f7f11
3 changed files with 48 additions and 29 deletions

View file

@ -35,7 +35,6 @@
} }
#To support images "image" property have be handled before this #To support images "image" property have be handled before this
if($Body) { $body = $Body | ConvertTo-Json }
$_headers = @{ $_headers = @{
"Authorization" = "Bearer $($token)" "Authorization" = "Bearer $($token)"
@ -60,9 +59,24 @@
Headers = $_headers Headers = $_headers
UseBasicParsing = $true UseBasicParsing = $true
ErrorAction = 'SilentlyContinue' ErrorAction = 'SilentlyContinue'
Proxy = 'http://localhost:8080'
} }
if ($Body) {$splatParameters["Body"] = [System.Text.Encoding]::UTF8.GetBytes($Body)} #Place holder for intended image manipulation
# if and when snipe it API gets support for images
if($null -ne $body -and $Body.Keys -contains 'image' ){
if($PSVersionTable.PSVersion -ge 7){
$Body['image'] = get-item $body['image']
$splatParameters["Form"] = $Body
} else {
write-warning "Setting images is supported only with powershell version 7 or greater"
$Body.Remove('image')
}
}
if ($Body -and $splatParameters.Keys -notcontains 'Form') {
$splatParameters["Body"] = $Body | Convertto-Json
}
$script:PSDefaultParameterValues = $global:PSDefaultParameterValues $script:PSDefaultParameterValues = $global:PSDefaultParameterValues
@ -72,7 +86,7 @@
try { try {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi" Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoke-WebRequest with: $($splatParameters | Out-String)" Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoke-WebRequest with: $($splatParameters | Out-String)"
$webResponse = Invoke-WebRequest @splatParameters $webResponse = Invoke-RestMethod @splatParameters
} }
catch { catch {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Failed to get an answer from the server" Write-Verbose "[$($MyInvocation.MyCommand.Name)] Failed to get an answer from the server"
@ -84,27 +98,27 @@
if ($webResponse) { if ($webResponse) {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Status code: $($webResponse.StatusCode)" Write-Verbose "[$($MyInvocation.MyCommand.Name)] Status code: $($webResponse.StatusCode)"
if ($webResponse.Content) { if ($webResponse) {
Write-Verbose $webResponse.Content Write-Verbose $webResponse
# API returned a Content: lets work wit it # API returned a Content: lets work wit it
try{ try{
$response = ConvertFrom-Json -InputObject $webResponse.Content
if ($response.status -eq "error") { if ($webResponse.status -eq "error") {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving" Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving"
# This could be handled nicely in an function such as: # This could be handled nicely in an function such as:
# ResolveError $response -WriteError # ResolveError $response -WriteError
Write-Error $($response.messages | Out-String) Write-Error $($webResponse.messages | Out-String)
} }
else { else {
$result = $response
if (($response) -and ($response | Get-Member -Name payload)) if ($webResponse.payload){
{ $result = $webResponse.payload
$result = $response.payload
} }
elseif (($response) -and ($response | Get-Member -Name rows)) { elseif ($webResponse.rows) {
$result = $response.rows $result = $webResponse.rows
} else {
$result = $webResponse
} }
$result $result

View file

@ -38,24 +38,12 @@ Cost of item being purchased.
.PARAMETER purchase_date .PARAMETER purchase_date
Date accessory was purchased Date accessory was purchased
.PARAMETER order_number
Order number for this accessory.
.PARAMETER purchase_cost
Cost of item being purchased.
.PARAMETER purchase_date
Date accessory was purchased
.PARAMETER supplier_id .PARAMETER supplier_id
ID number of the supplier for this accessory ID number of the supplier for this accessory
.PARAMETER location_id .PARAMETER location_id
ID number of the location the accessory is assigned to ID number of the location the accessory is assigned to
.PARAMETER min_qty
Min quantity of the accessory before alert is triggered
.PARAMETER url .PARAMETER url
URL of Snipeit system, can be set using Set-SnipeitInfoeItInfo command URL of Snipeit system, can be set using Set-SnipeitInfoeItInfo command
@ -100,6 +88,8 @@ function Set-SnipeitAccessory() {
[Nullable[System.Int32]]$supplier_id, [Nullable[System.Int32]]$supplier_id,
[Nullable[System.Int32]]$location_id,
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[string]$url, [string]$url,

View file

@ -16,7 +16,7 @@ Updates accessory on Snipe-It system
Set-SnipeitAccessory [-id] <Int32[]> [[-name] <String>] [[-qty] <Int32>] [[-category_id] <Int32>] Set-SnipeitAccessory [-id] <Int32[]> [[-name] <String>] [[-qty] <Int32>] [[-category_id] <Int32>]
[[-company_id] <Int32>] [[-manufacturer_id] <Int32>] [[-model_number] <String>] [[-order_number] <String>] [[-company_id] <Int32>] [[-manufacturer_id] <Int32>] [[-model_number] <String>] [[-order_number] <String>]
[[-purchase_cost] <Single>] [[-purchase_date] <DateTime>] [[-min_amt] <Int32>] [[-supplier_id] <Int32>] [[-purchase_cost] <Single>] [[-purchase_date] <DateTime>] [[-min_amt] <Int32>] [[-supplier_id] <Int32>]
[-url] <String> [-apiKey] <String> [-WhatIf] [-Confirm] [<CommonParameters>] [[-location_id] <Int32>] [-url] <String> [-apiKey] <String> [-WhatIf] [-Confirm] [<CommonParameters>]
``` ```
## DESCRIPTION ## DESCRIPTION
@ -40,7 +40,7 @@ Parameter Sets: (All)
Aliases: Aliases:
Required: True Required: True
Position: 14 Position: 15
Default value: None Default value: None
Accept pipeline input: False Accept pipeline input: False
Accept wildcard characters: False Accept wildcard characters: False
@ -91,6 +91,21 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False Accept wildcard characters: False
``` ```
### -location_id
ID number of the location the accessory is assigned to
```yaml
Type: Int32
Parameter Sets: (All)
Aliases:
Required: False
Position: 13
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -manufacturer_id ### -manufacturer_id
ID number of the manufacturer for this accessory. ID number of the manufacturer for this accessory.
@ -235,7 +250,7 @@ Parameter Sets: (All)
Aliases: Aliases:
Required: True Required: True
Position: 13 Position: 14
Default value: None Default value: None
Accept pipeline input: False Accept pipeline input: False
Accept wildcard characters: False Accept wildcard characters: False