Skip to content
/ xqrs Public

XQuery API for RESTful Web Services. A RESTXQ Implementation for MarkLogic Server.

Notifications You must be signed in to change notification settings

cfoster/xqrs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XQRS - XQuery API for RESTful Web Services

XQRS is a RESTXQ implementation for MarkLogic Server.

It's fast, scales and is light weight at around 1.1k lines of code. You can seamlessly add it to your existing project without it interfering. Support is available on GitHub. RESTXQ enables you to build RESTful services declaratively with XQuery Annotations in the same way as JAX-RS does for Java.

RESTXQ resource functions

Create RESTful Services with URIs that work exactly how you want them to

declare
  %rest:path("/factory/warehouse/wheel/{$wheel-id}")
  %rest:GET
function get-wheel($wheel-id as xs:string) {
  fn:doc($wheel-id)
};

declare
  %rest:path("/factory/warehouse/wheel/{$wheel-id}")
  %rest:PUT("{$doc}")
  %xdmp:update
function put-wheel($wheel-id as xs:string, $doc as document-node(element())) {
  xdmp:document-insert($wheel-id, $doc, map:entry("collections", "wheels"))
};

declare
  %rest:path("/factory/warehouse/wheel")
function list-wheels() {
  <wheels>{
    fn:collection("wheels")
  }</wheels>
};

Basic Examples

There are some basic examples here on this GitHub README. Full Documentation is available on the website.

Regular Expressions in Paths

declare
%rest:path("/control-suffix/{$a}/{$b=.+}")
function control-suffix($a as xs:string, $b as xs:string) as xs:string {
  string-join(($a, $b), ',')
};

declare
  %rest:path("{$entire-uri=.+}")
function total-control($entire-uri as xs:string) {
  $entire-uri
};

Selecting function by Method

Method Annotations GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH

declare
  %rest:path("/factory/warehouse/wheel/{$wheel-id}")
  %rest:POST
function put($wheel-id as xs:string) {
  create-wheel($wheel-id)
};

declare
  %rest:path("/factory/warehouse/wheel/{$wheel-id}")
  %rest:DELETE
function delete($delete-id as xs:string) {
  delete-wheel($wheel-id)
};

declare
  %rest:path("/factory/warehouse/wheel/{$wheel-id}")
  %rest:GET
function get($delete-id as xs:string) {
  get-wheel($wheel-id)
};

PUT and POST Content

declare
  %rest:path("/query-some-triples")
  %rest:POST("{$rdf-data}")
function query-some-triples($rdf as sem:triple*) {
  sem:sparql(
    "SELECT * WHERE { ?a ?b ?c }", (), (),
    sem:in-memory-store($rdf)
  )
};

declare
  %rest:path("/{$uri}")
  %rest:PUT("{$json-data}")
function insert-json($json-data as document-node(object-node()), $uri as xs:string) {
  xdmp:document-insert($uri, $json-data),
  "Thanks"
};

Content Negotiation

declare
  %rest:path("/get-resource")
  %rest:produces("application/json") (: Client Accept Header :)
function json-response() {
  array-node { 1, 2, 3, 4 }
};

declare
  %rest:path("/get-resource")
  %rest:produces("text/xml", "application/xml") (: Client Accept Header :)
function xml-response() {
  <data>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
  </data>
};

Query Params

declare
  %rest:path("/search")
  %rest:query-param-1('query', '{$q}')
  %rest:query-param-2('from', '{$from}', 1)
  %rest:query-param-3('to', '{$to}', 10)
function perform-search(
  $q as xs:string,
  $from as xs:integer,
  $to as xs:integer) {
  <results>{
    cts:search(fn:doc(), cts:parse($q))[$from to $to]
  }</results>
};

Output Serialization

declare
  %rest:path("/get.ttl")
  %output:method("turtle")
function turtle() as sem:triple* {
  muh:triples()
};

declare
  %rest:path("/get.n3")
  %output:method("n3")
function n3() as sem:triple* {
  muh:triples()
};

declare
  %rest:path("/get.xml")
  %output:method("xml")
  %output:encoding("iso-8859-1")
  %output:indent("yes")
  %output:media-type("application/special+xml")
function get-xml() {
  muh:xml()
};

About

XQuery API for RESTful Web Services. A RESTXQ Implementation for MarkLogic Server.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published