File Coverage

blib/lib/WWW/Gitea/Role/HTTP.pm
Criterion Covered Total %
statement 46 62 74.1
branch 9 22 40.9
condition 5 19 26.3
subroutine 10 12 83.3
pod 2 2 100.0
total 72 117 61.5


line stmt bran cond sub pod time code
1             package WWW::Gitea::Role::HTTP;
2              
3             # ABSTRACT: HTTP + token/basic auth role for the Gitea REST API
4              
5 4     4   1862 use Moo::Role;
  4         7  
  4         47  
6 4     4   1442 use Carp qw(croak);
  4         6  
  4         251  
7 4     4   1510 use JSON::MaybeXS qw(decode_json encode_json);
  4         33881  
  4         254  
8 4     4   1142 use HTTP::Request;
  4         34332  
  4         127  
9 4     4   2445 use LWP::UserAgent;
  4         94827  
  4         181  
10 4     4   1045 use MIME::Base64 qw(encode_base64);
  4         1774  
  4         278  
11 4     4   27 use URI;
  4         4  
  4         113  
12 4     4   18 use Log::Any qw($log);
  4         8  
  4         36  
13              
14              
15             requires 'api_url';
16             requires 'token';
17             requires 'username';
18             requires 'password';
19              
20             has ua => (
21             is => 'lazy',
22             builder => sub {
23 0   0 0   0 LWP::UserAgent->new(
24             agent => 'WWW-Gitea/' . ($WWW::Gitea::VERSION // 'dev'),
25             timeout => 30,
26             );
27             },
28             );
29              
30              
31             sub _apply_auth {
32 6     6   9529 my ($self, $req) = @_;
33 6 100 66     49 if (defined $self->token && length $self->token) {
    50 33        
34 5         62 $req->header(Authorization => 'token ' . $self->token);
35             }
36             elsif (defined $self->username && defined $self->password) {
37 1         11 $req->header(Authorization => 'Basic '
38             . encode_base64($self->username . ':' . $self->password, ''));
39             }
40 6         430 return;
41             }
42              
43             sub request {
44 4     4 1 10 my ($self, $method, $path, %args) = @_;
45              
46 4         77 my $uri = URI->new($self->api_url . $path);
47 4 50       8133 $uri->query_form($args{query}) if $args{query};
48              
49 4         373 my $req = HTTP::Request->new($method => $uri);
50 4         284 $self->_apply_auth($req);
51 4         38 $req->header(Accept => 'application/json');
52 4 50       127 for my $k (keys %{ $args{headers} || {} }) {
  4         19  
53 0         0 $req->header($k => $args{headers}{$k});
54             }
55              
56 4 50       12 if (defined $args{body}) {
57 0   0     0 my $ct = $args{content_type} || 'application/json';
58 0         0 $req->header('Content-Type' => $ct);
59 0 0       0 $req->content(ref $args{body} ? encode_json($args{body}) : $args{body});
60             }
61              
62 4         17 $log->debugf('Gitea %s %s', $method, $uri);
63 4         90 my $res = $self->ua->request($req);
64              
65 4         353 my $body = $res->decoded_content;
66 4         509 my $data;
67 4 50 33     27 if (defined $body && length $body && $body =~ /\A\s*[\{\[]/) {
      33        
68 4         20 $data = decode_json($body);
69             }
70              
71 4 50       12 unless ($res->is_success) {
72             my $msg = ref $data eq 'HASH'
73 0 0 0     0 ? ($data->{message} || $data->{error} || $res->status_line)
74             : $res->status_line;
75 0         0 $log->errorf('Gitea API error: %s', $msg);
76 0         0 croak "Gitea API error ($method $path): $msg";
77             }
78              
79             # 204 No Content (deletes etc.) — no body to decode
80 4 50       61 return defined $data ? $data : 1;
81             }
82              
83              
84             sub request_status {
85 0     0 1   my ($self, $method, $path, %args) = @_;
86 0           my $uri = URI->new($self->api_url . $path);
87 0 0         $uri->query_form($args{query}) if $args{query};
88 0           my $req = HTTP::Request->new($method => $uri);
89 0           $self->_apply_auth($req);
90 0           $req->header(Accept => 'application/json');
91 0           $log->debugf('Gitea %s %s (status only)', $method, $uri);
92 0           return $self->ua->request($req)->code;
93             }
94              
95              
96              
97             1;
98              
99             __END__