From 4f9159b228cb2533eccab11f82690171a1484ee5 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 29 Jun 2019 19:03:28 -0300 Subject: Use Authorization header instead query param --- lib/Matrix/Client/Requester.pm6 | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'lib/Matrix/Client/Requester.pm6') diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 index 1a60014..edaf2da 100644 --- a/lib/Matrix/Client/Requester.pm6 +++ b/lib/Matrix/Client/Requester.pm6 @@ -27,13 +27,22 @@ method !access-token-arg { } method get(Str $path, :$media = False, *%data) { - my $query = "?{self!access-token-arg}"; + 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); - return self!handle-error($!ua.get($uri)); + + 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) { @@ -46,9 +55,12 @@ method base-url(Bool :$media? = False --> Str) { multi method post(Str $path, Str $json, :$media = False) { my $encoded-path = $path.subst('#', '%23'); - my $url = $.base-url(:$media) ~ $encoded-path ~ "?{self!access-token-arg}"; + 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)); } @@ -60,19 +72,27 @@ multi method post(Str $path, :$media = False, *%params) { method post-bin(Str $path, Buf $buf, :$content-type) { my $encoded-path = $path.subst('#', '%23'); - my $req = POST($.base-url(:media) - ~ $encoded-path - ~ "?{self!access-token-arg}", + my $req = POST( + $.base-url(:media) ~ $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 ~ "?{self!access-token-arg}", + 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)) } @@ -84,7 +104,12 @@ multi method put(Str $path, *%params) { method delete(Str $path) { my $encoded-path = $path.subst('#', '%23'); my $req = HTTP::Request.new( - DELETE => $.base-url ~ $encoded-path ~ "?{self!access-token-arg}", + 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)) } -- cgit v1.2.3-70-g09d2 From ec4393cb81952fffa2273e313818a57c9611c5a2 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Thu, 11 Jul 2019 19:53:05 -0300 Subject: Use JSON::Fast --- META6.json | 4 ++-- lib/Matrix/Client.pm6 | 2 +- lib/Matrix/Client/Requester.pm6 | 2 +- lib/Matrix/Client/Room.pm6 | 2 +- lib/Matrix/Response.pm6 | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) (limited to 'lib/Matrix/Client/Requester.pm6') diff --git a/META6.json b/META6.json index 7d9d358..d31ee73 100644 --- a/META6.json +++ b/META6.json @@ -4,7 +4,7 @@ ], "build-depends" : [ ], "depends" : [ - "JSON::Tiny", + "JSON::Fast", "HTTP::UserAgent", "URI::Encode", "IO::Socket::SSL" @@ -34,5 +34,5 @@ "Test", "Test::META" ], - "version" : "0.5.0" + "version" : "0.5.1" } diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index d7206b0..9022c65 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -1,6 +1,6 @@ use HTTP::Request::Common; use URI::Encode; -use JSON::Tiny; +use JSON::Fast; use Matrix::Response; use Matrix::Client::Common; use Matrix::Client::Room; diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 index edaf2da..ff543c9 100644 --- a/lib/Matrix/Client/Requester.pm6 +++ b/lib/Matrix/Client/Requester.pm6 @@ -1,7 +1,7 @@ use HTTP::UserAgent; use HTTP::Request::Common; use URI::Encode; -use JSON::Tiny; +use JSON::Fast; use Matrix::Client::Exception; unit role Matrix::Client::Requester; diff --git a/lib/Matrix/Client/Room.pm6 b/lib/Matrix/Client/Room.pm6 index 4ec774f..ca97d2f 100644 --- a/lib/Matrix/Client/Room.pm6 +++ b/lib/Matrix/Client/Room.pm6 @@ -1,4 +1,4 @@ -use JSON::Tiny; +use JSON::Fast; use Matrix::Client::Common; use Matrix::Client::Requester; use Matrix::Response; diff --git a/lib/Matrix/Response.pm6 b/lib/Matrix/Response.pm6 index 14e2b21..268be84 100644 --- a/lib/Matrix/Response.pm6 +++ b/lib/Matrix/Response.pm6 @@ -1,4 +1,4 @@ -use JSON::Tiny; +use JSON::Fast; unit module Matrix::Response; @@ -69,7 +69,7 @@ class Matrix::Response::Sync { my Matrix::Response::Event @presence; my Matrix::Response::RoomInfo @joined-rooms; my Matrix::Response::InviteInfo @invited-rooms; - + for $json.List -> $ev { @presence.push(Matrix::Response::Event.new(|$ev)); } -- cgit v1.2.3-70-g09d2