Skip to content

Instantly share code, notes, and snippets.

@datadavev
Last active August 30, 2022 20:50
Show Gist options
  • Save datadavev/969ee2365bf10f4ee332b345c10c0a7e to your computer and use it in GitHub Desktop.
Save datadavev/969ee2365bf10f4ee332b345c10c0a7e to your computer and use it in GitHub Desktop.
<# Based on https://git.psu.edu/digipres/scripts/-/blob/master/metadata/arks.ps1
Mint a test identifier on EZID stage using powershell.
e.g.:
$ pwsh ./mint_ark.ps1
Minted: ark:/99999/fk4dn5nm4d
Metadata:
success : ark:/99999/fk4dn5nm4d
erc.who : Taco, Ted
erc.what : Taco Recipe
erc.when : 1999
_owner : apitest
_ownergroup : apitest
_created : 1661889799
_updated : 1661889799
_profile : erc
_target : https://google.com
_status : public
_export : yes
#>
# EZID variables
$baseURL = "https://ezid-stg.cdlib.org/"
$naan = "99999"
$user = "apitest"
$pass = "apitest"
$pair = "${user}:${pass}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$userAgent = "PowerShell"
function Get-ARKmetadata {
param (
$ark
)
$headers = @{
"Accept" = "text/plain"
"Authorization" = $basicAuthValue
}
$output = New-Object -TypeName psobject
$response = Invoke-WebRequest -URI "$baseURL/id/$ark" -Headers $headers -Method get -UserAgent $userAgent
$lines = $response.content.split([System.Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries)
$lines | ForEach-Object {
$split = $_.split(": ").trim()
$key = $split[0]
$value = $split[1]
$output | Add-Member -Name "$key" -Type NoteProperty -Value "$value"
}
$output
}
function Mint-ARK {
param($shoulder)
$headers = @{
"Accept" = "text/plain"
"Authorization" = $basicAuthValue
"Content-Type" = "text/plain"
"charset" = "UTF-8"
}
$body = @"
erc.who: Taco, Ted
erc.what: Taco Recipe
erc.when: 1999
_target: https://google.com
_status: public
"@
$URI = $baseURL + "/shoulder/ark:/" + $naan + "/" + $shoulder
$response = Invoke-WebRequest -Method Post -URI $URI -Body $body -Headers $headers -UserAgent $userAgent
$ab = $response -split ":", 2
if ($ab[1] -match '\S') {
$ab[1] = $ab[1].Trim()
}
return $ab
}
$result = Mint-Ark -shoulder fk4
if ($result[0] -eq "success") {
Write-Host (-join("Minted:", " ", $result[1]))
Write-Host "Metadata:"
Get-ARKmetadata -ark $result[1]
} else {
Write-Host "Oops"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment