Implement /tags and /tag/id endpoints

This commit is contained in:
Lucas 2023-02-18 14:47:32 +00:00
parent 57b00f6c28
commit ead5728a99
5 changed files with 84 additions and 5 deletions

View file

@ -2,10 +2,25 @@ package PoorBooru;
use Dancer2;
use HTTP::Tiny;
our $VERSION = v0.1;
our $VERSION = v0.0.1;
my $POORBOORU_API = setting("poorbooru_api");
sub http_tiny ()
{
return HTTP::Tiny->new(
timeout => 15,
verify_SSL => true,
agent => setting("appname") . " backend " .
version::->parse($VERSION)->normal,
default_headers => {
"Accept" => "application/json",
"Content-Type" => "application/json",
},
);
}
hook before_template_render => sub {
my $tokens = shift;
@ -18,20 +33,50 @@ hook before_template_render => sub {
get "/" => sub {
template "index" => {
"title" => "main",
title => "main",
};
};
get "/tags" => sub {
my $res = http_tiny()->get("$POORBOORU_API/tags");
send_error("API error", 500) if !$res->{success};
my $data = decode_json($res->{content});
my @tags = map +( {
name => $_->{name},
count => $_->{count},
uri => uri_for("/tag/" . $_->{name}),
} ), @$data;
template "tags" => {
title => "Tags",
tags => \@tags,
};
};
get "/tag/:tag_id" => sub {
get "/tag/:tag_id_or_name" => sub {
my $tag_id_or_name = route_parameters->get("tag_id_or_name");
my $res = http_tiny()->get("$POORBOORU_API/tag/$tag_id_or_name");
send_error("API error", 500) if !$res->{success};
my $data = decode_json($res->{content});
my @media = map +( {
# XXX point to a cache
image_src => $_->{download_uri},
view_uri => uri_for("/view/" . $_->{id}),
} ), @{$data->{media}};
template "gallery" => {
title => $data->{name},
media => \@media,
};
};
get "/random" => sub {
};
get "/image/:image_id" => sub {
get "/view/:media_id" => sub {
};
true;