Skip to content

Instantly share code, notes, and snippets.

@jahands
Last active April 17, 2020 23:17
Show Gist options
  • Save jahands/b577902cd9b03eb5030ff95e060ec712 to your computer and use it in GitHub Desktop.
Save jahands/b577902cd9b03eb5030ff95e060ec712 to your computer and use it in GitHub Desktop.
Class JsonBin {
[string]$BinID
[string]$SecretKey = '$2x65s4dfsdf98e46s5d4f/.'
[object]$Data
JsonBin($binID) {
$this.BinID = $binID
$this.Get()
}
Get() {
$res = Invoke-RestMethod -Method Get -Uri "https://api.jsonbin.io/b/$($this.BinID)/latest" -Headers @{'secret-key' = $this.SecretKey }
if (-not $res.success) {
Write-Error "Response did not contain required 'success' property! Please add it. Here's what we had: $($res | ConvertTo-Json)" -ErrorAction:Stop
}
$this.Data = $res
}
Put() {
Invoke-RestMethod -Method Put -Uri "https://api.jsonbin.io/b/$($this.BinID)" -Headers @{'Content-Type' = 'application/json'; 'secret-key' = $this.SecretKey } -Body $($this.Data | ConvertTo-Json -Compress)
}
}
Function New-JsonBinItemID {
Param (
[ValidateSet('Media', 'Text', 'Files', 'JHA', '1P', 'Factorio')]
[string]$Name,
[string]$Length = 5,
[switch]$Secure
)
$b = [JsonBin]::new('sdfsdfwe86f4sd65f4sd86fs')
$newID = ''
while ($newID -eq '' -or $b.Data.ids.$Name.Contains($newID) -or $b.Data.meta.reserved_ids.Contains($newID.ToLower())) {
$newID = (Get-RandomChars -Length $Length -Secure:$Secure)
}
$b.Data.ids.$Name += $newID
$b.Put()
Return $newID
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment