GithubHelp home page GithubHelp logo

cfoster / xqrs Goto Github PK

View Code? Open in Web Editor NEW
41.0 2.0 1.0 95 KB

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

Home Page: http://consulting.xmllondon.com/xqrs

XQuery 58.34% XSLT 4.57% Scala 37.09%
marklogic rest restxq xquery restful

xqrs's Introduction

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()
};

xqrs's People

Contributors

cfoster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

pebsconsulting

xqrs's Issues

Authentication / Access Permissions / Application Level Authentication

This ticket is a place to discuss ideas around adding support for Application Level Authentication to XQRS.

Some ideas are as follows

declare
  %rest:path("/my/path")
  %rest:GET
  %xdmp:privilege("http://marklogic.com/xdmp/privileges/infostudio", "execute")
function exec() {
  (: function will only invoke if the logged in user has the above privilege :)
};
declare
  %rest:path("/my-basket")
  %rest:GET
  %xdmp:user("john.smith")
function exec() {
  (: function will only invoke if the logged in user is john.smith :)
};
declare
  %rest:path("/group-basket")
  %rest:GET
  %xdmp:role("tde-admin")
function exec() {
  (: function will only invoke if the logged in user has the role tde-admin :)
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.