mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-11 18:00:26 +00:00
Add a Zotero demo for the new http mechanism
An initial experiment for handling paginated APIs. This isn't perfect; it isn't possible to interrupt things, for example.
This commit is contained in:
parent
27f9df3af5
commit
4b9a6b5757
39
core/modules/filters/url-ops.js
Normal file
39
core/modules/filters/url-ops.js
Normal file
@ -0,0 +1,39 @@
|
||||
/*\
|
||||
title: $:/core/modules/filters/url-ops.js
|
||||
type: application/javascript
|
||||
module-type: filteroperator
|
||||
|
||||
Filter operators for URL operations
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports["setquerystring"] = function(source,operator,options) {
|
||||
var URL = $tw.browser ? window.URL : require("url").URL,
|
||||
URLSearchParams = $tw.browser ? window.URLSearchParams : require("url").URLSearchParams,
|
||||
name = operator.operands.length >= 1 ? operator.operands[0] : null,
|
||||
value = operator.operands.length >= 2 ? operator.operands[1] : "",
|
||||
results = [];
|
||||
source(function(tiddler,title) {
|
||||
var url;
|
||||
try {
|
||||
url = new URL(title);
|
||||
} catch(e) {
|
||||
}
|
||||
if(url) {
|
||||
var params = new URLSearchParams(url.search);
|
||||
if(name) {
|
||||
params.set(name,value);
|
||||
}
|
||||
url.search = params.toString();
|
||||
results.push(url.toString());
|
||||
}
|
||||
});
|
||||
return results;
|
||||
};
|
||||
|
||||
})();
|
@ -66,9 +66,9 @@ HttpClient.prototype.handleHttpRequest = function(event) {
|
||||
callback: function(err,data,xhr) {
|
||||
var headers = {};
|
||||
$tw.utils.each(xhr.getAllResponseHeaders().split("\r\n"),function(line) {
|
||||
var parts = line.split(":");
|
||||
if(parts.length === 2) {
|
||||
headers[parts[0].toLowerCase()] = parts[1].trim();
|
||||
var pos = line.indexOf(":");
|
||||
if(pos !== -1) {
|
||||
headers[line.substr(0,pos)] = line.substr(pos + 1).trim();
|
||||
}
|
||||
});
|
||||
setBinding(bindStatus,xhr.status === 200 ? "complete" : "error");
|
||||
|
27
editions/test/tiddlers/tests/data/filters/querystrings.tid
Normal file
27
editions/test/tiddlers/tests/data/filters/querystrings.tid
Normal file
@ -0,0 +1,27 @@
|
||||
title: Filters/QueryStrings
|
||||
description: Query string filter operators
|
||||
type: text/vnd.tiddlywiki-multiple
|
||||
tags: [[$:/tags/wiki-test-spec]]
|
||||
|
||||
title: Output
|
||||
|
||||
\define url() https://tiddlywiki.com/
|
||||
|
||||
<$let lf={{{ [charcode[10],[13]]}}}>
|
||||
|
||||
<$text text={{{
|
||||
[<url>setquerystring[]]
|
||||
[<url>setquerystring[animal],[elephant]]
|
||||
[<url>setquerystring[animal],[elephant]setquerystring[animal],[ostrich]]
|
||||
[<url>setquerystring[animal],[elephant]setquerystring[animal],[ostrich]setquerystring[animal]]
|
||||
+[join<lf>]
|
||||
}}}/>
|
||||
|
||||
</$let>
|
||||
+
|
||||
title: ExpectedResult
|
||||
|
||||
https://tiddlywiki.com/
|
||||
https://tiddlywiki.com/?animal=elephant
|
||||
https://tiddlywiki.com/?animal=ostrich
|
||||
https://tiddlywiki.com/?animal=
|
@ -0,0 +1,78 @@
|
||||
title: WidgetMessage: tm-http-request Example - Zotero
|
||||
tags: $:/tags/Macro
|
||||
|
||||
\define zotero-save-item(item)
|
||||
<$action-createtiddler
|
||||
$basetitle={{{ =[[_zotero_import ]] =[<item>jsonget[key]] =[[ ]] =[<item>jsonget[title]] +[join[]] }}}
|
||||
text={{{ [<item>jsonget[title]] }}}
|
||||
tags="$:/tags/ZoteroImport"
|
||||
>
|
||||
<$action-setmultiplefields $tiddler=<<createTiddler-title>> $fields="[<item>jsonindexes[]addprefix[zotero-]]" $values="[<item>jsonindexes[]] :map[<item>jsonget<currentTiddler>else[.XXXXX.]]"/>
|
||||
</$action-createtiddler>
|
||||
\end zotero-save-item
|
||||
|
||||
\define zotero-save-items(data)
|
||||
<$list filter="[<data>jsonindexes[]] :map[<data>jsonextract<currentTiddler>,[data]]" variable="item">
|
||||
<$macrocall $name="zotero-save-item" item=<<item>>/>
|
||||
</$list>
|
||||
\end zotero-save-items
|
||||
|
||||
\define zotero-get-items(start:"0",limit:"25")
|
||||
|
||||
\define completion()
|
||||
\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]
|
||||
<$action-log msg="In completion"/>
|
||||
<$action-log/>
|
||||
<!-- Success -->
|
||||
<$list filter="[<status>compare:number:gteq[200]compare:number:lteq[299]]" variable="ignore">
|
||||
<!-- Import these items -->
|
||||
<$macrocall $name="zotero-save-items" data=<<data>>/>
|
||||
<!-- Check if there are any more items to download -->
|
||||
<$list filter="[<headers>jsonget[total-results]subtract<start>subtract<limit>compare:number:gt[0]]" variable="ignore">
|
||||
<$macrocall $name="zotero-get-items" start={{{ [<start>add<limit>] }}} limit=<<limit>>/>
|
||||
</$list>
|
||||
</$list>
|
||||
\end completion
|
||||
|
||||
\define progress()
|
||||
\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]
|
||||
<$action-log message="In progress-actions"/>
|
||||
\end progress
|
||||
|
||||
\define request-url()
|
||||
https://api.zotero.org/groups/27508/items/?format=json&sort=title
|
||||
\end request-url
|
||||
|
||||
<$action-sendmessage
|
||||
$message="tm-http-request"
|
||||
url={{{ [<request-url>setquerystring[start],<__start__>setquerystring[limit],<__limit__>] }}}
|
||||
method="GET"
|
||||
header-accept="application/json"
|
||||
bind-status="$:/temp/zotero/status"
|
||||
bind-progress="$:/temp/zotero/progress"
|
||||
oncompletion=<<completion>>
|
||||
onprogress=<<progress>>
|
||||
var-start=<<__start__>>
|
||||
var-limit=<<__limit__>>
|
||||
/>
|
||||
\end
|
||||
|
||||
\define zotero-actions()
|
||||
<$macrocall $name="zotero-get-items" start="0" limit="5"/>
|
||||
\end
|
||||
|
||||
<$button actions=<<zotero-actions>>>
|
||||
Call Zotero
|
||||
</$button>
|
||||
|
||||
Tiddlers:
|
||||
|
||||
<ol>
|
||||
<$list filter="[tag[$:/tags/ZoteroImport]]">
|
||||
<li>
|
||||
<$link>
|
||||
<$view field="title"/>
|
||||
</$link>
|
||||
</li>
|
||||
</$list>
|
||||
</ol>
|
@ -41,3 +41,7 @@ The following variables are passed to the progress handler:
|
||||
|lengthComputable |Whether the progress loaded and total figures are valid - "yes" or "no" |
|
||||
|loaded |Number of bytes loaded so far |
|
||||
|total |Total number bytes to be loaded |
|
||||
|
||||
!! Examples
|
||||
|
||||
* [[Zotero's|https://www.zotero.org/]] API for retrieving reference items: [[WidgetMessage: tm-http-request Example - Zotero]]
|
||||
|
Loading…
Reference in New Issue
Block a user