|
| 1 | +function Get-Neocities |
| 2 | +{ |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Gets neocities information |
| 6 | + .DESCRIPTION |
| 7 | + Gets neocities information from the neocities API, or lists the files in your neocities site. |
| 8 | + .EXAMPLE |
| 9 | + Get-Neocities |
| 10 | + .EXAMPLE |
| 11 | + Get-Neocities -Credential $neocitiesCredential |
| 12 | + .EXAMPLE |
| 13 | + Get-Neocities -List |
| 14 | + #> |
| 15 | + [Alias('neocities')] |
| 16 | + [CmdletBinding(DefaultParameterSetName='info')] |
| 17 | + param( |
| 18 | + # If set, will list the files in your neocities site |
| 19 | + [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='list')] |
| 20 | + [switch] |
| 21 | + $List, |
| 22 | + |
| 23 | + # The credential used to connect. |
| 24 | + # This only needs to be provided once per module session |
| 25 | + # (every time the module is imported) |
| 26 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 27 | + [Alias( |
| 28 | + 'Credentials', # Plural aliases are nice |
| 29 | + 'PSCredential', # so are parameters that match the type name. |
| 30 | + 'NeocitiesCredential', # A contextual alias is a good idea, too. |
| 31 | + 'NeocitiesCredentials' # And you may need to pluralize that contextual alias. |
| 32 | + )] |
| 33 | + [PSCredential] |
| 34 | + $Credential, |
| 35 | + |
| 36 | + # The access token used to connect. |
| 37 | + # This only needs to be provided once per module session |
| 38 | + # (every time the module is imported) |
| 39 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 40 | + [string] |
| 41 | + $AccessToken |
| 42 | + ) |
| 43 | + |
| 44 | + begin { |
| 45 | + $NeocitiesApi = "https://neocities.org/api" |
| 46 | + } |
| 47 | + |
| 48 | + process { |
| 49 | + # The parameter set name contains the route |
| 50 | + $parameterSet = $PSCmdlet.ParameterSetName |
| 51 | + # and we want to use this to decorate all returned values with a type name |
| 52 | + $psuedoNamespace = "neocities" |
| 53 | + $pseudoType = "$parameterSet" |
| 54 | + # Start by constructing the parameters for Invoke-RestMethod |
| 55 | + $InvokeSplat = [Ordered]@{ |
| 56 | + Uri = "$NeocitiesApi", $PSCmdlet.ParameterSetName -join '/' |
| 57 | + } |
| 58 | + |
| 59 | + # If an access token was provided |
| 60 | + if ($AccessToken) |
| 61 | + { |
| 62 | + # use it |
| 63 | + $InvokeSplat.Headers = @{Authorization = "Bearer $AccessToken"} |
| 64 | + # and cache it for later use |
| 65 | + $script:NeocitiesAccessToken = $AccessToken |
| 66 | + } |
| 67 | + elseif ($Credential) |
| 68 | + { |
| 69 | + # If a credential was provided, use it |
| 70 | + $InvokeSplat.Credential = $Credential |
| 71 | + # and cache it for later use |
| 72 | + $script:NeoCitiesCredential = $Credential |
| 73 | + # (don't forget to set authentication to basic) |
| 74 | + $InvokeSplat.Authentication = 'Basic' |
| 75 | + } |
| 76 | + elseif ($script:NeocitiesAccessToken) { |
| 77 | + # If we had a cached access token, use it |
| 78 | + $InvokeSplat.Headers = @{Authorization = "Bearer $($script:NeocitiesAccessToken)"} |
| 79 | + } |
| 80 | + elseif ($script:NeoCitiesCredential) { |
| 81 | + # If we had a cached credential, use it |
| 82 | + $InvokeSplat.Credential = $script:NeoCitiesCredential |
| 83 | + # and don't forget to set authentication to basic. |
| 84 | + $InvokeSplat.Authentication = 'Basic' |
| 85 | + } |
| 86 | + |
| 87 | + # If neither an access token nor a credential was provided, we can't do anything. |
| 88 | + if (-not $InvokeSplat.Credential -and -not $InvokeSplat.Headers) |
| 89 | + { |
| 90 | + # so error out. |
| 91 | + Write-Error "No -Credential provided" |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + # Write a little verbose message to let the user know what we're doing |
| 97 | + Write-Verbose "Requesting $($InvokeSplat.Uri)" |
| 98 | + # and get a response from neocities. |
| 99 | + $neocitiesResponse = Invoke-RestMethod @InvokeSplat |
| 100 | + switch ($parameterSet) { |
| 101 | + info { |
| 102 | + # If we're getting info, we want to return the info object |
| 103 | + $neocitiesResponse = $neocitiesResponse.info |
| 104 | + } |
| 105 | + list { |
| 106 | + # If we're listing files, we want to return the files object |
| 107 | + $neocitiesResponse = @($neocitiesResponse.files) |
| 108 | + # and we want to return each as a 'file', not a 'list' |
| 109 | + $pseudoType = 'file' |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + # Go over each response |
| 114 | + foreach ($neoResponse in $neocitiesResponse) { |
| 115 | + # and decorate them with the type name |
| 116 | + $neoResponse.pstypenames.clear() |
| 117 | + $neoResponse.pstypenames.insert(0, ($psuedoNamespace, $pseudoType -join '.')) |
| 118 | + # and output the response. |
| 119 | + $neoResponse |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments