|
17 | 17 |
|
18 | 18 | # Body of the request |
19 | 19 | [ValidateNotNullOrEmpty()] |
20 | | - [string]$Body, |
| 20 | + [Hashtable]$Body, |
21 | 21 |
|
22 | 22 | [string] $Token, |
23 | 23 |
|
|
34 | 34 | Throw $exception |
35 | 35 | } |
36 | 36 |
|
| 37 | + #To support images "image" property have be handled before this |
| 38 | + |
37 | 39 | $_headers = @{ |
38 | 40 | "Authorization" = "Bearer $($token)" |
39 | 41 | 'Content-Type' = 'application/json; charset=utf-8' |
|
59 | 61 | ErrorAction = 'SilentlyContinue' |
60 | 62 | } |
61 | 63 |
|
62 | | - if ($Body) {$splatParameters["Body"] = [System.Text.Encoding]::UTF8.GetBytes($Body)} |
| 64 | + # Place holder for intended image manipulation |
| 65 | + # if and when snipe it API gets support for images |
| 66 | + if($null -ne $body -and $Body.Keys -contains 'image' ){ |
| 67 | + if($PSVersionTable.PSVersion -ge '7.0'){ |
| 68 | + $Body['image'] = get-item $body['image'] |
| 69 | + # As multipart/form-data is always POST we need add |
| 70 | + # requested method for laravel named as '_method' |
| 71 | + $Body['_method'] = $Method |
| 72 | + $splatParameters["Method"] = 'POST' |
| 73 | + $splatParameters["Form"] = $Body |
| 74 | + } else { |
| 75 | + # use base64 encoded images for powershell version < 7 |
| 76 | + Add-Type -AssemblyName "System.Web" |
| 77 | + $mimetype = [System.Web.MimeMapping]::GetMimeMapping($body['image']) |
| 78 | + $Body['image'] = 'data:@'+$mimetype+';base64,'+[Convert]::ToBase64String([IO.File]::ReadAllBytes($Body['image'])) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if ($Body -and $splatParameters.Keys -notcontains 'Form') { |
| 83 | + $splatParameters["Body"] = $Body | Convertto-Json |
| 84 | + } |
63 | 85 |
|
64 | 86 | $script:PSDefaultParameterValues = $global:PSDefaultParameterValues |
65 | 87 |
|
66 | | - Write-Debug $Body |
| 88 | + Write-Debug "$($Body | ConvertTo-Json)" |
67 | 89 |
|
68 | 90 | # Invoke the API |
69 | 91 | try { |
70 | 92 | Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi" |
71 | 93 | Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoke-WebRequest with: $($splatParameters | Out-String)" |
72 | | - $webResponse = Invoke-WebRequest @splatParameters |
| 94 | + $webResponse = Invoke-RestMethod @splatParameters |
73 | 95 | } |
74 | 96 | catch { |
75 | 97 | Write-Verbose "[$($MyInvocation.MyCommand.Name)] Failed to get an answer from the server" |
|
81 | 103 | if ($webResponse) { |
82 | 104 | Write-Verbose "[$($MyInvocation.MyCommand.Name)] Status code: $($webResponse.StatusCode)" |
83 | 105 |
|
84 | | - if ($webResponse.Content) { |
85 | | - Write-Verbose $webResponse.Content |
| 106 | + if ($webResponse) { |
| 107 | + Write-Verbose $webResponse |
86 | 108 |
|
87 | 109 | # API returned a Content: lets work wit it |
88 | 110 | try{ |
89 | | - $response = ConvertFrom-Json -InputObject $webResponse.Content |
90 | 111 |
|
91 | | - if ($response.status -eq "error") { |
| 112 | + if ($webResponse.status -eq "error") { |
92 | 113 | Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving" |
93 | 114 | # This could be handled nicely in an function such as: |
94 | 115 | # ResolveError $response -WriteError |
95 | | - Write-Error $($response.messages | Out-String) |
| 116 | + Write-Error $($webResponse.messages | Out-String) |
96 | 117 | } |
97 | 118 | else { |
98 | | - $result = $response |
99 | | - if (($response) -and ($response | Get-Member -Name payload)) |
100 | | - { |
101 | | - $result = $response.payload |
| 119 | + #update operations return payload |
| 120 | + if ($webResponse.payload){ |
| 121 | + $result = $webResponse.payload |
| 122 | + } |
| 123 | + #Search operations return rows |
| 124 | + elseif ($webResponse.rows) { |
| 125 | + $result = $webResponse.rows |
102 | 126 | } |
103 | | - elseif (($response) -and ($response | Get-Member -Name rows)) { |
104 | | - $result = $response.rows |
| 127 | + #Remove operations returns status and message |
| 128 | + elseif ($webResponse.status -eq 'success'){ |
| 129 | + $result = $webResponse.payload |
105 | 130 | } |
| 131 | + #get operations with id returns just one object |
| 132 | + else { |
| 133 | + $result = $webResponse |
| 134 | + } |
| 135 | + |
| 136 | + Write-Verbose "Status: $($webResponse.status)" |
| 137 | + Write-Verbose "Messages: $($webResponse.messages)" |
106 | 138 |
|
107 | 139 | $result |
| 140 | + |
| 141 | + |
| 142 | + |
108 | 143 | } |
109 | 144 | } |
110 | 145 | catch { |
|
0 commit comments