Browse files

Module now has naming based on WebContent instead of URL, should be m…

…ore intuitive
  • Loading branch information...
1 parent f2c36f2 commit e3d11c3dc51f673204abd7c52558c075f98c0311 @chaliy committed May 25, 2012
Showing with 139 additions and 40 deletions.
  1. +116 −21 PsUrl/PsUrl.psm1
  2. +13 −13 Readme.md
  3. +10 −6 Tests.ps1
View
137 PsUrl/PsUrl.psm1
@@ -10,6 +10,8 @@ Param(
[String]$ToFile,
[Management.Automation.PSCredential]$Credential
)
+ Write-Verbose "Get-Url is considered obsolete. Please use Get-WebContent instead"
+
$client = (New-Object Net.WebClient)
if ($Credential){
$ntwCred = $Credential.GetNetworkCredential()
@@ -41,6 +43,48 @@ Param(
#>
}
+function Get-WebContent {
+[CmdletBinding()]
+Param(
+ [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
+ [String]$Url,
+ [Management.Automation.PSCredential]$Credential
+)
+ $client = (New-Object Net.WebClient)
+ if ($Credential){
+ $ntwCred = $Credential.GetNetworkCredential()
+ $client.Credentials = $ntwCred
+ $auth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::Default.GetBytes($ntwCred.UserName + ":" + $ntwCred.Password))
+ $client.Headers.Add("Authorization", $auth)
+ }
+
+ try {
+ $client.DownloadString($Url)
+ } catch [System.Net.WebException] {
+ throw "Request failed: ""$($_.Exception.Message)"""
+ }
+
+<#
+.Synopsis
+ Downloads content from given url as a string.
+.Description
+.Parameter Url
+ URL to download
+.Parameter ToFile
+ Optional parameter to dowload stuff to the file.
+.Example
+ Get-WebContent http://chaliy.name
+
+ Description
+ -----------
+ Downloads content of the http://chaliy.name
+
+.Link
+ Send-WebContent
+
+#>
+}
+
function Write-Url {
[CmdletBinding()]
Param(
@@ -52,6 +96,48 @@ Param(
[Management.Automation.PSCredential]$Credential,
[String]$ContentType
)
+ Write-Verbose "Write-Url is considered obsolete. Please use Send-WebContent instead"
+ if ($Content -ne ""){
+ Send-WebContent -Url:$Url -Content:$Content -Timeout:$Timeout -Credential:$Credential -ContentType:$ContentType
+ } else {
+ Send-WebContent -Url:$Url -Data:$Data -Timeout:$Timeout -Credential:$Credential -ContentType:$ContentType
+ }
+<#
+.Synopsis
+ POST values to URL
+.Description
+.Parameter Url
+ URL to POST
+.Parameter Data
+ Hashtable of the data to post.
+.Parameter Timeout
+ Optional timeout value, by default timeout is 1 minute.
+.Parameter ContentType
+ Adds Content-Type header to request
+.Example
+ Write-Url http://chaliy.name -Data @{"Foo" = "Bar" }
+
+ Description
+ -----------
+ POST's to the http://chaliy.name as application/x-www-form-urlencoded
+
+#>
+}
+
+
+function Send-WebContent {
+[CmdletBinding()]
+Param(
+ [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
+ [String]$Url,
+ [Parameter(ParameterSetName='Data')]
+ [HashTable]$Data,
+ [Parameter(ParameterSetName='Content')]
+ [String]$Content,
+ [TimeSpan]$Timeout = [TimeSpan]::FromMinutes(1),
+ [Management.Automation.PSCredential]$Credential,
+ [String]$ContentType
+)
try{
@@ -69,27 +155,27 @@ Param(
if ($ContentType -ne ""){
$req.ContentType = $ContentType
}
-
- if ($Content -ne ""){
- $reqStream = $req.GetRequestStream()
- $reqBody = [Text.Encoding]::Default.GetBytes($Content)
- $reqStream.Write($reqBody, 0, $reqBody.Length)
-
- } else {
-
- Add-Type -AssemblyName System.Web
- $formData = [Web.HttpUtility]::ParseQueryString("")
- foreach($key in $Data.Keys){
- $formData.Add($key, $Data[$key])
+
+ switch($PSCmdlet.ParameterSetName) {
+ Content {
+ $reqStream = $req.GetRequestStream()
+ $reqBody = [Text.Encoding]::Default.GetBytes($Content)
+ $reqStream.Write($reqBody, 0, $reqBody.Length)
}
- $reqBody = [Text.Encoding]::Default.GetBytes($formData.ToString())
-
- $req.ContentType = "application/x-www-form-urlencoded"
- $reqStream = $req.GetRequestStream()
- $reqStream.Write($reqBody, 0, $reqBody.Length)
+ Data {
+ Add-Type -AssemblyName System.Web
+ $formData = [Web.HttpUtility]::ParseQueryString("")
+ foreach($key in $Data.Keys){
+ $formData.Add($key, $Data[$key])
+ }
+ $reqBody = [Text.Encoding]::Default.GetBytes($formData.ToString())
+ $req.ContentType = "application/x-www-form-urlencoded"
+ $reqStream = $req.GetRequestStream()
+ $reqStream.Write($reqBody, 0, $reqBody.Length)
+ }
}
-
+
$reqStream.Close()
$Method = $req.Method
@@ -125,15 +211,24 @@ Param(
.Parameter ContentType
Adds Content-Type header to request
.Example
- Write-Url http://chaliy.name -Data @{"Foo" = "Bar" }
+ Send-WebContent http://chaliy.name -Data @{"Foo" = "Bar" }
Description
-----------
POST's to the http://chaliy.name as application/x-www-form-urlencoded
+.Link
+ Get-WebContent
#>
}
-Export-ModuleMember Get-Url
-Export-ModuleMember Write-Url
+Export-ModuleMember Get-Url #Obsolete
+Export-ModuleMember Write-Url #Obsolete
+
+Set-Alias gwc Get-WebContent
+Set-Alias swc Send-WebContent
+Export-ModuleMember Get-WebContent
+Export-ModuleMember Send-WebContent
+Export-ModuleMember -Alias gwc
+Export-ModuleMember -Alias swc
View
26 Readme.md
@@ -1,43 +1,43 @@
-PsUrl Utils
-=============
+# PsUrl Utils
Set of commands to download and post to web
-Features
-========
+# Features
1. Download content from any URL as a string
2. Download content from any URL to file
3. POST url encoded form to any URL
-Example
-=======
+# Examples
For example to download stuff from http://example.com execute
- get-url http://example.com
+ get-webcontent http://example.com
You can also use pipes
- get-url http://example.com | set-content example.html
+ get-webcontent http://example.com | set-content example.html
Using pipes actully allows running scripts directly from web
- get-url https://gist.github.com/raw/909561/hello_world.ps1 | invoke-expression
+ get-webcontent https://gist.github.com/raw/909561/hello_world.ps1 | invoke-expression
Another option is to download to file
get-url http://example.com -ToFile "example.html"
-Installation
-============
+# Installation
-If you have <a href="https://github.com/chaliy/psget">PsGet</a> installed, you can execute:
+If you have <a href="https://github.com/psget/psget">PsGet</a> installed, you can execute:
install-module PsUrl
Or manual steps
1. Copy PsUrl.psm1 to your modules folder (e.g. $Env:PSModulePath\PsUrl\ )
2. Execute Import-Module PsUrl (or add this command to your profile)
- 3. Enjoy!
+ 3. Enjoy!
+
+# License
+
+Licensed under the MIT
View
16 Tests.ps1
@@ -1,13 +1,17 @@
$here = (Split-Path -parent $MyInvocation.MyCommand.Definition)
-import-module -name ($here + "\PsUrl\PsUrl.psm1")
+import-module -name ($here + "\PsUrl\PsUrl.psm1") -force
write-host Should support downloading stuff
-get-url "http://example.com" | out-null
+get-webcontent "http://example.com" | out-null
+
+write-host Should support 404
+get-webcontent "http://us.blizzard.com/en-us/404/" -ErrorAction:SilentlyContinue
write-host Should support downloading to file
-get-url "http://example.com" -ToFile "example.html"
-if (-not (test-path "example.html")){ write-error "example.html was not downloaded" }
-remove-item "example.html"
+get-url "http://example.com" -ToFile "$here\example.html"
+if (-not (test-path "$here\example.html")){ write-error "example.html was not downloaded" }
+remove-item "$here\example.html"
write-host Should support posting stuff
-write-url "http://example.com" -Data @{"Foo" = "Bar"} | out-null
+send-webcontent "http://example.com" -Data @{"Foo" = "Bar"} | out-null
+

0 comments on commit e3d11c3

Please sign in to comment.