| 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
|
3
|
|
|
3
|
|
1725
|
use Moo::Role; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
21
|
|
|
6
|
3
|
|
|
3
|
|
1188
|
use Carp qw(croak); |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
235
|
|
|
7
|
3
|
|
|
3
|
|
1442
|
use JSON::MaybeXS qw(decode_json encode_json); |
|
|
3
|
|
|
|
|
33825
|
|
|
|
3
|
|
|
|
|
234
|
|
|
8
|
3
|
|
|
3
|
|
847
|
use HTTP::Request; |
|
|
3
|
|
|
|
|
40519
|
|
|
|
3
|
|
|
|
|
186
|
|
|
9
|
3
|
|
|
3
|
|
2503
|
use LWP::UserAgent; |
|
|
3
|
|
|
|
|
91064
|
|
|
|
3
|
|
|
|
|
177
|
|
|
10
|
3
|
|
|
3
|
|
538
|
use MIME::Base64 qw(encode_base64); |
|
|
3
|
|
|
|
|
789
|
|
|
|
3
|
|
|
|
|
214
|
|
|
11
|
3
|
|
|
3
|
|
21
|
use URI; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
83
|
|
|
12
|
3
|
|
|
3
|
|
16
|
use Log::Any qw($log); |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
29
|
|
|
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
|
2
|
|
|
2
|
|
9144
|
my ($self, $req) = @_; |
|
33
|
2
|
100
|
66
|
|
|
21
|
if (defined $self->token && length $self->token) { |
|
|
|
50
|
33
|
|
|
|
|
|
34
|
1
|
|
|
|
|
16
|
$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
|
2
|
|
|
|
|
157
|
return; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub request { |
|
44
|
0
|
|
|
0
|
1
|
|
my ($self, $method, $path, %args) = @_; |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $uri = URI->new($self->api_url . $path); |
|
47
|
0
|
0
|
|
|
|
|
$uri->query_form($args{query}) if $args{query}; |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new($method => $uri); |
|
50
|
0
|
|
|
|
|
|
$self->_apply_auth($req); |
|
51
|
0
|
|
|
|
|
|
$req->header(Accept => 'application/json'); |
|
52
|
0
|
0
|
|
|
|
|
for my $k (keys %{ $args{headers} || {} }) { |
|
|
0
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
$req->header($k => $args{headers}{$k}); |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
if (defined $args{body}) { |
|
57
|
0
|
|
0
|
|
|
|
my $ct = $args{content_type} || 'application/json'; |
|
58
|
0
|
|
|
|
|
|
$req->header('Content-Type' => $ct); |
|
59
|
0
|
0
|
|
|
|
|
$req->content(ref $args{body} ? encode_json($args{body}) : $args{body}); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
$log->debugf('Gitea %s %s', $method, $uri); |
|
63
|
0
|
|
|
|
|
|
my $res = $self->ua->request($req); |
|
64
|
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $body = $res->decoded_content; |
|
66
|
0
|
|
|
|
|
|
my $data; |
|
67
|
0
|
0
|
0
|
|
|
|
if (defined $body && length $body && $body =~ /\A\s*[\{\[]/) { |
|
|
|
|
0
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
$data = decode_json($body); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
unless ($res->is_success) { |
|
72
|
|
|
|
|
|
|
my $msg = ref $data eq 'HASH' |
|
73
|
0
|
0
|
0
|
|
|
|
? ($data->{message} || $data->{error} || $res->status_line) |
|
74
|
|
|
|
|
|
|
: $res->status_line; |
|
75
|
0
|
|
|
|
|
|
$log->errorf('Gitea API error: %s', $msg); |
|
76
|
0
|
|
|
|
|
|
croak "Gitea API error ($method $path): $msg"; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# 204 No Content (deletes etc.) — no body to decode |
|
80
|
0
|
0
|
|
|
|
|
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__ |