2023-07-17 11:15:20 +00:00
title: WidgetMessage: tm-http-request Example - Random Dog
2024-02-29 12:39:41 +00:00
tags: $:/tags/Macro $:/tags/Global
2023-07-17 11:15:20 +00:00
\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.
2023-07-31 13:36:29 +00:00
//Note that the images and videos can be quite large, so this demo is not recommended to be used over mobile data connections.//
2023-07-17 11:15:20 +00:00
<$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>
2023-08-01 14:13:22 +00:00
<div style="width:300px;">
2023-07-17 11:15:20 +00:00
<$transclude $tiddler=<<currentTiddler>>/>
</div>
</li>
</$list>
</ol>