|
1 | | -function Invoke-SnipeitMethod { |
2 | | - <# |
| 1 | +<# |
3 | 2 | .SYNOPSIS |
4 | | - Extracted invokation of the REST method to own function. |
5 | | - #> |
| 3 | + Make api request to Snipe it |
| 4 | +
|
| 5 | + .PARAMETER Api |
| 6 | + Api part of url. prefix with slash ie. "/api/v1/hardware" |
| 7 | +
|
| 8 | + .PARAMETER Method |
| 9 | + Method of the invokation, one of following "GET", "POST", "PUT", "PATCH" or "DELETE" |
| 10 | +
|
| 11 | + .PARAMETER Body |
| 12 | + Request body as hashtable. Needed for post, put and patch |
| 13 | +
|
| 14 | + .PARAMETER GetParameters |
| 15 | + Get-Parameters as hastable. |
| 16 | +#> |
| 17 | + |
| 18 | +function Invoke-SnipeitMethod { |
6 | 19 | [OutputType( |
7 | 20 | [PSObject] |
8 | 21 | )] |
| 22 | + |
9 | 23 | param ( |
10 | | - # REST API to invoke |
| 24 | + |
11 | 25 | [Parameter(Mandatory = $true)] |
12 | | - [Uri]$URi, |
| 26 | + [string]$Api, |
13 | 27 |
|
14 | | - # Method of the invokation |
15 | 28 | [ValidateSet("GET", "POST", "PUT", "PATCH", "DELETE")] |
16 | 29 | [string]$Method = "GET", |
17 | 30 |
|
18 | | - # Body of the request |
19 | 31 | [Hashtable]$Body, |
20 | 32 |
|
21 | | - [string] $Token, |
22 | | - |
23 | | - # GET Parameters |
24 | 33 | [Hashtable]$GetParameters |
25 | | - |
26 | 34 | ) |
27 | 35 |
|
28 | 36 | BEGIN { |
| 37 | + #use legacy per command based url and apikey |
| 38 | + if ( $null -ne $SnipeitPSSession.legacyUrl -and $null -ne $SnipeitPSSession.legacyApiKey ) { |
| 39 | + [string]$Url = $SnipeitPSSession.legacyUrl |
| 40 | + Write-Debug "Invoke-SnipeitMethod url: $Url" |
| 41 | + $Token = ConvertFrom-SecureString -AsPlainText -SecureString $SnipeitPSSession.legacyApiKey |
| 42 | + |
| 43 | + } elseif ($null -ne $SnipeitPSSession.url -and $null -ne $SnipeitPSSession.apiKey) { |
| 44 | + [string]$Url = $SnipeitPSSession.url |
| 45 | + Write-Debug "Invoke-SnipeitMethod url: $Url" |
| 46 | + $Token = ConvertFrom-SecureString -AsPlainText -SecureString $SnipeitPSSession.apiKey |
| 47 | + |
| 48 | + } else { |
| 49 | + throw "Please use Connect-SnipeitPS to setup connection before any other commands." |
| 50 | + } |
| 51 | + |
29 | 52 | # Validation of parameters |
30 | 53 | if (($Method -in ("POST", "PUT", "PATCH")) -and (!($Body))) { |
31 | 54 | $message = "The following parameters are required when using the ${Method} parameter: Body." |
32 | 55 | $exception = New-Object -TypeName System.ArgumentException -ArgumentList $message |
33 | 56 | Throw $exception |
34 | 57 | } |
35 | 58 |
|
| 59 | + #Build request uri |
| 60 | + $apiUri = "$Url$Api" |
36 | 61 | #To support images "image" property have be handled before this |
37 | 62 |
|
38 | 63 | $_headers = @{ |
39 | | - "Authorization" = "Bearer $($token)" |
| 64 | + "Authorization" = "Bearer $($Token)" |
40 | 65 | 'Content-Type' = 'application/json; charset=utf-8' |
41 | 66 | "Accept" = "application/json" |
42 | 67 | } |
43 | 68 | } |
44 | 69 |
|
45 | 70 | Process { |
46 | | - if ($GetParameters -and ($URi -notlike "*\?*")) |
47 | | - { |
| 71 | + # This can be done using $Body, maybe some day - PetriAsi |
| 72 | + if ($GetParameters -and ($apiUri -notlike "*\?*")){ |
48 | 73 | Write-Debug "Using `$GetParameters: $($GetParameters | Out-String)" |
49 | | - [string]$URI += (ConvertTo-GetParameter $GetParameters) |
| 74 | + [string]$apiUri = $apiUri + (ConvertTo-GetParameter $GetParameters) |
50 | 75 | # Prevent recursive appends |
51 | 76 | $GetParameters = $null |
52 | 77 | } |
53 | 78 |
|
54 | 79 | # set mandatory parameters |
55 | 80 | $splatParameters = @{ |
56 | | - Uri = $URi |
| 81 | + Uri = $apiUri |
57 | 82 | Method = $Method |
58 | 83 | Headers = $_headers |
59 | 84 | UseBasicParsing = $true |
60 | 85 | ErrorAction = 'SilentlyContinue' |
61 | 86 | } |
62 | 87 |
|
63 | | - # Place holder for intended image manipulation |
64 | | - # if and when snipe it API gets support for images |
| 88 | + # Send image requests as multipart/form-data if supported |
65 | 89 | if($null -ne $body -and $Body.Keys -contains 'image' ){ |
66 | 90 | if($PSVersionTable.PSVersion -ge '7.0'){ |
67 | 91 | $Body['image'] = get-item $body['image'] |
|
71 | 95 | $splatParameters["Method"] = 'POST' |
72 | 96 | $splatParameters["Form"] = $Body |
73 | 97 | } else { |
74 | | - # use base64 encoded images for powershell version < 7 |
75 | | - Add-Type -AssemblyName "System.Web" |
76 | | - $mimetype = [System.Web.MimeMapping]::GetMimeMapping($body['image']) |
77 | | - $Body['image'] = 'data:@'+$mimetype+';base64,'+[Convert]::ToBase64String([IO.File]::ReadAllBytes($Body['image'])) |
| 98 | + # use base64 encoded images for powershell version < 7 |
| 99 | + Add-Type -AssemblyName "System.Web" |
| 100 | + $mimetype = [System.Web.MimeMapping]::GetMimeMapping($body['image']) |
| 101 | + $Body['image'] = 'data:@'+$mimetype+';base64,'+[Convert]::ToBase64String([IO.File]::ReadAllBytes($Body['image'])) |
78 | 102 | } |
79 | 103 | } |
80 | 104 |
|
81 | 105 | if ($Body -and $splatParameters.Keys -notcontains 'Form') { |
82 | | - $splatParameters["Body"] = $Body | Convertto-Json |
| 106 | + $splatParameters["Body"] = [System.Text.Encoding]::UTF8.GetBytes(($Body | Convertto-Json)) |
83 | 107 | } |
84 | 108 |
|
85 | 109 | $script:PSDefaultParameterValues = $global:PSDefaultParameterValues |
|
105 | 129 | if ($webResponse) { |
106 | 130 | Write-Verbose $webResponse |
107 | 131 |
|
108 | | - # API returned a Content: lets work wit it |
| 132 | + # API returned a Content: lets work with it |
109 | 133 | try{ |
110 | | - |
111 | 134 | if ($webResponse.status -eq "error") { |
112 | | - Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving" |
| 135 | + Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received ... resolving" |
113 | 136 | # This could be handled nicely in an function such as: |
114 | 137 | # ResolveError $response -WriteError |
115 | 138 | Write-Error $($webResponse.messages | Out-String) |
116 | | - } |
117 | | - else { |
| 139 | + } else { |
118 | 140 | #update operations return payload |
119 | | - if ($webResponse.payload){ |
| 141 | + if ($webResponse.payload) { |
120 | 142 | $result = $webResponse.payload |
121 | 143 | } |
122 | 144 | #Search operations return rows |
123 | 145 | elseif ($webResponse.rows) { |
124 | 146 | $result = $webResponse.rows |
125 | 147 | } |
126 | 148 | #Remove operations returns status and message |
127 | | - elseif ($webResponse.status -eq 'success'){ |
| 149 | + elseif ($webResponse.status -eq 'success') { |
128 | 150 | $result = $webResponse.payload |
129 | 151 | } |
130 | 152 | #Search and query result with no results |
|
140 | 162 | Write-Verbose "Messages: $($webResponse.messages)" |
141 | 163 |
|
142 | 164 | $result |
143 | | - |
144 | | - |
145 | | - |
146 | 165 | } |
147 | 166 | } |
148 | 167 | catch { |
|
151 | 170 |
|
152 | 171 | } |
153 | 172 | elseif ($webResponse.StatusCode -eq "Unauthorized") { |
154 | | - Write-Error "[$($MyInvocation.MyCommand.Name)] You are not Authorized to access the resource, check your token is correct" |
| 173 | + Write-Error "[$($MyInvocation.MyCommand.Name)] You are not Authorized to access the resource, check your apiKey is correct" |
155 | 174 | } |
156 | 175 | else { |
157 | 176 | # No content, although statusCode < 400 |
|
169 | 188 | Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function ended" |
170 | 189 | } |
171 | 190 | } |
| 191 | + |
0 commit comments