Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request fayland#24 from lkundrak/master
Various improvements we've done to make use of Net::GitHub
  • Loading branch information
fayland committed Aug 27, 2011
2 parents 2978080 + edc4996 commit 91291fc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
12 changes: 11 additions & 1 deletion lib/Net/GitHub/V2.pm
Expand Up @@ -141,12 +141,22 @@ For those B<(authentication required)>, you must set login and token (in L<https
login => 'fayland', token => '54b5197d7f92f52abc5c7149b313cf51', # faked
);
If you want to work with private repo, you can set B<always_Authorization>:
If you want to work with private repo, you can set B<always_Authorization>.
To disable call rate limiting (e.g. if your account is whitelisted), set
B<api_throttle> to 0.
By default, error responses are propagated to the user as they are received
from the API. By switching B<throw_errors> on you can make the be turned into
exceptions instead, so that you don't have to check for error response after
every call.
my $github = Net::GitHub::V2->new(
owner => 'fayland', repo => 'perl-net-github',
login => 'fayland', token => '54b5197d7f92f52abc5c7149b313cf51', # faked
always_Authorization => 1,
api_throttle => 0,
throw_errors => 0,
);
=head1 METHODS
Expand Down
3 changes: 2 additions & 1 deletion lib/Net/GitHub/V2/HasRepo.pm
Expand Up @@ -13,7 +13,8 @@ has 'repo' => ( isa => 'Str', is => 'ro', required => 1 );
sub args_to_pass {
my $self = shift;
my $ret;
foreach my $col ('owner', 'repo', 'login', 'token', 'always_Authorization') {
foreach my $col ('owner', 'repo', 'login', 'token', 'always_Authorization', 'throw_errors',
'api_throttle', 'api_url', 'api_url_https') {
$ret->{$col} = $self->$col if defined $self->$col;
}
return $ret;
Expand Down
44 changes: 39 additions & 5 deletions lib/Net/GitHub/V2/NoRepo.pm
Expand Up @@ -21,9 +21,13 @@ has 'token' => (is => 'rw', isa => 'Str', predicate => 'has_token',);
# always send Authorization header, useful for private respo
has 'always_Authorization' => ( is => 'rw', isa => 'Bool', default => 0 );

# simplifies error handling
has 'throw_errors' => ( is => 'rw', isa => 'Bool', default => 0 );

# api
has 'api_url' => ( is => 'ro', default => 'http://github.com/api/v2/json/');
has 'api_url_https' => ( is => 'ro', default => 'https://github.com/api/v2/json/');
has 'api_throttle' => ( is => 'rw', isa => 'Bool', default => 1 );

has 'ua' => (
isa => 'WWW::Mechanize',
Expand Down Expand Up @@ -98,6 +102,16 @@ before get_json_to_obj_authed => sub {
};

sub get_json_to_obj_authed {
push @_, undef;
_get_json_to_obj_authed(@_);
}

sub get_json_to_obj_authed_GET {
push @_, 'GET';
_get_json_to_obj_authed(@_);
}

sub get_json_to_obj_authed_POST {
push @_, 'POST';
_get_json_to_obj_authed(@_);
}
Expand All @@ -116,7 +130,7 @@ sub _get_json_to_obj_authed {
my $self = shift;
my $pending_url = shift;

my $request_method = pop @_; # can be DELETE or PUT
my $request_method = pop @_; # defaults to GET or POST if undefined

croak 'login and token are required' unless ( $self->has_login and $self->has_token );

Expand All @@ -129,19 +143,39 @@ sub _get_json_to_obj_authed {
$key = pop @_;
}

$request_method ||= @_ ? 'POST' : 'GET';
my $req = $request_method eq 'DELETE' ? HTTP::Request::Common::DELETE( $url, [ @_ ] ) :
$request_method eq 'PUT' ? HTTP::Request::Common::PUT( $url, [ @_ ] ) :
HTTP::Request::Common::POST( $url, [ @_ ] );

$request_method eq 'POST' ? HTTP::Request::Common::POST( $url, [ @_ ] ) :
HTTP::Request::Common::GET( $url );

# "schacon/token:6ef8395fecf207165f1a82178ae1b984"
my $auth_basic = $self->login . '/token:' . $self->token;
$req->header('Authorization', 'Basic ' . encode_base64($auth_basic));

my $res = $self->ua->request($req);
return { error => '404 Not Found' } if $res->code == 404;

# Slow down if we're approaching the rate limit
# By the way GitHub mistakes days for minutes in their documentation --
# the rate limit is per minute, not per day.
if ( $self->api_throttle ) {
sleep 2 if (($res->header('x-ratelimit-remaining') || 0)
< ($res->header('x-ratelimit-limit') || 60) / 2);
}

my $json = $res->content();
my $data = $self->json->jsonToObj($json);
my $data = eval { $self->json->jsonToObj($json) };
unless ($data) {
# We tolerate bad JSON for errors,
# otherwise we just rethrow the JSON parsing problem.
die unless $res->is_error;
$data = { error => $res->message };
}

if ( $self->throw_errors ) {
croak (ref $data->{error} eq 'ARRAY' ? $data->{error}[0] : $data->{error})
if exists $data->{error};
}

return $data->{$key} if ( $key and exists $data->{$key} );
return $data;
Expand Down
23 changes: 23 additions & 0 deletions lib/Net/GitHub/V2/Repositories.pm
Expand Up @@ -86,6 +86,19 @@ sub delete {
}
}

sub update {
my ( $self, %up ) = @_;

my $repo = $self->repo;

my @values;
foreach my $key ( keys %up ) {
push @values, ( "values[$key]", $up{$key} );
}

return $self->get_json_to_obj_authed( "repos/show/$repo", @values, 'repository' );
}

sub set_private {
my ( $self ) = @_;

Expand Down Expand Up @@ -269,6 +282,16 @@ create a new repository (authentication required). $name are required. like 'per
delete a repository (authentication required)
=item update
$organization->update(
description => 'Linux kernel rewritten in Visual Basic',
has_wiki => 0,
has_issues => 1,
has_downloads => 0);
change repository metadata.
=item set_private
=item set_public
Expand Down

0 comments on commit 91291fc

Please sign in to comment.