| 1 | package WWW::Gitea::Role::HTTP; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: HTTP + token/basic auth role for the Gitea REST API | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use JSON::MaybeXS qw(decode_json encode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use HTTP::Request::Common (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | requires 'api_url'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | requires 'token'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | requires 'username'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | requires 'password'; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | has ua => ( | ||||||||||||||||||||||||||||||||||||||||||
| 22 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_ua: not called CC=2 SCAR=17.9
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | LWP::UserAgent->new( | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$WWW::Gitea::VERSION // "dev"
$WWW::Gitea::VERSION // "dev"
MC/DC0%0/1
$WWW::Gitea::VERSION // "dev"
$WWW::Gitea::VERSION
MC/DC0%0/1
$WWW::Gitea::VERSION // "dev"
$WWW::Gitea::VERSION
Truth table0%0/2
A
A= $WWW::Gitea::VERSION
Truth table0%0/2
A
A= $WWW::Gitea::VERSION
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | agent => 'WWW-Gitea/' . ($WWW::Gitea::VERSION // 'dev'), | ||||||||||||||||||||||||||||||||||||||||||
| 26 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | ▶ | sub _apply_auth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_auth: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 8 | my ($self, $req) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 8 | ▶ | if (defined $self->token && length $self->token) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $self->token and length $self->token
MC/DC50%1/2
defined $self->token and length $self->token
defined $self->token
length $self->token
Truth table66%2/3
A and B
A= defined $self->tokenB= length $self->token
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | $req->header(Authorization => 'token ' . $self->token); | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ▶ | elsif (defined $self->username && defined $self->password) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $self->username and defined $self->password
MC/DC0%0/2
defined $self->username and defined $self->password
defined $self->username
defined $self->password
Truth table33%1/3
A and B
A= defined $self->usernameB= defined $self->password
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | $req->header(Authorization => 'Basic ' | |||||||||||||||||||||||||||||||||||||||||
| 39 | . encode_base64($self->username . ':' . $self->password, '')); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 6 | ▶ | sub request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: called CC=19 SCAR=39.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | my ($self, $method, $path, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 6 | my $uri = URI->new($self->api_url . $path); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 6 | ▶ | $uri->query_form($args{query}) if $args{query}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | my $req; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | ▶ | if (my $up = $args{upload}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | my $field = $up->{field} || 'attachment'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$up->{'field'} || 'attachment'
MC/DC0%0/1
$up->{'field'} || 'attachment'
$up->{'field'}
Truth table50%1/2
A
A= $up->{'field'}
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | my $value = defined $up->{file} | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ? [ $up->{file}, $up->{filename} ] | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | : [ undef, $up->{filename}, Content => ($up->{content} // '') ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$up->{"content"} // ""
MC/DC0%0/1
$up->{"content"} // ""
$up->{"content"}
Truth table50%1/2
A
A= $up->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | $req = HTTP::Request::Common::POST( | |||||||||||||||||||||||||||||||||||||||||
| 57 | "$uri", | ||||||||||||||||||||||||||||||||||||||||||
| 58 | Content_Type => 'form-data', | ||||||||||||||||||||||||||||||||||||||||||
| 59 | Content => [ $field => $value ], | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | $req->method($method); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | $req = HTTP::Request->new($method => $uri); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | $self->_apply_auth($req); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | $req->header(Accept => 'application/json'); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | ▶ | for my $k (keys %{ $args{headers} || {} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'headers'} || {}
MC/DC0%0/1
$args{'headers'} || {}
$args{'headers'}
Truth table50%1/2
A
A= $args{'headers'}
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $req->header($k => $args{headers}{$k}); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | if (defined $args{body}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my $ct = $args{content_type} || 'application/json'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'content_type'} || 'application/json'
MC/DC0%0/1
$args{'content_type'} || 'application/json'
$args{'content_type'}
Truth table0%0/2
A
A= $args{'content_type'}
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $req->header('Content-Type' => $ct); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $req->content(ref $args{body} ? encode_json($args{body}) : $args{body}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 6 | $log->debugf('Gitea %s %s', $method, $uri); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | my $res = $self->ua->request($req); | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | my $body = $res->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | my $data; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | ▶ | if (defined $body && length $body && $body =~ /\A\s*[\{\[]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $body and length $body and $body =~ /\A\s*[\{\[]/
defined $body and length $body
MC/DC0%0/3
defined $body and length $body and $body =~ /\A\s*[\{\[]/
defined $body
length $body
$body =~ /\A\s*[\{\[]/
Truth table25%1/4
A and B and C
A= defined $bodyB= length $bodyC= $body =~ /\A\s*[\{\[]/
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | $data = decode_json($body); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | unless ($res->is_success) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | my $msg = ref $data eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | ? ($data->{message} || $data->{error} || $res->status_line) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$data->{"message"} or $data->{"error"} or $res->status_line
$data->{"message"} or $data->{"error"}
MC/DC0%0/3
$data->{"message"} or $data->{"error"} or $res->status_line
$data->{"message"}
$data->{"error"}
$res->status_line
Truth table0%0/4
A or B or C
A= $data->{"message"}B= $data->{"error"}C= $res->status_line
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | : $res->status_line; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $log->errorf('Gitea API error: %s', $msg); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | croak "Gitea API error ($method $path): $msg"; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | # 204 No Content (deletes etc.) — no body to decode | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | ▶ | return defined $data ? $data : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub request_status { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_status: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my ($self, $method, $path, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $uri = URI->new($self->api_url . $path); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $uri->query_form($args{query}) if $args{query}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $req = HTTP::Request->new($method => $uri); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->_apply_auth($req); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $req->header(Accept => 'application/json'); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $log->debugf('Gitea %s %s (status only)', $method, $uri); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return $self->ua->request($req)->code; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||||||||||||