summaryrefslogtreecommitdiff
path: root/lib/Ultramarine/Serialiser/XML.pm6
blob: a4354f5b950128f1cafb9742f2160c254101aea0 (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
31
32
33
use v6.d.PREVIEW;
use Cro::HTTP::BodySerializers;
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 = (
            qq{<?xml version="1.0" encoding="UTF-8"?>\n} ~
            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 }
    }
   
}