-package utils;
+package GitHubBackup::API;
use strict;
use warnings;
use utf8;
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;
}
sub get {
- my $url = shift;
+ my $self = shift;
+ my $url = shift;
my %parameters = @_;
my $parameters = '';
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++;
return $data;
}
+sub access_token {
+ my $self = shift;
+
+ return $self->{access_token}->();
+}
+
+
package GitHubBackup;
use strict;
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});
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,
})
;
return $path;
}
+sub api {
+ my $self = shift;
+
+ return $self->{api}->();
+}
+
sub message {
my $self = shift;
my $message = shift;
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};
}
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 }