|
|
rest:matching-request(
|
|
$options as element(rest:options),
|
|
[$uri as xs:string],
|
|
[$method as xs:string],
|
|
[$accept-headers as xs:string],
|
|
[$user-params as map:map]
|
| ) as element(rest:request)? |
|
 |
Summary:
This function returns the request element in the options node
that matches the specified URI. If you only specify options parameter, then the current
environment is used for all the other parameters.
|
Parameters:
$options
:
The options node.
|
$uri
(optional):
The URI with which to locate the matching request element.
|
$method
(optional):
The HTTP method to match with the request element.
|
$accept-headers
(optional):
The accept header.
The Accept request-header to match any accept conditions in the
request element.
|
$user-params
(optional):
The user parameters to match with those in the
request element.
|
|
Example:
xquery version "1.0-ml";
import module namespace rest="http://marklogic.com/appservices/rest"
at "/MarkLogic/appservices/utils/rest.xqy";
declare option xdmp:mapping "false";
let $options :=
<options xmlns="http://marklogic.com/appservices/rest">
<request uri="^/shakespeare/(.+)/(.+)" endpoint="/redirect.xqy">
<uri-param name="__ml_redirect__">/$1/$2</uri-param>
</request>
<request uri="^/shakespeare/(.+)" endpoint="/redirect.xqy">
<uri-param name="__ml_redirect__">/$1</uri-param>
</request>
<request uri="^/(.+)/act(\d+)" endpoint="/endpoint.xqy">
<uri-param name="play">$1.xml</uri-param>
<uri-param name="act" as="integer">$2</uri-param>
<param name="scene" as="integer" values="1|2|3" default="1"/>
</request>
<request uri="^/(.+)$" endpoint="/endpoint.xqy">
<uri-param name="play">$1.xml</uri-param>
</request>
<request uri="^(.+)$" endpoint="/options.xqy" user-params="allow">
<uri-param name="__ml_options__">$1</uri-param>
<http method="OPTIONS"/>
</request>
</options>
let $uri := "/shakespeare/hamlet"
let $accept := xdmp:get-request-header("Accept")
let $params := map:map()
return rest:matching-request($options, $uri, "GET", $accept, $params)
(: Returns the request node that matches the URI, "/shakespeare/hamlet". :)
|
|
|
|
rest:process-request(
|
|
$request as element(rest:request)
|
| ) as map:map |
|
 |
Summary:
This function is used in the endpoint main module to parse the incoming request
against the options. It returns a map that contains all of the parameters as typed values.
Processing the request also checks all of the associated conditions and will raise an error
if any condition is not met.
If the request is processed successfully, then all of the conditions have been met and the
returned map contains all of the parameters. If not, an error occurs.
|
Parameters:
$request
:
The request to be processed into a map.
|
|
Example:
xquery version "1.0-ml";
import module namespace rest="http://marklogic.com/appservices/rest"
at "/MarkLogic/appservices/utils/rest.xqy";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare option xdmp:mapping "false";
let $request :=
<rest:request uri="^/endpoint/(.+)/(\d+)$" endpoint="/endpoint.xqy">
<rest:uri-param name="play">$1.xml</rest:uri-param>
<rest:uri-param name="act" as="decimal" optional="true">$2</rest:uri-param>
</rest:request>
let $map := rest:process-request($request)
let $play := map:get($map, "play")
let $num := map:get($map, "act")
return
if (empty($num))
then
fn:doc($play)
else
fn:doc($play)/PLAY/ACT[$num]
(: The rest:process-request returns a map from $request that contains the
keys 'play' and 'act', which are use to return either the contents of
an entire play or a specific act in the play. :)
|
|
|