aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix/Client/Requester.pm6
blob: 36a9f69c89b020b84aeb8f737f474dff0a2d110b (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use HTTP::UserAgent;
use HTTP::Request::Common;
use URI::Encode;
use JSON::Fast;
use Matrix::Client::Exception;

unit role Matrix::Client::Requester;

has $.home-server is required;
has $.access-token = "";

has $!ua = HTTP::UserAgent.new;
has $!client-endpoint = "/_matrix/client/r0";
has $!url-prefix = "";
has $!sync-since = "";

method !handle-error($response) is hidden-from-backtrace {
    unless $response.is-success {
        my $data = from-json($response.content);
        X::Matrix::Response.new(:code($data<errcode>), :error($data<error>)).throw;
    }
    $response
}

method !access-token-arg {
    $!access-token ?? "access_token=$!access-token" !! ''
}

method get(Str $path, :$media = False, *%data) {
    my $query = "?";
    for %data.kv -> $k,$v {
        $query ~= "&$k=$v" if $v.so;
    }
    my $encoded-path = $path.subst('#', '%23');
    my $uri = $.base-url(:$media) ~ $encoded-path ~ uri_encode($query);

    my $req = HTTP::Request.new(GET => $uri);

    if $!access-token.so {
        $req.header.field(Authorization => "Bearer {$!access-token}");
    }

    return self!handle-error(
        $!ua.request($req)
    );
}

method base-url(Bool :$media? = False --> Str) {
    if !$media {
        "$.home-server$!client-endpoint$!url-prefix"
    } else {
        "$.home-server/_matrix/media/r0"
    }
}

multi method post(Str $path, Str $json, :$media = False) {
    my $encoded-path = $path.subst('#', '%23');
    my $url = $.base-url(:$media) ~ $encoded-path;
    my $req = HTTP::Request.new(POST => $url,
                                Content-Type => 'application/json');
    if $!access-token.so {
        $req.header.field(Authorization => "Bearer {$!access-token}");
    }
    $req.add-content($json);
    return self!handle-error($!ua.request($req));
}

multi method post(Str $path, :$media = False, *%params) {
    my $json = to-json(%params);
    $.post($path, $json, :$media)
}

method post-bin(Str $path, Buf $buf, :$content-type) {
    my $encoded-path = $path.subst('#', '%23');
    my $req = POST(
        $.base-url() ~ $encoded-path,
        content => $buf,
        Content-Type => $content-type
    );

    if $!access-token.so {
        $req.header.field(Authorization => "Bearer {$!access-token}");
    }

    return self!handle-error($!ua.request($req));
}

multi method put(Str $path, Str $json) {
    my $encoded-path = $path.subst('#', '%23');
    my $req = HTTP::Request.new(PUT => $.base-url() ~ $encoded-path,
                                Content-Type => 'application/json');
    if $!access-token.so {
        $req.header.field(Authorization => "Bearer {$!access-token}");
    }

    $req.add-content($json);
    return self!handle-error($!ua.request($req))
}

multi method put(Str $path, *%params) {
    self.put($path, to-json(%params))
}

method delete(Str $path) {
    my $encoded-path = $path.subst('#', '%23');
    my $req = HTTP::Request.new(
        DELETE => $.base-url ~ $encoded-path,
        Content-Type => 'application/json');
    if $!access-token.so {
        $req.header.field(
            Authorization => "Bearer $!access-token"
        );
    }
    return self!handle-error($!ua.request($req))
}