summaryrefslogtreecommitdiff
path: root/lib/Ultramarine/Serialiser/XML.pm6
blob: 2d04427be0e9f4b65e494d3bbc309c9b2c7c078b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use v6.d.PREVIEW;
use Cro::HTTP::BodySerializer;
use XML::Writer;
 
class Ultramarine::Serialiser::XML
 does Cro::HTTP::BodySerializer {
 
    method is-applicable(Cro::HTTP::Message $message, $body --> Bool{
        with $message.content-type {
            (.type eq 'application'|'text' && .subtype eq 'xml' || .suffix eq 'xml'&&
                ($body ~~ Map || $body ~~ List)
        }
        else {
            False
        }
    }
 
    method serialize(Cro::HTTP::Message $message, $body --> Supply{
        my $xml = XML::Writer.serialize(
            subsonic-response => [
                :xmlns<http://subsonic.org/restapi>,
                :version<1.13.0>,
                |$body,
            ],
        ).encode('utf-8');
        self!set-content-length($message, $xml.bytes);
        supply { emit $xml }
    }
   
}