1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-05 01:26:18 +00:00
TiddlyWiki5/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request Example Random Dog.tid
Jeremy Ruston 9b2af13596 tm-http-request: Add support for binary responses
With a demo courtesy of https://random.dog/

@rmunn you recently worked on the base64 utilities. I tried to use $tw.utils.base64Encode instead of window.btoa, but found that it didn't work. It's concerning because we expose that utility method as a filter operation, and it would be frustrating if we were not base64encoding things properly.
2023-07-17 12:15:20 +01:00

89 lines
2.2 KiB
Plaintext

title: WidgetMessage: tm-http-request Example - Random Dog
tags: $:/tags/Global
\procedure download-dog(url)
\procedure completion-download-dog()
\import [subfilter{$:/core/config/GlobalImportFilter}]
<$action-log msg="In completion-download-dog"/>
<$action-log/>
<!-- Success -->
<$list filter="[<status>compare:number:gteq[200]compare:number:lteq[299]]" variable="ignore">
<!-- Create the dog tiddler -->
<$action-createtiddler
$basetitle=`$:/RandomDog/$(title)$`
text=<<data>>
tags="$:/tags/RandomDog"
type={{{ [<headers>jsonget[content-type]] }}}
credits="https://random.dog/"
>
<$action-log msg="Created tiddler" title=<<createTiddler-title>>/>
</$createtiddler>
</$list>
\end completion-download-dog
<$action-sendmessage
$message="tm-http-request"
url=<<url>>
method="GET"
binary="yes"
oncompletion=<<completion-download-dog>>
var-title=<<url>>
/>
\end download-dog
\procedure get-random-dog()
\procedure completion-get-json()
\import [subfilter{$:/core/config/GlobalImportFilter}]
<$action-log msg="In completion-get-json"/>
<$action-log/>
<!-- Success -->
<$list filter="[<status>compare:number:gteq[200]compare:number:lteq[299]]" variable="ignore">
<!-- Download the dog -->
<$macrocall $name="download-dog" url={{{ [<data>jsonget[url]] }}}/>
</$list>
\end completion-get-json
<$action-sendmessage
$message="tm-http-request"
url="https://random.dog/woof.json"
method="GET"
oncompletion=<<completion-get-json>>
/>
\end get-random-dog
!! Random Dogs
This demo uses the API of the website https://random.dog/ to import a random dog image or video.
<$button actions=<<get-random-dog>>>
Import a random dog image or video
</$button>
<$list filter="[tag[$:/tags/RandomDog]limit[1]]" variable="ignore">
!! Imported Tiddlers
<$button>
<$action-deletetiddler $filter="[tag[$:/tags/RandomDog]]"/>
Delete all imported random dogs
</$button>
Export all imported random dogs: <$macrocall $name="exportButton" exportFilter="[tag[$:/tags/RandomDog]]" lingoBase="$:/language/Buttons/ExportTiddlers/"/>
</$list>
<ol>
<$list filter="[tag[$:/tags/RandomDog]!sort[modified]]">
<li>
<$link>
<$text text=<<currentTiddler>>/>
</$link>
<div style="width:300px;height:300px;">
<$transclude $tiddler=<<currentTiddler>>/>
</div>
</li>
</$list>
</ol>