Skip to content

Instantly share code, notes, and snippets.

@derek-baker
Created August 17, 2020 14:26
Show Gist options
  • Save derek-baker/d0b3e51f94a34364551b00a7dc47cf9c to your computer and use it in GitHub Desktop.
Save derek-baker/d0b3e51f94a34364551b00a7dc47cf9c to your computer and use it in GitHub Desktop.
param(
[Parameter(mandatory=$true)]
$apiUrl, # EX: 'https://<ProductionAppDomain>/api/v1.0/bulkupload/postgrievance',
[Parameter(mandatory=$true)]
$apiKey, # EX 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
[Parameter(mandatory=$true)]
$repEmail,
[Parameter(mandatory=$true)]
$parcelIdentifier,
[Parameter(mandatory=$true)]
$rp524JsonString, # EX: '{ "TODO:" : "TheData" }',
[Parameter(mandatory=$true)]
$signatureImage, # EX: "$PSScriptRoot\Signature.PNG",
[Parameter(mandatory=$true)]
$supportingDocumentsZipPath # EX: "$PSScriptRoot\SupportingDocuments.zip"
)
#Requires -Version 5.1
#Requires -PSEdition Desktop
$ErrorActionPreference = "Stop";
Set-StrictMode -Version 'Latest'
# Windows Powershell defaults to TLS1.0, which is obsolete, so configure the session to use TLS1.2
function SetDefaultTlsVersion() {
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
}
# Test that the JSON is not malformed
function ValidateJson(
[Parameter(mandatory=$true)]
[string] $json
) {
try {
$json | ConvertFrom-Json | Out-Null
}
catch {
Write-Error -Message 'It appears your JSON is invalid. Please investigate.'
exit 1
}
}
# Convert files into format we can serialize to JSON
function GetFileAsBase64String(
[Parameter(mandatory=$true)]
[string] $filePath
) {
[byte[]] $bytes = Get-Content -Path $filePath -Encoding Byte -Verbose
return [Convert]::ToBase64String($bytes)
}
# Send grievance-creation request to API
function MakeRequest(
[Parameter(mandatory=$true)]
[string] $url,
[Parameter(mandatory=$true)]
[string] $email,
[Parameter(mandatory=$true)]
[string] $parcelId,
[Parameter(mandatory=$true)]
[string] $json,
[Parameter(mandatory=$true)]
[string] $signature,
[Parameter(mandatory=$true)]
[string] $zip,
[Parameter(mandatory=$false)]
[string] $apiMethod = 'POST',
[Parameter(mandatory=$false)]
[string] $apiKeyHeader = 'X-SDG-APIKEY',
[Parameter(mandatory=$false)]
[string] $contentType = 'application/json'
) {
$headers = @{}
$headers[$apiKeyHeader] = $apiKey
$body = @{
formData = $json
email = $email
parcelId = $parcelId
signatureFour = $signature
signatureFive = $signature
supportingDocsZipBytesAsBase64 = $zip
}
$restParams = @{
Uri = $url
Method = $apiMethod
Headers = $headers
ContentType = $contentType
Body = (ConvertTo-Json -InputObject $body)
}
Invoke-RestMethod @restParams
}
# Set up for request
SetDefaultTlsVersion
ValidateJson -json $rp524JsonString
$zipData = if ($null -ne $supportingDocumentsZipPath) {
GetFileAsBase64String -filePath $supportingDocumentsZipPath
}
[string] $signature = GetFileAsBase64String -filePath $signatureImage
# Send data
$response = MakeRequest `
-url $apiUrl `
-email $repEmail `
-parcelId $parcelIdentifier `
-json $rp524JsonString `
-zip $zipData `
-signature $signature
$response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment