Last active
June 23, 2021 02:40
-
-
Save joeypiccola/9e2766491d682d3db838ceb103965f4b to your computer and use it in GitHub Desktop.
Simple class example for an API Get call to thecatapi.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class cat_breed { | |
[string]$name | |
[string]$origin | |
[string]$id | |
[String]$alt_names | |
static [cat_breed[]] Get( | |
[string]$name, | |
[string]$token | |
) { | |
$uri = "https://api.thecatapi.com/v1/breeds/search?q=$name" | |
$headers = @{ | |
'x-api-key' = $token | |
} | |
$return = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers | |
$output = @() | |
if ($return) { | |
foreach ($item in $return) { | |
$output += [cat_breed]::New( | |
$item.name, | |
$item.origin, | |
$item.id, | |
$item.alt_names | |
) | |
} | |
return $output | |
} else { | |
return $null | |
} | |
} | |
static [cat_breed[]] Get( | |
[string]$alt_names, | |
[string]$token, | |
[string]$fake | |
) { | |
$uri = "https://api.thecatapi.com/v1/breeds/" | |
$headers = @{ | |
'x-api-key' = $token | |
} | |
$return = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers | |
$return = $return | Where-Object { $_.alt_names -like "*$alt_names*" } | |
$output = @() | |
if ($return) { | |
foreach ($item in $return) { | |
$output += [cat_breed]::New( | |
$item.name, | |
$item.origin, | |
$item.id, | |
$item.alt_names | |
) | |
} | |
return $output | |
} else { | |
return $null | |
} | |
} | |
cat_breed( | |
[string]$name, | |
[string]$origin, | |
[string]$id, | |
[string]$alt_names | |
) { | |
$this.name = $name | |
$this.origin = $origin | |
$this.id = $id | |
$this.alt_names = $alt_names | |
} | |
} | |
function Get-CatBreed { | |
[CmdletBinding()] | |
param ( | |
[Parameter(ParameterSetName='name')] | |
[string]$name, | |
[Parameter(ParameterSetName='alt_name')] | |
[string]$alt_name, | |
[Parameter(Mandatory)] | |
[string]$token | |
) | |
switch ($PSCmdlet.ParameterSetName) { | |
'name' { | |
[cat_breed]::get($name,$token) | |
} | |
'alt_name' { | |
[cat_breed]::get($alt_name,$token,$null) | |
} | |
Default { | |
Write-Output 'sigh' | |
} | |
} | |
} | |
<# | |
# find all cat breeds with "sib" in the name | |
PS /Users/jpiccola> Get-CatBreed -token $token -name 'sib' | |
name origin id alt_names | |
---- ------ -- --------- | |
Siberian Russia sibe Moscow Semi-longhair, HairSiberian Forest Cat | |
# find all cat breeds with "hair" in the breed's alternate names | |
PS /Users/jpiccola> Get-CatBreed -token $token -alt_name 'hair' | |
name origin id alt_names | |
---- ------ -- --------- | |
American Shorthair United States asho Domestic Shorthair | |
Balinese United States bali Long-haired Siamese | |
Chantilly-Tiffany United States ctif Chantilly, Foreign Longhair | |
Himalayan United States hima Himalayan Persian, Colourpoint Persian, Longhaired Colou… | |
Maine Coon United States mcoo Coon Cat, Maine Cat, Maine Shag, Snowshoe Cat, American … | |
Nebelung United States nebe Longhaired Russian Blue | |
Persian Iran (Persia) pers Longhair, Persian Longhair, Shiraz, Shirazi | |
Siberian Russia sibe Moscow Semi-longhair, HairSiberian Forest Cat | |
Somali Somalia soma Fox Cat, Long-Haired Abyssinian | |
Sphynx Canada sphy Canadian Hairless, Canadian Sphynx | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment