-
Notifications
You must be signed in to change notification settings - Fork 14
/
New-APIHelperFunction.ps1
84 lines (63 loc) · 2.78 KB
/
New-APIHelperFunction.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function New-APIHelperFunction ($ApiResource, $Path) {
$Verb = Switch ($ApiResource.Method) {
'GET' { 'Get' }
'POST' { 'Send' }
'DELETE' { 'Remove' }
}
$ResourceName = (Get-Culture).TextInfo.ToTitleCase($ApiResource.Resource)
$ResourceNames = ($Resourcename.TrimStart("/").Split("/") | ForEach-Object { "$_".Replace("_","") })
$ResourceName = $ResourceNames[0] + '_' + ($ResourceNames[1..999] -Join "" -Replace ":", "_")
$FunctionName = "${Verb}-Twitter${ResourceName}"
$HelpParameters = @()
$FunctionParameters = @()
ForEach ($Parameter in $ApiResource.ReferenceProperties.Parameters) {
$HelpParameters += ".PARAMETER $($Parameter.Name)"
$HelpParameters += " $($Parameter.Description)`r`n"
$FunctionParameters += "[string]`$$(($Parameter.Name -Split " " -Split "\.")[0])"
}
$FunctionString = @"
function ${FunctionName} {
<#
.SYNOPSIS
$($ApiResource.ReferenceProperties.fDescription)
.DESCRIPTION
$($ApiResource.ReferenceProperties.Description -Split "`r`n" -Join "`r`n ")
$($HelpParameters -Join "`r`n")
.NOTES
This helper function was generated by the information provided here:
$($ApiResource.ReferenceUrl)
#>
[CmdletBinding()]
Param(
$($FunctionParameters -Join ",`r`n ")
)
Begin {
[hashtable]`$Parameters = `$PSBoundParameters
`$CmdletBindingParameters | ForEach-Object { `$Parameters.Remove(`$_) }
[string]`$Method = '$($ApiResource.Method)'
[string]`$Resource = '$($ApiResource.Resource)'
[string]`$ResourceUrl = '$($ApiResource.ReferenceProperties.ResourceUrl)'
}
Process {
# Find & Replace any ResourceUrl parameters.
`$UrlParameters = [regex]::Matches(`$ResourceUrl, '(?<!\w):\w+')
ForEach (`$UrlParameter in `$UrlParameters) {
`$UrlParameterValue = `$Parameters["`$(`$UrlParameter.Value.TrimStart(":"))"]
`$ResourceUrl = `$ResourceUrl -Replace `$UrlParameter.Value, `$UrlParameterValue
}
`$OAuthSettings = Get-TwitterOAuthSettings -Resource `$Resource
Invoke-TwitterAPI -Method `$Method -ResourceUrl `$ResourceUrl -Parameters `$Parameters -OAuthSettings `$OAuthSettings
}
End {
}
}
"@
$FunctionString | Set-Content "${Path}\${FunctionName}.ps1" -Encoding UTF8
}
$Path = New-Item -Path '.\helper' -ItemType Directory -Force
$ApiResources = Get-Content ".\twitter_api.json" | ConvertFrom-Json
ForEach ($ApiResource in ($ApiResources | Where-Object { $_.ReferenceProperties.ResourceUrl })) {
#if ($ApiResource.Resource -ne '/account/settings') { Continue } ## Used for Debuging
Write-Warning "New-APIHelperFunction: $($ApiResource.Resource)"
New-APIHelperFunction -ApiResource $ApiResource -Path $Path
}