Permalink
Browse files

Add .success method

  • Loading branch information...
CurtTilmes committed Jun 9, 2017
1 parent b5dda29 commit 95f975fd4ac69c4a561de86dd03e03958e9983c9
Showing with 31 additions and 5 deletions.
  1. +8 −3 README.md
  2. +4 −0 doc/LibCurl/Easy.pod6
  3. +4 −0 doc/LibCurl::Easy.md
  4. +7 −2 lib/LibCurl/Easy.pm6
  5. +8 −0 xt/02-http.t
View
@@ -42,7 +42,7 @@ A [Perl 6](https://perl6.org/) interface to
# GET
print LibCurl::Easy.new(URL => 'http://example.com').perform.content;
# GET (download a file)
LibCurl::Easy.new(URL => 'http://example.com/somefile',
download => 'somefile').perform;
@@ -116,6 +116,7 @@ You can even import very simple subroutines ala [WWW](https://github.com/zoffixz
my $curl = LibCurl::Easy.new(:verbose, :followlocation);
$curl.setopt(URL => 'http://example.com', download => './myfile.html');
$curl.perform;
say $curl.success;
say $curl.Content-Type;
say $curl.Content-Length;
say $curl.Date;
@@ -306,10 +307,14 @@ thrown X::LibCurl exception. You can catch these with CATCH. You can
see the string error, or cast to Int to see the [libcurl error
code](https://curl.haxx.se/libcurl/c/libcurl-errors.html).
For HTTP transfers, you can access the response code with
`getinfo('response-code')` or just `.response-code`. You can also
check that the response code is in the 2xx range with `.success`.
You might find the
[failonerror](https://curl.haxx.se/libcurl/c/CURLOPT_FAILONERROR.html)
option useful to force an error if the HTTP code is equal to or larger
than 400.
than 400. That will cause an exception in those cases.
On an error, you may find [extra human readable error
messages](https://curl.haxx.se/libcurl/c/CURLOPT_ERRORBUFFER.html)
@@ -343,7 +348,7 @@ there are also convenience methods for each info field.
say $curl.getinfo(<effective-url response-code>); # Hash with those keys
say $curl.getinfo; # Hash of all info fields
say $curl.effective-url;
say $curl.response-code;
View
@@ -91,6 +91,10 @@ A Hash of all received headers from the last transaction.
Get a single from the received headers.
=item method B<success>() returns Bool
Is the HTTP response-code 2xx?
=item multi method B<getinfo>(Str $info)
Returns a single info object. See ... for a description of all the
View
@@ -80,6 +80,10 @@ A Hash of all received headers from the last transaction.
Get a single from the received headers.
* method **success**() returns Bool
Is the HTTP response-code 2xx?
* multi method **getinfo**(Str $info)
Returns a single info object. See ... for a description of all the info fields.
View
@@ -507,7 +507,7 @@ class LibCurl::Easy
$value eq ';' ?? "$field;" !! "$field: $value"
);
}
return self;
}
@@ -548,7 +548,7 @@ class LibCurl::Easy
{
$!handle.setopt(CURLOPT_HTTPHEADER, $!header-slist);
unless $!download-fh
unless $!download-fh
{
$!buf = Buf.new;
$!handle.setopt(CURLOPT_WRITEDATA, $!handle);
@@ -585,6 +585,11 @@ class LibCurl::Easy
return self;
}
method success() returns Bool
{
200 <= self.getinfo('response-code') < 300
}
multi method getinfo(Str $info)
{
my ($code, $type) = %infofields{$info};
View
@@ -5,6 +5,8 @@ use Test::When <online>;
use LibCurl::HTTP;
plan 18;
my $version = LibCurl::HTTP.version;
like $version, /^libcurl/, 'LibCurl Version';
@@ -20,6 +22,8 @@ isa-ok $curl, LibCurl::HTTP, 'Created Object';
#
my $x = $curl.GET('http://example.com').perform;
ok $curl.success, 'Response success';
is $curl.response-code, 200, 'Response code';
is $curl.statusline, 'HTTP/1.1 200 OK', 'Status line';
@@ -37,6 +41,8 @@ $curl = LibCurl::HTTP.new();
$curl.HEAD('http://example.com').perform;
ok $curl.success, 'Response success';
is $curl.response-code, 200, 'Response code';
is $curl.statusline, 'HTTP/1.1 200 OK', 'Status line';
@@ -55,6 +61,8 @@ $curl = LibCurl::HTTP.new();
$curl.POST('http://httpbin.org/post?foo=42&bar=x').perform;
ok $curl.success, 'Response success';
is $curl.response-code, '200', 'Response-code';
is $curl.statusline, 'HTTP/1.1 200 OK', 'Status line';

0 comments on commit 95f975f

Please sign in to comment.