MyNixOS website logo
Description

Library for parsing and expanding URI Templates, as per RFC 6570

As per the RFC,

For example, the following URI Template includes a form-style
parameter expression, as indicated by the "?" operator appearing
before the variable names.
http:/www.example.comfoo{?query,number}
The expansion process for expressions beginning with the question-
mark ("?") operator follows the same pattern as form-style interfaces
on the World Wide Web:
http:/www.example.comfoo{?query,number}
_____________/
|
|
For each defined variable in [ query, number ],
substitute "?" if it is the first substitution or "&"
thereafter, followed by the variable name, =, and the
variable's value.
If the variables have the values
query := "mycelium"
number := 100
then the expansion of the above URI Template is
http:/www.example.comfoo?query=mycelium&number=100
Alternatively, if query is undefined, then the expansion would be
http:/www.example.comfoo?number=100
or if both variables are undefined, then it would be
http:/www.example.comfoo

This library handles expanding such templates.

network-uri-template

Hackage Stackage Nightly Stackage LTS

Library for parsing and expanding URI Templates, as per RFC 6570.

URI Templates

An example from the RFC:

For example, the following URI Template includes a form-style parameter expression, as indicated by the "?" operator appearing before the variable names.

  http://www.example.com/foo{?query,number}

The expansion process for expressions beginning with the question- mark ("?") operator follows the same pattern as form-style interfaces on the World Wide Web:

  http://www.example.com/foo{?query,number}
                            \_____________/
                               |
                               |
          For each defined variable in [ 'query', 'number' ],
          substitute "?" if it is the first substitution or "&"
          thereafter, followed by the variable name, '=', and the
          variable's value.

If the variables have the values

  query  := "mycelium"
  number := 100

then the expansion of the above URI Template is

  http://www.example.com/foo?query=mycelium&number=100

Alternatively, if 'query' is undefined, then the expansion would be

  http://www.example.com/foo?number=100

or if both variables are undefined, then it would be

  http://www.example.com/foo

For a complete description of URI Templates, consult the RFC or see our extracted test cases.

Example

{-# LANGUAGE OverloadedStrings #-}

module Main
  ( main
  ) where

import Prelude

import Data.Text (Text)
import Data.Text.IO qualified as T
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Network.URI.Template
import System.Exit (die)

main :: IO ()
main = do
  let
    vars :: Map VarName VarValue
    vars =
      Map.fromList
        [ ("query", "mycelium")
        , ("number", "100")
        ]

    template :: Text
    template = "http://www.example.com/foo{?query,number}"

    handle = either (die . templateErrorPretty) T.putStrLn

  handle $ processTemplate vars template
  -- => http://www.example.com/foo?query=mycelium&number=100

  handle $ processTemplate (Map.delete "query" vars) template
  -- => http://www.example.com/foo?number=100

  handle $ processTemplate (Map.empty) template
  -- => http://www.example.com/foo

CLI

This project includes a small CLI to experiment with template expansion.

network-uri-template \
  --var 'query  := "mycelium"' \
  --var 'number := "100"' \
  --var 'path   := ("foo", "bar")' \
  --var 'keys   := [("sort","asc"), ("page","2")]' \
  'http://www.example.com{/path*}/foo{?query:2,number}{&keys*}'

License

This project is licensed AGPLv3. See COPYING.

Metadata

Version

0.1.1.4

Platforms (76)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows