Skip to content
Browse files

discovery index for data API, closes #20

  • Loading branch information...
1 parent f3812ff commit f6cab9e9e7a2ab74a537cb69e04ade9cd7359b5e @EfraimFeinstein EfraimFeinstein committed May 21, 2012
View
69 code/api/data/dindex.xqm
@@ -0,0 +1,69 @@
+xquery version "3.0";
+(: API module for functions for index URIs
+ :
+ : Functions assume that the following has already been done:
+ : authentication,
+ : content negotiation
+ :
+ : Copyright 2012 Efraim Feinstein <efraim@opensiddur.org>
+ : Open Siddur Project
+ : Licensed Under the GNU Lesser General Public License, version 3 or later
+ :)
+module namespace dindex = 'http://jewishliturgy.org/api/data/index';
+
+declare namespace rest="http://exquery.org/ns/rest/annotation/";
+declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
+declare namespace o="http://a9.com/-/spec/opensearch/1.1/";
+
+(:~ list all available data APIs :)
+declare
+ %rest:GET
+ %rest:path("/data")
+ %rest:produces("application/xhtml+xml", "text/html", "application/xml", "text/xml")
+ function dindex:list(
+ ) as item()+ {
+ <rest:response>
+ <output:serialization-parameters>
+ <output:method value="html5"/>
+ </output:serialization-parameters>
+ </rest:response>,
+ <html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>Open Siddur API Index</title>
+ </head>
+ <body>
+ <ul class="results">
+ <li class="result">
+ {((: TODO: replace request:get-uri() with rest:get-absolute-uri()
+ :))}
+ <a class="discovery" href="{request:get-uri()}/transliteration">Transliteration</a>
+ </li>
+ </ul>
+ </body>
+ </html>
+};
+
+declare
+ %rest:GET
+ %rest:path("/data/OpenSearchDescription")
+ %rest:query-param("source", "{$source}", "")
+ %rest:produces("application/opensearchdescription+xml","application/xml","text/xml")
+ function dindex:open-search(
+ $source as xs:string
+ ) as item()+ {
+ <rest:response>
+ <output:serialization-parameters>
+ <output method="xml"/>
+ </output:serialization-parameters>
+ </rest:response>,
+ <o:OpenSearchDescription
+ xmlns:jsearch="http://jewishliturgy.org/ns/search">
+ <o:ShortName>Open Siddur Search</o:ShortName>
+ <o:Description>Full text search of Open Siddur texts.</o:Description>
+ <o:Tags>siddur</o:Tags>
+ <o:Contact>efraim@opensiddur.org</o:Contact>
+ <o:Url type="application/xhtml+xml"
+ template="{$source}?q={{searchTerms}}&amp;start={{startIndex?}}&amp;max-results={{count?}}"
+ />
+ </o:OpenSearchDescription>
+};
View
1 code/queries/pre-release-testing.xql
@@ -14,6 +14,7 @@ let $tests-to-run :=
<test module="/code/tests/api/access.t.xml" />
<test module="/code/tests/api/login.t.xml"/>
<test module="/code/tests/modules/mirror.t.xml" admin="1"/>
+ <test module="/code/tests/api/data/dindex.t.xml"/>
<test module="/code/tests/api/data/transliteration.t.xml"/>
<test module="/code/tests/transforms/translit/translit.t.xml"/>
</tests>
View
65 code/tests/api/data/data.t.xml
@@ -1,65 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<TestSuite
- xmlns:tei="http://www.tei-c.org/ns/1.0"
- xmlns:j="http://jewishliturgy.org/ns/jlptei/1.0"
- xmlns:jx="http://jewishliturgy.org/ns/jlp-processor"
- xmlns:http="http://expath.org/ns/http-client">
- <suiteName>Data API index test</suiteName>
- <description>
- <p>Test the data API index</p>
- <author>Efraim Feinstein</author>
- <copyright>Copyright 2012 Efraim Feinstein,
- Licensed under the GNU Lesser General Public License, version 3 or later</copyright>
- </description>
- <setup/>
- <namespace prefix="html">http://www.w3.org/1999/xhtml</namespace>
- <imports>
- import module namespace apitest="http://jewishliturgy.org/modules/apitest"
- at "xmldb:exist:///code/tests/api/apitest.xqm";
- </imports>
- <TestSet>
- <testName>HTTP REST calls</testName>
- <setup/>
- <imports>
- </imports>
- <functions>
- </functions>
- <tearDown/>
- <test>
- <task>GET accepting HTML</task>
- <code><![CDATA[
- apitest:get("/code/api/data", <header name="Accept" value="application/xhtml+xml"/>)
- ]]></code>
- <class href="../common.t.xml#SelfConsistentMenu"/>
- <class href="../common.t.xml#Testing"/>
- </test>
- <test>
- <task>GET accepting text/plain</task>
- <code><![CDATA[
- apitest:get("/code/api/data", <header name="Accept" value="text/plain"/>)
- ]]></code>
- <class href="../common.t.xml#Unacceptable"/>
- </test>
- <test>
- <task>PUT</task>
- <code><![CDATA[
- apitest:put("/code/api/data", (), <x/>)
- ]]></code>
- <class href="../common.t.xml#InvalidMethod"/>
- </test>
- <test>
- <task>POST</task>
- <code><![CDATA[
- apitest:post("/code/api/data", (), <x/>)
- ]]></code>
- <class href="../common.t.xml#InvalidMethod"/>
- </test>
- <test>
- <task>DELETE</task>
- <code><![CDATA[
- apitest:delete("/code/api/data", ())
- ]]></code>
- <class href="../common.t.xml#InvalidMethod"/>
- </test>
- </TestSet>
-</TestSuite>
View
44 code/tests/api/data/dindex.t.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<TestSuite
+ xmlns:tei="http://www.tei-c.org/ns/1.0"
+ xmlns:j="http://jewishliturgy.org/ns/jlptei/1.0"
+ xmlns:jx="http://jewishliturgy.org/ns/jlp-processor"
+ xmlns:o="http://a9.com/-/spec/opensearch/1.1/"
+ xmlns:http="http://expath.org/ns/http-client">
+ <suiteName>Data API index test</suiteName>
+ <description>
+ <p>Test the data API index</p>
+ <author>Efraim Feinstein</author>
+ <copyright>Copyright 2012 Efraim Feinstein,
+ Licensed under the GNU Lesser General Public License, version 3 or later</copyright>
+ </description>
+ <setup/>
+ <namespace prefix="html">http://www.w3.org/1999/xhtml</namespace>
+ <namespace prefix="o">http://a9.com/-/spec/opensearch/1.1/</namespace>
+ <imports>
+ import module namespace dindex="http://jewishliturgy.org/api/data/index"
+ at "xmldb:exist:///code/api/data/dindex.xqm";
+ </imports>
+ <TestSet>
+ <testName>dindex:list()</testName>
+ <test>
+ <task>GET</task>
+ <code><![CDATA[
+ dindex:list()
+ ]]></code>
+ <class href="../common.t.xml#SerializeHTML5"/>
+ <xpath desc="returns a results class">.//html:*[@class='results']</xpath>
+ <xpath desc="returns at least one result class">count(.//html:li[@class='result']) &gt;= 1</xpath>
+ </test>
+ </TestSet>
+ <TestSet>
+ <testName>dindex:open-search()</testName>
+ <test>
+ <task>GET, no source</task>
+ <code><![CDATA[
+ dindex:open-search("")
+ ]]></code>
+ <xpath desc="An OpenSearch description is returned">self::o:OpenSearchDescription</xpath>
+ </test>
+ </TestSet>
+</TestSuite>
View
36 data/controller.xql
@@ -1,9 +1,35 @@
-xquery version "1.0";
-(: this controller simply redirects to /code/api :)
+xquery version "3.0";
+(: This controller behaves like a fill-in for RESTXQ.
+ : It does not do content negotiation.
+ : Copyright 2012 Efraim Feinstein <efraim@opensiddur.org>
+ : Licensed under the GNU Lesser General Public License, version 3 or later
+ :)
+import module namespace api="http://jewishliturgy.org/modules/api"
+ at "xmldb:exist:///code/api/modules/api.xqm";
+import module namespace dindex="http://jewishliturgy.org/api/data/index"
+ at "xmldb:exist:///code/api/data/dindex.xqm";
+
declare namespace exist="http://exist.sourceforge.net/NS/exist";
declare variable $exist:path external;
+declare variable $exist:resource external;
-<exist:dispatch>
- <exist:redirect url="/code/api/data/{$exist:path}"/>
-</exist:dispatch>
+let $uri := request:get-uri()
+let $authenticated :=
+ (: log in if you can, otherwise, let the access be checked
+ : by the called function
+ :)
+ api:request-authentication()
+return
+ api:rest-response(
+ switch (api:get-method())
+ case "GET"
+ return
+ if ($exist:resource="OpenSearchDescription")
+ then
+ dindex:open-search(request:get-parameter("source", ""))
+ else
+ dindex:list()
+ default
+ return api:rest-error(405, "Method not allowed")
+ )

0 comments on commit f6cab9e

Please sign in to comment.
Something went wrong with that request. Please try again.