Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to eXist 5 #376

Merged
merged 40 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
6fa2622
apply some fixes for current eXist-db 4.7
peterstadler May 28, 2019
0ba0ded
add collation for sorting of facets
peterstadler May 29, 2019
1072455
some speedups for eXist 4.7
peterstadler Apr 27, 2020
bdf098c
add `norm:create-norm-doc-documents()`
peterstadler Apr 28, 2020
d3c2c8a
response data is always an array
peterstadler Apr 28, 2020
2681650
Merge branch 'develop' into test-4.7
peterstadler Sep 25, 2020
77e9fda
Merge branch 'develop' into test-4.7
peterstadler Sep 29, 2020
9c7ed8f
Merge branch 'develop' into test-4.7
peterstadler Sep 29, 2020
4005093
move functions
peterstadler Sep 30, 2020
f58fbbd
refactor to use eXist5 facets and fields
peterstadler Sep 30, 2020
c3b3eab
remove outdated stuff
peterstadler Sep 30, 2020
46eadb8
move `strip-diacritics()` to WeGA-WebApp-lib str module
peterstadler Sep 30, 2020
21af8d5
get rid of norm module completely
peterstadler Sep 30, 2020
186c334
fix `core:undated`
peterstadler Sep 30, 2020
9a3766c
declare namespace
peterstadler Sep 30, 2020
df76556
remove superfluous lines
peterstadler Oct 1, 2020
afcf3b7
refactor `core:undated()` to use date filter
peterstadler Oct 1, 2020
5d40748
add function `core:index-keys-for-field()`
peterstadler Oct 1, 2020
82b7b6c
fix filtering
peterstadler Oct 2, 2020
2906dd2
Merge branch 'develop' into test-4.7
peterstadler Oct 8, 2020
ec89ce4
set semver-min
peterstadler Nov 9, 2020
65baecd
update processing/filtering of tests
peterstadler Nov 9, 2020
b54447f
Merge branch 'develop' into test-4.7
peterstadler Nov 23, 2020
a3a8f94
Merge branch 'develop' into test-4.7
peterstadler Nov 23, 2020
c0f04b8
update Dockerfile to use eXist5.2.0
peterstadler Nov 30, 2020
fb4ef35
use local curl binary
peterstadler Nov 30, 2020
b4215c1
update (some) expected results
peterstadler Nov 30, 2020
d03a894
add dependencies
peterstadler Nov 30, 2020
99b486d
getting saxon from sourceforge
peterstadler Nov 30, 2020
dff6de5
try to reduce memory consumption
peterstadler Nov 30, 2020
231cb2c
normalize characterNames
peterstadler Nov 30, 2020
f450351
update expected results
peterstadler Nov 30, 2020
b049fd2
try more recent distro for travis
peterstadler Nov 30, 2020
307dd3a
Merge branch 'develop' into test-4.7
peterstadler Dec 2, 2020
894c153
ups, never meant to commit these files
peterstadler Dec 2, 2020
5d8139a
exclude xpr-files from the xar package
peterstadler Dec 2, 2020
33b672d
Merge branch 'develop' into test-4.7
peterstadler Dec 2, 2020
914136e
refactor some API endpoints
peterstadler Dec 7, 2020
ea19a17
Merge branch 'develop' into test-4.7
peterstadler Dec 8, 2020
a9581e8
update Readme
peterstadler Dec 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor some API endpoints
  • Loading branch information
peterstadler committed Dec 7, 2020
commit 914136eab226eb204854e77e597a7d2fe9fc3c57
19 changes: 11 additions & 8 deletions api/v1/controller.xql
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,26 @@ declare variable $local:url-parameters as map(*) := map:merge((
~:)
declare function local:serialize-json($response as item()*) {
let $serializationParameters := ('method=text', 'media-type=application/json', 'encoding=utf-8')
let $responseBody :=
if($response instance of map(*) and map:contains($response,'results')) then $response?results
else if($response instance of map(*) and map:contains($response,'core')) then $response
else map {'code' : 500, 'message' : 'Internal server error', 'fields' : ''}
let $setHeader1 := response:set-header('cache-control','max-age=0, no-cache, no-store')
let $setHeader2 := response:set-header('pragma','no-cache')
let $setHeader3 :=
if($response[1] instance of map(*) and map:contains($response[1],'code'))
then response:set-status-code($response[1]?code)
if($response instance of map(*) and map:contains($response,'code'))
then response:set-status-code($response?code)
else if($responseBody instance of map(*) and map:contains($responseBody,'code'))
then response:set-status-code($responseBody?code)
else ()
let $setHeader4 :=
if($response[1] instance of map(*) and map:contains($response[1],'totalRecordCount'))
then response:set-header('totalRecordCount', $response[1]?totalRecordCount)
if($response instance of map(*) and map:contains($response,'totalRecordCount'))
then response:set-header('totalRecordCount', $response?totalRecordCount)
else ()
let $setHeader5 := response:set-header('Access-Control-Allow-Origin', '*')
(:let $setHeader3 :=
if(exists($response)) then response:set-header('ETag', util:hash($response, 'md5'))
else ():)
return
response:stream(
serialize(subsequence($response, if(count($response) gt 1) then 2 else 1),
serialize($responseBody,
<output:serialization-parameters>
<output:method>json</output:method>
</output:serialization-parameters>
Expand Down
47 changes: 42 additions & 5 deletions api/v1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,39 @@
"produces": ["application/json"],
"responses": {
"200": {
"description": "A single object with status information",
"description": "Healthy \u2013 A single object with status information",
"schema": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"healthy",
"unhealthy"
],
"default": "healthy",
"description": "Status (healthy|unhealthy) of the currently running WeGA-WebApp"
},
"svnRevision": {
"type": "integer",
"description": "The Subversion revision of the currently installed WeGA-data"
},
"deployment": {
"type": "string",
"format": "date-time",
"description": "The datetime of the deployment of the WeGA-WebApp"
},
"version": {
"type": "string",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
}
}
}
},
"500": {
"description": "Unhealthy \u2013 A single object with status information",
"schema": {
"type": "object",
"properties": {
"status": {
"type": "string",
"default": "unhealthy",
"description": "Status (healthy|unhealthy) of the currently running WeGA-WebApp"
},
"svnRevision": {
Expand Down Expand Up @@ -336,6 +359,20 @@
}
}
},
"403": {
"description": "The creation of new IDs is only available in the development environment",
"schema": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32",
"default": 403
},
"message": {"type": "string"}
}
}
},
"default": {
"description": "Unexpected error",
"schema": {"$ref": "#/definitions/Error"}
Expand Down
127 changes: 72 additions & 55 deletions modules/api.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,29 @@ declare variable $api:max-limit := function($swagger-conf as map(*)) as xs:integ
$swagger-conf?parameters?limitParam?maximum
};

declare function api:documents($model as map(*)) as map(*)* {
declare function api:documents($model as map(*)) as map(*) {
let $ids :=
if(exists($model('docID'))) then api:findByID($model('docID'))
else for $docType in api:resolve-docTypes($model) return core:getOrCreateColl($docType, 'indices', true())
return (
map { 'totalRecordCount': count($ids) },
api:document(api:subsequence($ids, $model), $model)
map {
'totalRecordCount': count($ids),
'results': api:document(api:subsequence($ids, $model), $model)
}
)
};

declare function api:documents-findByDate($model as map(*)) as map(*)* {
declare function api:documents-findByDate($model as map(*)) as map(*) {
let $documents := for $docType in api:resolve-docTypes($model) return wdt:lookup($docType, core:getOrCreateColl($docType, 'indices', true()))?filter-by-date($model?fromDate, $model?toDate)
return (
map { 'totalRecordCount': count($documents) },
api:document(api:subsequence($documents, $model), $model)
map {
'totalRecordCount': count($documents),
'results': api:document(api:subsequence($documents, $model), $model)
}
)
};

declare function api:documents-findByMention($model as map(*)) as map(*)* {
declare function api:documents-findByMention($model as map(*)) as map(*) {
let $mentioned-doc := api:findByID($model('docID'))
let $backlinks :=
if($mentioned-doc)
Expand All @@ -65,12 +69,14 @@ declare function api:documents-findByMention($model as map(*)) as map(*)* {
for $docType in api:resolve-docTypes($model)
return wdt:lookup($docType, $backlinks)('filter')()
return (
map { 'totalRecordCount': count($documents) },
api:document(api:subsequence($documents, $model), $model)
map {
'totalRecordCount': count($documents),
'results': api:document(api:subsequence($documents, $model), $model)
}
)
};

declare function api:documents-findByAuthor($model as map(*)) as map(*)* {
declare function api:documents-findByAuthor($model as map(*)) as map(*) {
let $author := api:findByID($model('authorID'))
let $documents :=
if($author)
Expand All @@ -80,8 +86,10 @@ declare function api:documents-findByAuthor($model as map(*)) as map(*)* {
)
else ()
return (
map { 'totalRecordCount': count($documents) },
api:document(api:subsequence($documents, $model), $model)
map {
'totalRecordCount': count($documents),
'results': api:document(api:subsequence($documents, $model), $model)
}
)
};

Expand All @@ -104,36 +112,39 @@ declare function api:code-findByElement($model as map(*)) {
(: The 'secret' $total switch is used for our list of examples on the spec pages and is of type element()*;
regular output from the API is the following subsequence of type map(*)* :)
else (
map { 'totalRecordCount': count($eval) },
api:codeSample(api:subsequence($eval, $model), $model)
map {
'totalRecordCount': count($eval),
'results': api:codeSample(api:subsequence($eval, $model), $model)
}
)
};

declare function api:application-status($model as map(*)*) as map(*)* {
declare function api:application-status($model as map(*)*) as map(*) {
let $healthy := query:facsimile(crud:doc('A040043'))[tei:graphic/@url]
and core:getOrCreateColl('letters', 'A002068', true())//tei:seg[@type='wordOfTheDay']
return
(
map:merge((
map:entry('totalRecordCount', 1),
if(not($healthy)) then map:entry('code', 500) else ()
)),
map {
"status": if($healthy) then "healthy" else "unhealthy",
"svnRevision": if (config:getCurrentSvnRev()) then config:getCurrentSvnRev() else 0,
"deployment": xs:dateTime($config:repo-descriptor/repo:deployed),
"version": config:expath-descriptor()/data(@version)
}
)
if(not($healthy)) then map:entry('code', 500) else (),
map:entry('results',
map {
"status": if($healthy) then "healthy" else "unhealthy",
"svnRevision": if (config:getCurrentSvnRev()) then config:getCurrentSvnRev() else 0,
"deployment": xs:dateTime($config:repo-descriptor/repo:deployed),
"version": config:expath-descriptor()/data(@version)
}
)
))
};

declare function api:application-newID($model as map(*)) as map(*)* {
declare function api:application-newID($model as map(*)) as map(*) {
if($config:isDevelopment)
then (
map { 'totalRecordCount': 1 },
map {
'docID': dev:createNewID($model?docType),
'docType': $model?docType
map {
'results':
map {
'docID': dev:createNewID($model?docType),
'docType': $model?docType
}
}
)
else
Expand All @@ -151,7 +162,7 @@ declare function api:application-newID($model as map(*)) as map(*)* {
:
: Expected parameters in the $model object are `facet`, `scope`, `docType`, and optionally `term`.
:)
declare function api:facets($model as map(*)) as map(*)* {
declare function api:facets($model as map(*)) as map(*) {
let $lang := config:guess-language($model('lang'))
let $model := map:merge(($model, map {'lang': $lang} ))
let $fileName := util:hash($model?facet || $model?scope || $model?docType || $lang, 'md5')
Expand Down Expand Up @@ -193,8 +204,10 @@ declare function api:facets($model as map(*)) as map(*)* {
))
else $allFacets
return (
map { 'totalRecordCount': count($facets) },
api:subsequence($facets, $model)
map {
'totalRecordCount': count($facets),
'results': array { api:subsequence($facets, $model) }
}
)
};

Expand Down Expand Up @@ -286,40 +299,44 @@ declare %private function api:subsequence($seq as item()*, $model as map(*)) {
(:~
: Helper function for creating a Document object
~:)
declare %private function api:document($documents as document-node()*, $model as map(*)) as map(*)* {
declare %private function api:document($documents as document-node()*, $model as map(*)) as array(*) {
let $host := $model('swagger:config')?host
let $basePath := $model('swagger:config')?basePath
let $scheme := $model('swagger:config')?schemes[1]
return
for $doc in $documents
let $id := $doc/*/data(@xml:id)
let $docType := config:get-doctype-by-id($id)
let $supportsHTML := $docType = ('letters', 'persons', 'diaries', 'writings', 'news', 'documents', 'thematicCommentaries')
return
map {
'uri' : $scheme || '://' || $host || substring-before($basePath, 'api') || $id,
'docID' : $id,
'docType' : $docType,
'title' : wdt:lookup($docType, $doc)('title')('txt')
}
array {
for $doc in $documents
let $id := $doc/*/data(@xml:id)
let $docType := config:get-doctype-by-id($id)
let $supportsHTML := $docType = ('letters', 'persons', 'diaries', 'writings', 'news', 'documents', 'thematicCommentaries')
return
map {
'uri' : $scheme || '://' || $host || substring-before($basePath, 'api') || $id,
'docID' : $id,
'docType' : $docType,
'title' : wdt:lookup($docType, $doc)('title')('txt')
}
}
};

(:~
: Helper function for creating a CodeSample object
~:)
declare function api:codeSample($nodes as node()*, $model as map(*)) as map(*)* {
declare function api:codeSample($nodes as node()*, $model as map(*)) as array(*) {
let $host := $model('swagger:config')?host
let $basePath := $model('swagger:config')?basePath
let $scheme := $model('swagger:config')?schemes?1
return
for $node in $nodes
let $docID := $node/root()/*/data(@xml:id)
return
map {
'uri' : $scheme || '://' || $host || substring-before($basePath, 'api') || $docID,
'docID' : $docID,
'codeSample' : serialize(functx:change-element-ns-deep(wega-util:process-xml-for-display($node), '', ''))
}
array {
for $node in $nodes
let $docID := $node/root()/*/data(@xml:id)
return
map {
'uri' : $scheme || '://' || $host || substring-before($basePath, 'api') || $docID,
'docID' : $docID,
'codeSample' : serialize(functx:change-element-ns-deep(wega-util:process-xml-for-display($node), '', ''))
}
}
};

(:~
Expand Down