initial code commit

This commit is contained in:
tmenier 2014-02-16 17:49:54 -06:00
parent 3b3a1f719e
commit f2d0e3da21
14 changed files with 845 additions and 150 deletions

154
.gitignore vendored
View File

@ -1,151 +1,5 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
bin
obj
*.suo
*.user
*.sln.docstates
# Build results
[Dd]ebug/
[Rr]elease/
x64/
build/
[Bb]in/
[Oo]bj/
# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
!packages/*/build/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile
# Visual Studio profiler
*.psess
*.vsp
*.vspx
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
*.ncrunch*
.*crunch*.local.xml
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.Publish.xml
*.pubxml
# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/
# Windows Azure Build Output
csx
*.build.csdef
# Windows Store app package directory
AppPackages/
# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
App_Data/*.mdf
App_Data/*.ldf
# =========================
# Windows detritus
# =========================
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Mac crap
.DS_Store
*.nupkg
packages

6
.nuget/NuGet.Config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>

BIN
.nuget/NuGet.exe Normal file

Binary file not shown.

151
.nuget/NuGet.targets Normal file
View File

@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
<!-- Enable the restore command to run before builds -->
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
<!-- Property that enables building a package from a project -->
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
<!-- Download NuGet.exe if it does not already exist -->
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
</PropertyGroup>
<ItemGroup Condition=" '$(PackageSources)' == '' ">
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
<!--
<PackageSource Include="https://www.nuget.org/api/v2/" />
<PackageSource Include="https://my-nuget-source/nuget/" />
-->
</ItemGroup>
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
<!-- Windows specific commands -->
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
</PropertyGroup>
<PropertyGroup>
<PackagesProjectConfig>packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
</PropertyGroup>
<Choose>
<When Condition="Exists('$(PackagesProjectConfig)')">
<PropertyGroup>
<PackagesConfig>$(PackagesProjectConfig)</PackagesConfig>
</PropertyGroup>
</When>
<When Condition="Exists('packages.config')">
<PropertyGroup>
<PackagesConfig>packages.config</PackagesConfig>
</PropertyGroup>
</When>
</Choose>
<PropertyGroup>
<!-- NuGet command -->
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>
<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
<NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
<PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
<PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
<!-- Commands -->
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
<!-- We need to ensure packages are restored prior to assembly resolve -->
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>
<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
$(BuildDependsOn);
BuildPackage;
</BuildDependsOn>
</PropertyGroup>
<Target Name="CheckPrerequisites">
<!-- Raise an error if we're unable to locate nuget.exe -->
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
<!--
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
This effectively acts as a lock that makes sure that the download operation will only happen once and all
parallel builds will have to wait for it to complete.
-->
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
</Target>
<Target Name="_DownloadNuGet">
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
</Target>
<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(RestoreCommand)"
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
<Exec Command="$(RestoreCommand)"
LogStandardErrorAsError="true"
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>
<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(BuildCommand)"
Condition=" '$(OS)' != 'Windows_NT' " />
<Exec Command="$(BuildCommand)"
LogStandardErrorAsError="true"
Condition=" '$(OS)' == 'Windows_NT' " />
</Target>
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<OutputFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
OutputFilename = Path.GetFullPath(OutputFilename);
Log.LogMessage("Downloading latest version of NuGet.exe...");
WebClient webClient = new WebClient();
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{81F583F8-1D15-4E0B-8064-EB892042C09A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Flurl.Test</RootNamespace>
<AssemblyName>Flurl.Test</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\Flurl\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\Flurl\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="UrlBuilderTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Flurl\Flurl.csproj">
<Project>{70a34167-759e-4902-82e0-e6a84c2ce46f}</Project>
<Name>Flurl</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Flurl.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Flurl.Test")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c9cde065-a7bb-4cd9-b17e-234727627b78")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using NUnit.Framework;
namespace Flurl.Test
{
[TestFixture]
public class UrlBuilderTests
{
[Test]
public void Url_implicitly_converts_to_string() {
var url = new Url("http://www.mysite.com/more?x=1&y=2");
var someMethodThatTakesAString = new Action<string> (s => { });
someMethodThatTakesAString(url); // if this compiles, test passed.
}
[Test]
public void IsUrl_true_for_valid_url() {
Assert.IsTrue("http://www.mysite.com/more?x=1&y=2".IsUrl());
}
[Test]
public void IsUrl_false_for_invalid_url() {
Assert.IsFalse("not_a_url".IsUrl());
}
[Test]
public void constructor_parses_url() {
var url = new Url("http://www.mysite.com/more?x=1&y=2");
Assert.AreEqual("http://www.mysite.com/more", url.Path);
CollectionAssert.AreEqual(new NameValueCollection() {{ "x", "1" }, { "y", "2" }}, url.QueryParams);
}
[Test]
public void can_append_path_segment() {
var url = "http://www.mysite.com".AppendPathSegment("endpoint");
Assert.AreEqual("http://www.mysite.com/endpoint", url.ToString());
}
[Test]
public void appending_null_path_segment_throws_arg_null_ex() {
Assert.Throws<ArgumentNullException>(() => "http://www.mysite.com".AppendPathSegment(null));
}
[Test]
public void can_append_multiple_path_segments_by_multi_args() {
var url = "http://www.mysite.com".AppendPathSegments("category", "endpoint");
Assert.AreEqual("http://www.mysite.com/category/endpoint", url.ToString());
}
[Test]
public void can_append_multiple_path_segments_by_enumerable() {
IEnumerable<string> segments = new[] { "category", "endpoint" };
var url = "http://www.mysite.com".AppendPathSegments(segments);
Assert.AreEqual("http://www.mysite.com/category/endpoint", url.ToString());
}
[Test]
public void can_add_query_param() {
var url = "http://www.mysite.com".AddQueryParam("x", 1);
Assert.AreEqual("http://www.mysite.com?x=1", url.ToString());
}
[Test]
public void can_add_multiple_query_params_from_anon_object() {
var url = "http://www.mysite.com".AddQueryParams(new { x = 1, y = 2 });
Assert.AreEqual("http://www.mysite.com?x=1&y=2", url.ToString());
}
[Test]
public void can_add_multiple_query_params_from_dictionary() {
// let's challenge it a little with non-string keys
var url = "http://www.mysite.com".AddQueryParams(new Dictionary<int, string> {{1, "x"}, {2, "y"}});
Assert.AreEqual("http://www.mysite.com?1=x&2=y", url.ToString());
}
[Test]
public void can_remove_query_param() {
var url = "http://www.mysite.com/more?x=1&y=2".RemoveQueryParam("x");
Assert.AreEqual("http://www.mysite.com/more?y=2", url.ToString());
}
[Test]
public void can_remove_query_params_by_multi_args() {
var url = "http://www.mysite.com/more?x=1&y=2".RemoveQueryParams("x", "y");
Assert.AreEqual("http://www.mysite.com/more", url.ToString());
}
[Test]
public void can_remove_query_params_by_enumerable() {
var url = "http://www.mysite.com/more?x=1&y=2".RemoveQueryParams(new[] {"x", "y"});
Assert.AreEqual("http://www.mysite.com/more", url.ToString());
}
[Test]
public void can_do_crazy_long_fluent_expression() {
var url = "http://www.mysite.com"
.AddQueryParams(new { a = 1, b = 2, c = 999 })
.AppendPathSegment("category")
.RemoveQueryParam("c")
.AddQueryParam("z", 55)
.RemoveQueryParams("a", "z")
.AddQueryParams(new { n = "hi", m = "bye" })
.AppendPathSegment("endpoint");
Assert.AreEqual("http://www.mysite.com/category/endpoint?b=2&n=hi&m=bye", url.ToString());
}
[Test]
public void encodes_invalid_path_chars() {
var url = "http://www.mysite.com".AppendPathSegment("hey there how are ya");
Assert.AreEqual("http://www.mysite.com/hey%20there%20how%20are%20ya", url.ToString());
}
[Test]
public void does_not_reencode_path_escape_chars() {
var url = "http://www.mysite.com".AppendPathSegment("hey+there+how+are+ya");
Assert.AreEqual("http://www.mysite.com/hey+there+how+are+ya", url.ToString());
}
[Test]
public void encodes_query_params() {
var url = "http://www.mysite.com".AddQueryParams(new { x = "$50", y = "2+2=4" });
Assert.AreEqual("http://www.mysite.com?x=%2450&y=2%2b2%3d4", url.ToString());
}
}
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.3" targetFramework="net45" />
</packages>

33
Flurl.sln Normal file
View File

@ -0,0 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flurl", "Flurl\Flurl.csproj", "{70A34167-759E-4902-82E0-E6A84C2CE46F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flurl.Test", "Flurl.Test\Flurl.Test.csproj", "{81F583F8-1D15-4E0B-8064-EB892042C09A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{BA3FF997-39E0-49F7-AD38-C3B80225AC41}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{70A34167-759E-4902-82E0-E6A84C2CE46F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70A34167-759E-4902-82E0-E6A84C2CE46F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70A34167-759E-4902-82E0-E6A84C2CE46F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70A34167-759E-4902-82E0-E6A84C2CE46F}.Release|Any CPU.Build.0 = Release|Any CPU
{81F583F8-1D15-4E0B-8064-EB892042C09A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81F583F8-1D15-4E0B-8064-EB892042C09A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81F583F8-1D15-4E0B-8064-EB892042C09A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81F583F8-1D15-4E0B-8064-EB892042C09A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

63
Flurl/Flurl.csproj Normal file
View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{70A34167-759E-4902-82E0-E6A84C2CE46F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Flurl</RootNamespace>
<AssemblyName>Flurl</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Flurl.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\Flurl.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="Url.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="NuGet\" />
</ItemGroup>
<ItemGroup>
<None Include="Flurl.nuspec" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

15
Flurl/Flurl.nuspec Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>Flurl</id>
<version>0.1.0</version>
<title>Flurl</title>
<authors>Todd Menier</authors>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>Copyright 2014</copyright>
<tags>fluent url uri querystring builder</tags>
</metadata>
</package>

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Flurl")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Flurl")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f513ba12-2731-4a60-a7cd-7072d2195e14")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

107
Flurl/StringExtensions.cs Normal file
View File

@ -0,0 +1,107 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Flurl
{
/// <summary>
/// A set of string extension methods for working with Flurl URLs
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Checks if a string is a well-formed URL.
/// </summary>
/// <param name="s">the string to check</param>
/// <returns>true if s is a well-formed URL</returns>
public static bool IsUrl(this string s) {
return s != null && Uri.IsWellFormedUriString(s, UriKind.Absolute);
}
/// <summary>
/// Converts string to a Url object and appends a segment to the URL path,
/// ensuring there is one and only one '/' character as a seperator.
/// </summary>
/// <param name="segment">The segment to append</param>
/// <returns>the resulting Url object</returns>
public static Url AppendPathSegment(this string url, string segment) {
return new Url(url).AppendPathSegment(segment);
}
/// <summary>
/// Appends multiple segments to the URL path, ensuring there is one and only one '/' character as a seperator.
/// </summary>
/// <param name="segments">The segments to append</param>
/// <returns>the Url object with the segments appended</returns>
public static Url AppendPathSegments(this string url, params string[] segments) {
return new Url(url).AppendPathSegments(segments);
}
/// <summary>
/// Appends multiple segments to the URL path, ensuring there is one and only one '/' character as a seperator.
/// </summary>
/// <param name="segments">The segments to append</param>
/// <returns>the Url object with the segments appended</returns>
public static Url AppendPathSegments(this string url, IEnumerable<string> segments) {
return new Url(url).AppendPathSegments(segments);
}
/// <summary>
/// Converts string to a Url object and adds a parameter to the query string, overwriting the value if name exists.
/// </summary>
/// <param name="name">name of query string parameter</param>
/// <param name="value">value of query string parameter</param>
/// <returns>The Url obect with the query string parameter added</returns>
public static Url AddQueryParam(this string url, string name, object value) {
return new Url(url).AddQueryParam(name, value);
}
/// <summary>
/// Converts string to a Url object, parses values object into name/value pairs, and adds them to the query string,
/// overwriting any that already exist.
/// </summary>
/// <param name="values">Typically an anonymous object, ie: new { x = 1, y = 2 }</param>
/// <returns>The Url object with the query string parameters added</returns>
public static Url AddQueryParams(this string url, object values) {
return new Url(url).AddQueryParams(values);
}
/// <summary>
/// Converts string to a Url object and adds key/value pairs and to the query string,
/// overwriting any that already exist.
/// </summary>
/// <param name="values">Dictionary of key/value pairs to add to the query string</param>
/// <returns>The Url object with the query string parameters added</returns>
public static Url AddQueryParams(this string url, IDictionary values) {
return new Url(url).AddQueryParams(values);
}
/// <summary>
/// Converts string to a Url object and removes a name/value pair from the query string by name.
/// </summary>
/// <param name="name">Query string parameter name to remove</param>
/// <returns>The Url object with the query string parameter removed</returns>
public static Url RemoveQueryParam(this string url, string name) {
return new Url(url).RemoveQueryParam(name);
}
/// <summary>
/// Converts string to a Url object and removes multiple name/value pairs from the query string by name.
/// </summary>
/// <param name="names">Query string parameter names to remove</param>
/// <returns>The Url object with the query string parameters removed</returns>
public static Url RemoveQueryParams(this string url, params string[] names) {
return new Url(url).RemoveQueryParams(names);
}
/// <summary>
/// Converts string to a Url object and removes multiple name/value pairs from the query string by name.
/// </summary>
/// <param name="names">Query string parameter names to remove</param>
/// <returns>The Url object with the query string parameters removed</returns>
public static Url RemoveQueryParams(this string url, IEnumerable<string> names) {
return new Url(url).RemoveQueryParams(names);
}
}
}

193
Flurl/Url.cs Normal file
View File

@ -0,0 +1,193 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Web;
namespace Flurl
{
/// <summary>
/// Represents a URL that can be built fluently
/// </summary>
public class Url
{
/// <summary>
/// The full absolute path part of the URL (everthing except the query string).
/// </summary>
public string Path { get; private set; }
/// <summary>
/// Collection of all query string parameters.
/// </summary>
public NameValueCollection QueryParams { get; private set; }
/// <summary>
/// Constructs a Url object from a string.
/// </summary>
/// <param name="baseUrl">The URL to use as a starting point (required)</param>
public Url(string baseUrl) {
if (baseUrl == null)
throw new ArgumentNullException("baseUrl");
var parts = baseUrl.Split('?');
Path = parts[0];
// nice tip from John Bledsoe: http://stackoverflow.com/a/1877016/62600
QueryParams = HttpUtility.ParseQueryString(parts.Length > 1 ? parts[1] : "");
}
/// <summary>
/// Basically a Path.Combine for URLs. Ensures exactly one '/' character is used to seperate each segment.
/// URL-encodes illegal characters but not reserved characters.
/// </summary>
/// <param name="baseUrl">The URL to use as a starting point (required). NOT URL-encoded.</param>
/// <param name="segments">Paths to combine.</param>
/// <returns></returns>
public static string Combine(string baseUrl, params string[] segments) {
if (baseUrl == null)
throw new ArgumentNullException("baseUrl");
return new Url(baseUrl).AppendPathSegments(segments).ToString();
}
/// <summary>
/// Encodes characters that are strictly illegal in a URL. Does not encode reserved characters, i.e. '/', '+', etc.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string Cleanse(string url) {
return Uri.EscapeUriString(url);
}
/// <summary>
/// Appends a segment to the URL path, ensuring there is one and only one '/' character as a seperator.
/// </summary>
/// <param name="segment">The segment to append</param>
/// <param name="encode">If true, URL-encode the segment where necessary</param>
/// <returns>the Url object with the segment appended</returns>
public Url AppendPathSegment(string segment) {
if (segment == null)
throw new ArgumentNullException("segment");
if (!Path.EndsWith("/")) Path += "/";
Path += HttpUtility.UrlPathEncode(segment.TrimStart('/').TrimEnd('/'));
return this;
}
/// <summary>
/// Appends multiple segments to the URL path, ensuring there is one and only one '/' character as a seperator.
/// </summary>
/// <param name="segments">The segments to append</param>
/// <returns>the Url object with the segments appended</returns>
public Url AppendPathSegments(params string[] segments) {
Array.ForEach(segments, s => AppendPathSegment(s));
return this;
}
/// <summary>
/// Appends multiple segments to the URL path, ensuring there is one and only one '/' character as a seperator.
/// </summary>
/// <param name="segments">The segments to append</param>
/// <returns>the Url object with the segments appended</returns>
public Url AppendPathSegments(IEnumerable<string> segments) {
foreach(var s in segments)
AppendPathSegment(s);
return this;
}
/// <summary>
/// Adds a parameter to the query string, overwriting the value if name exists.
/// </summary>
/// <param name="name">name of query string parameter</param>
/// <param name="value">value of query string parameter</param>
/// <returns>The Url obect with the query string parameter added</returns>
public Url AddQueryParam(string name, object value) {
QueryParams[name] = (value == null) ? null : value.ToString();
return this;
}
/// <summary>
/// Parses object into name/value pairs and adds them to the query string, overwriting any that already exist.
/// </summary>
/// <param name="values">Typically an anonymous object, ie: new { x = 1, y = 2 }</param>
/// <returns>The Url object with the query string parameters added</returns>
public Url AddQueryParams(object values) {
if (values == null)
return this;
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(values))
AddQueryParam(prop.Name, prop.GetValue(values));
return this;
}
/// <summary>
/// Adds key/value pairs and to the query string, overwriting any that already exist.
/// </summary>
/// <param name="values">Dictionary of key/value pairs to add to the query string</param>
/// <returns>The Url object with the query string parameters added</returns>
public Url AddQueryParams(IDictionary values) {
if (values == null)
return this;
foreach (var key in values.Keys)
AddQueryParam(key.ToString(), values[key]);
return this;
}
/// <summary>
/// Removes a name/value pair from the query string by name.
/// </summary>
/// <param name="name">Query string parameter name to remove</param>
/// <returns>The Url object with the query string parameter removed</returns>
public Url RemoveQueryParam(string name) {
QueryParams.Remove(name);
return this;
}
/// <summary>
/// Removes multiple name/value pairs from the query string by name.
/// </summary>
/// <param name="names">Query string parameter names to remove</param>
/// <returns>The Url object with the query string parameters removed</returns>
public Url RemoveQueryParams(params string[] names) {
Array.ForEach(names, QueryParams.Remove);
return this;
}
/// <summary>
/// Removes multiple name/value pairs from the query string by name.
/// </summary>
/// <param name="names">Query string parameter names to remove</param>
/// <returns>The Url object with the query string parameters removed</returns>
public Url RemoveQueryParams(IEnumerable<string> names) {
foreach(var name in names)
QueryParams.Remove(name);
return this;
}
/// <summary>
/// Converts this Url object to its string representation.
/// </summary>
/// <returns></returns>
public override string ToString() {
var url = Path;
if (QueryParams.Count > 0)
url += "?" + QueryParams;
return url;
}
/// <summary>
/// Implicit conversion to string.
/// </summary>
/// <param name="url">the Url object</param>
/// <returns>The string</returns>
public static implicit operator string(Url url) {
return url.ToString();
}
}
}