From bbbe48b60c7e25c5015d424cb4252cba275d4989 Mon Sep 17 00:00:00 2001 From: htrgouvea Date: Fri, 29 Nov 2024 12:42:03 +0000 Subject: [PATCH] new modules to optmize --- lib/Sentra/Utils/Repositories_List.pm | 37 +++++++++++++++++++++++++++ lib/Sentra/Utils/UserAgent.pm | 20 ++++++++++----- 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 lib/Sentra/Utils/Repositories_List.pm diff --git a/lib/Sentra/Utils/Repositories_List.pm b/lib/Sentra/Utils/Repositories_List.pm new file mode 100644 index 0000000..b2ce4be --- /dev/null +++ b/lib/Sentra/Utils/Repositories_List.pm @@ -0,0 +1,37 @@ +package Sentra::Utils::Repositories_List { + use strict; + use warnings; + use JSON; + use Sentra::Utils::UserAgent; + + sub new { + my ($self, $org, $token) = @_; + + my @repos; + my $page = 1; + my $userAgent = Sentra::Utils::UserAgent -> new($token); + + while (1) { + my $url = "https://api.github.com/orgs/$org/repos?per_page=100&page=$page"; + my $response = $userAgent -> get($url); + + if ($response -> code() == 200) { + my $data = decode_json($response -> content()); + + if (scalar(@$data) == 0) { + last; + } + + for my $repo (@$data) { + push @repos, "$org/$repo->{name}" unless $repo->{archived}; + } + + $page++; + } + } + + return @repos; + } +} + +1; \ No newline at end of file diff --git a/lib/Sentra/Utils/UserAgent.pm b/lib/Sentra/Utils/UserAgent.pm index 7f1d511..c75b65c 100644 --- a/lib/Sentra/Utils/UserAgent.pm +++ b/lib/Sentra/Utils/UserAgent.pm @@ -1,19 +1,27 @@ package Sentra::Utils::UserAgent { use strict; use warnings; - use Mojo::UserAgent; + use LWP::UserAgent; sub new { - my $userAgent = Mojo::UserAgent -> new(); + my ($self, $token) = @_; + + my $userAgent = LWP::UserAgent -> new( + timeout => 5, + ssl_opts => { + verify_hostname => 0, + SSL_verify_mode => 0 + }, + agent => "Sentra 0.0.3" + ); - my $headers = { + $userAgent -> default_headers -> header ( 'X-GitHub-Api-Version' => '2022-11-28', 'Accept' => 'application/vnd.github+json', - 'User-Agent' => 'Sentra 0.0.3', 'Authorization' => "Bearer $token" - }; + ); - return ""; + return $userAgent; } }