Skip to content

Instantly share code, notes, and snippets.

@cdhunt
Created February 2, 2022 12:50
Show Gist options
  • Save cdhunt/fa4b457cce2df02f951e209f2609262a to your computer and use it in GitHub Desktop.
Save cdhunt/fa4b457cce2df02f951e209f2609262a to your computer and use it in GitHub Desktop.
# [CloudSmithClientPackagesGet]::new('stackoverflow','nuget').Invoke() | select Name, Version, Uploaded_At, Slug
class CloudSmithClient {
[string]$Namespace
[string]$Uri
[string]$Method
[string] hidden $ApiVersion = 'v1'
[string] hidden $Path
[hashtable] hidden $Headers = @{ }
[hashtable] hidden $Body = @{ }
[string] hidden $EnvName_Token = 'CLOUDSMITH_API_KEY'
[string] hidden $ConfigPath
CloudSmithClient($method, $path) {
$this.Path = $path
$this.Method = $method
if ($IsWindows) {
$this.ConfigPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath 'cloudsmith\credentials.ini'
}
if ($IsLinux) {
$this.ConfigPath = Join-Path -Path $env:HOME -ChildPath '/.config/cloudsmith/credentials.ini'
}
if ($IsMacOS) {
$this.ConfigPath = Join-Path -Path $env:HOME -ChildPath '/Library/Application Support/cloudsmith/credentials.ini'
}
if (Test-Path $this.ConfigPath) {
$search = Get-Content $this.ConfigPath | Select-String -Pattern "^api_key = (\w+)"
if ($search.Matches.Success -and $search.Matches.Groups.count -eq 2) {
$this.SetToken($search.Matches.Groups[1].Value)
}
}
if ([Environment]::GetEnvironmentVariables().Contains($this.EnvName_Token)) {
$this.SetToken([Environment]::GetEnvironmentVariable($this.EnvName_Token))
}
$this.ConstructUri()
}
[void] ConstructUri() {
$this.Uri = 'https://api.cloudsmith.io/{0}/{1}/{2}/' -f $this.ApiVersion, $this.Path, $this.Namespace
}
[CloudSmithClient] SetNamespace([string]$namespace) {
$this.Namespace = $namespace
$this.ConstructUri()
return $this
}
[CloudSmithClient] SetToken([string]$token) {
if ($this.Headers.ContainsKey('X-Api-Key')) {
$this.Headers['X-Api-Key'] = $token
}
else {
$this.Headers.Add('X-Api-Key', $token)
}
return $this
}
[object] Invoke() {
$parameters = @{
Uri = $this.Uri
Headers = $this.Headers
ContentType = 'application/json'
Method = $this.Method
}
if ($this.Body.Count -gt 0) {
$parameters["Body"] = '[{0}]' -f ($this.Body | ConvertTo-Json)
}
return Invoke-RestMethod @parameters
}
}
class CloudSmithClientPackages : CloudSmithClient {
[string]$Repository
CloudSmithClientPackages ($method, $repository) : base ($method, 'packages' ) {
$this.Repository = $repository
$this.ConstructUri()
}
[CloudSmithClientPackages] SetRepository([string]$repository) {
$this.Repository = $repository
$this.ConstructUri()
return $this
}
[void] ConstructUri() {
([CloudSmithClient]$this).ConstructUri()
$this.Uri = "{0}{1}" -f $this.Uri, $this.Repository
}
}
class CloudSmithClientPackagesGet : CloudSmithClientPackages {
CloudSmithClientPackagesGet ($repository): base ('Get', $repository) {}
CloudSmithClientPackagesGet ($namespace, $repository): base ('Get', $repository) {
$this.Namespace = $namespace
$this.ConstructUri()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment