Skip to content

matt9ucci/Tavis.UriTemplates

This branch is 22 commits behind tavis-software/Tavis.UriTemplates:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Aug 20, 2024
64336e3 · Aug 20, 2024
Jul 5, 2024
Nov 30, 2014
Aug 20, 2024
May 24, 2012
Oct 28, 2021
May 24, 2012
Jan 7, 2015
Jun 28, 2023
Jun 28, 2023
Jun 28, 2023
Aug 24, 2022
Mar 19, 2019
Mar 19, 2019

Repository files navigation

Uri Templates

Build and deploy CodeQL NuGet

.NET implementation of the URI Template Spec RFC6570.

Library implements Level 4 compliance and is tested against test cases from UriTemplate test suite.

Here are some basic usage examples:

Replacing a path segment parameter,

[Fact]
public void UpdatePathParameter()
{
    var url = new UriTemplate("http://example.org/{tenant}/customers")
        .AddParameter("tenant", "acmé")
        .Resolve();

    Assert.Equal("http://example.org/acm%C3%A9/customers", url);
}

Setting query string parameters,

[Fact]
public void ShouldResolveUriTemplateWithNonStringParameter()
{
    var url = new UriTemplate("http://example.org/location{?lat,lng}")
        .AddParameters(new { lat = 31.464, lng = 74.386 })
        .Resolve();

    Assert.Equal("http://example.org/location?lat=31.464&lng=74.386", url);
}

Resolving a URI when parameters are not set will simply remove the parameters,

[Fact]
public void SomeParametersFromAnObject()
{
    var url = new UriTemplate("http://example.org{/environment}{/version}/customers{?active,country}")
        .AddParameters(new
        {
            version = "v2",
            active = "true"
        })
        .Resolve();

    Assert.Equal("http://example.org/v2/customers?active=true", url);
}

You can even pass lists as parameters

[Fact]
public void ApplyParametersObjectWithAListofInts()
{
    var url = new UriTemplate("http://example.org/customers{?ids,order}")
        .AddParameters(new
        {
            order = "up",
            ids = new[] {21, 75, 21}
        })
        .Resolve();

    Assert.Equal("http://example.org/customers?ids=21,75,21&order=up", url);
}

And dictionaries,

[Fact]
public void ApplyDictionaryToQueryParameters()
{
    var url = new UriTemplate("http://example.org/foo{?coords*}")
        .AddParameter("coords", new Dictionary<string, string>
        {
            {"x", "1"},
            {"y", "2"},
        })
        .Resolve();

    Assert.Equal("http://example.org/foo?x=1&y=2", url);
}

We also handle all the complex URI encoding rules automatically.

[Fact]
public void TestExtremeEncoding()
{
    var url = new UriTemplate("http://example.org/sparql{?query}")
            .AddParameter("query", "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who }")
            .Resolve();
    Assert.Equal("http://example.org/sparql?query=PREFIX%20dc%3A%20%3Chttps%3A%2F%2Flinproxy.fan.workers.dev%3A443%2Fhttp%2Fpurl.org%2Fdc%2Felements%2F1.1%2F%3E%20SELECT%20%3Fbook%20%3Fwho%20WHERE%20%7B%20%3Fbook%20dc%3Acreator%20%3Fwho%20%7D", url);
}

There is a blogpost that discusses these examples and more in detail.

As well as having a set of regular usage tests, this library also executes tests based on a standard test suite. This test suite is pulled in as a Git Submodule, therefore when cloning this repo, you will need use the --recursive switch,

    git clone --recursive git@github.com:tavis-software/Tavis.UriTemplates.git

Current this library does not pass all of the failure tests. I.e. If you pass an invalid URI Template, you may not get an exception.

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.7%
  • Batchfile 0.3%