From 13e240051b38965d9bcdf26e6f638fc0468cad56 Mon Sep 17 00:00:00 2001 From: Ken-ichi Mito Date: Wed, 21 Aug 2013 16:38:15 +0900 Subject: [PATCH] change package utils to GitHubBackup::API * add GitHubBackup::API->access_token to use OAuth with json api --- Dev/github/GitHubBackup.pm | 64 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/Dev/github/GitHubBackup.pm b/Dev/github/GitHubBackup.pm index 6542ef3..30fd705 100644 --- a/Dev/github/GitHubBackup.pm +++ b/Dev/github/GitHubBackup.pm @@ -1,4 +1,4 @@ -package utils; +package GitHubBackup::API; use strict; use warnings; use utf8; @@ -7,8 +7,16 @@ use Carp qw(croak); use LWP::UserAgent; use JSON; +sub new { + my $class = shift; + my $args = shift; + + return bless $args, $class; +} + sub json_api { - my $url = shift; + my $self = shift; + my $url = shift; my $ua = LWP::UserAgent->new; my $json = JSON->new->utf8->indent; @@ -23,7 +31,8 @@ sub json_api { } sub get { - my $url = shift; + my $self = shift; + my $url = shift; my %parameters = @_; my $parameters = ''; @@ -34,7 +43,7 @@ sub get { my $page = 1; my $data = []; while(1) { - my $result = json_api("$url?per_page=100&page=$page$parameters"); + my $result = $self->json_api("$url?per_page=100&page=$page$parameters"); if (ref($result) eq 'ARRAY' && scalar @$result > 0) { push @$data, @$result; $page++; @@ -47,6 +56,13 @@ sub get { return $data; } +sub access_token { + my $self = shift; + + return $self->{access_token}->(); +} + + package GitHubBackup; use strict; @@ -99,6 +115,29 @@ sub directory { return $self->{directory}; } +sub access_token { + my $self = shift; + my $args = shift; + + if (defined $args) { + $self->{access_token} = $args; + } + + return $self->{access_token}; +} + +sub api { + my $self = shift; + + unless ($self->{api}) { + $self->{api} = GitHubBackup::API->new({ + access_token => sub {$self->access_token}, + }); + } + + return $self->{api}; +} + sub repos { my $self = shift; return $self->{repos} if ($self->{repos}); @@ -108,16 +147,17 @@ sub repos { my $account = $self->account or croak "account is not set"; my $result; if (my $repository = $self->repository) { - $result = [ utils::json_api("/repos/$account/$repository") ]; + $result = [ $self->api->json_api("/repos/$account/$repository") ]; } else { - $result = utils::get("/users/$account/repos"); + $result = $self->api->get("/users/$account/repos"); } foreach my $repos (@$result) { push @{$self->{repos}}, GitHubBackup::Repository->new({ directory => sub {$self->directory}, + api => sub {$self->api}, repos => $repos, }) ; @@ -193,6 +233,12 @@ sub directory { return $path; } +sub api { + my $self = shift; + + return $self->{api}->(); +} + sub message { my $self = shift; my $message = shift; @@ -236,7 +282,7 @@ sub forks { my $self = shift; return $self->{forks} if ($self->{forks}); - $self->{forks} = utils::get("/repos/" . $self->full_name . "/forks"); + $self->{forks} = $self->api->get("/repos/" . $self->full_name . "/forks"); return $self->{forks}; } @@ -291,8 +337,8 @@ sub issues { my $self = shift; return $self->{issues} if ($self->{issues}); - my $open = utils::get("/repos/" . $self->full_name . "/issues"); - my $closed = utils::get("/repos/" . $self->full_name . "/issues", state => 'closed'); + my $open = $self->api->get("/repos/" . $self->full_name . "/issues"); + my $closed = $self->api->get("/repos/" . $self->full_name . "/issues", state => 'closed'); if ($open) { push @{$self->{issues}}, @$open } if ($closed) { push @{$self->{issues}}, @$closed } -- 1.7.9.5