aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix/Client/Requester.rakumod
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@gmail.com>2021-01-11 22:20:09 -0300
committerMatias Linares <matiaslina@gmail.com>2021-01-11 22:20:09 -0300
commit7be9fa68bef82a7576344a6e8cc1e51154c6b3bf (patch)
treed60908a75c60b55d4ea90f6079c2459f9cf56be9 /lib/Matrix/Client/Requester.rakumod
parent9da9134f0b28c0e4c69537b54fd96dca3a66aaed (diff)
downloadperl6-matrix-client-7be9fa68bef82a7576344a6e8cc1e51154c6b3bf.tar.gz
Response & Exception refactor
Diffstat (limited to 'lib/Matrix/Client/Requester.rakumod')
-rw-r--r--lib/Matrix/Client/Requester.rakumod115
1 files changed, 115 insertions, 0 deletions
diff --git a/lib/Matrix/Client/Requester.rakumod b/lib/Matrix/Client/Requester.rakumod
new file mode 100644
index 0000000..36a9f69
--- /dev/null
+++ b/lib/Matrix/Client/Requester.rakumod
@@ -0,0 +1,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))
+}