From 61c78c94b9fa6ae83d065e9a4eb1d5df401265ce Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Sat, 9 Apr 2016 22:32:40 -0400 Subject: [PATCH] Work --- META6.json | 2 +- README.md | 38 ++++++++++++++++++++++++++++++++++++++ examples/glot.p6 | 10 ++-------- lib/GlotIO.pm6 | 47 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 81 insertions(+), 16 deletions(-) diff --git a/META6.json b/META6.json index 221baaf..288eff7 100644 --- a/META6.json +++ b/META6.json @@ -2,7 +2,7 @@ "perl" : "6.c", "name" : "GlotIO", "version" : "1.001001", - "description" : "use glot.io API: pastebin allowing execution of code", + "description" : "Use glot.io API: pastebin allowing execution of code", "depends" : [ "HTTP::Tinyish", "JSON::Fast", diff --git a/README.md b/README.md index 70cc086..55f36d5 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,44 @@ A shortcut for calling `.run` (takes same arguments) and returning just the `stderr` key. Will `fail` with the entire `Hash` returned from `.run` if the program errors out. +## `.list` + +```perl6 +say $glot.list[0..3]; + +say $glot.list(:3page, :50per-page, :mine)[0]; +``` + +Fetches a list of metadata for snippets. Takes optional +named arguments: + +* `page` positive integer starting at and defaulting to 1. Specifies the page to display +* `per-page` positive integer stating the number of items to return perl +page. Defaults to `100`. Maximum value is `100`. +* `mine` boolean specifying whether public or your own snippets should be +listed. Defaults to `False`. Requires `key` argument to `.new` to be provided +if set to `True. + +Returns a list of `Hash`s as the one shown here: + +```perl6 + [ + { + created => "2016-04-09T17:52:19Z", + files_hash => "2afa1f37cc0bc7d033e4b3a049659792f5caac6d", + id => "edltstt3n0", + language => "cpp", + modified => "2016-04-09T17:52:19Z", + owner => "anonymous", + public => Bool::True, + title => "Untitled", + url => "https://snippets.glot.io/snippets/edltstt3n0", + }, + ... + ] +``` + + ---- # REPOSITORY diff --git a/examples/glot.p6 b/examples/glot.p6 index 8bb1470..aa912f5 100644 --- a/examples/glot.p6 +++ b/examples/glot.p6 @@ -3,11 +3,5 @@ use lib 'lib'; use GlotIO; my GlotIO $glot .= new: key => 't/key'.IO.lines[0]; -say $glot.stderr: 'perl6', 'note "Hello, World!"'; - -=finish - -say $glot.run: 'perl6', [ - 'main.p6' => 'use lib "."; use Foo; doit;', - 'Foo.pm6' => 'unit module Foo; sub doit is export { say "42" }', -]; +use Data::Dump; +say Dump $glot.list(:4page, :!mine); \ No newline at end of file diff --git a/lib/GlotIO.pm6 b/lib/GlotIO.pm6 index 87dcf39..874e369 100644 --- a/lib/GlotIO.pm6 +++ b/lib/GlotIO.pm6 @@ -5,15 +5,19 @@ use JSON::Fast; use URI::Escape; has Str $.key; -has $!api-url = 'https://run.glot.io'; +has $!run-api-url = 'https://run.glot.io'; +has $!snip-api-url = 'https://snippets.glot.io'; has $!ua = HTTP::Tinyish.new(agent => "Perl 6 NASA.pm6"); -method !request ($method, $url, $content?, *%params) { +method !request ($method, $url, $content?, *%params, Bool :$add-token) { #%params = %params.kv.map: { uri-escape $_ }; my %res; if ( $method eq 'GET' ) { - %res = $!ua.get: $url; + %res = $!ua.get: $url, + headers => %( + ('Authorization' => "Token $!key" if $add-token) + ); } elsif ( $method eq 'POST' ) { %res = $!ua.post: $url, @@ -28,15 +32,28 @@ method !request ($method, $url, $content?, *%params) { } %res or fail "ERROR %res: %res"; - return from-json %res; + + return from-json %res + unless %res; + + say %res; + say '---'; + my %links; + for %res ~~ m:g/ '<'(.+?)'>;'\s+'rel="'(.+?)\" / -> $m { + %links{ ~$m[1] } = ~$m[0]; + }; + return %( + |%links, + content => from-json %res + ); } method languages { - self!request('GET', $!api-url ~ '/languages').map: *; + self!request('GET', $!run-api-url ~ '/languages').map: *; } method versions (Str $lang) { - my $uri = $!api-url ~ '/languages/' ~ uri-escape($lang); + my $uri = $!run-api-url ~ '/languages/' ~ uri-escape($lang); self!request('GET', $uri).map: *; } @@ -45,7 +62,7 @@ multi method run (Str $lang, @files, :$ver = 'latest') { %content = @files.map: { %(name => .key, content => .value ) }; - my $uri = $!api-url ~ '/languages/' ~ uri-escape($lang) + my $uri = $!run-api-url ~ '/languages/' ~ uri-escape($lang) ~ '/' ~ uri-escape($ver); self!request: 'POST', $uri, to-json %content; } @@ -59,8 +76,24 @@ method stdout (|c) { fail "Error: $res" if $res; $res; } + method stderr (|c) { my $res = self.run(|c); fail "Error: $res" if $res; $res; } + +method list ( + Int $page = 1, + Int :$per-page = 100, + Str :$owner, + Str :$language, + Bool :$mine = False, +) { + self!request: 'GET', $!snip-api-url ~ '/snippets' + ~ "?page=$page&per_page=$per-page" + ~ ( $owner.defined ?? "&owner=" ~ uri-escape($owner) !! '' ) + ~ ( $language.defined ?? "&language=" ~ uri-escape($language) !! '' ), + add-token => $mine; +} +