File: RESTClient.pm

package info (click to toggle)
libgitlab-api-v4-perl 0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,500 kB
  • sloc: perl: 5,960; sh: 838; python: 63; makefile: 12
file content (279 lines) | stat: -rw-r--r-- 7,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package GitLab::API::v4::RESTClient;
our $VERSION = '0.27';

=encoding utf8

=head1 NAME

GitLab::API::v4::RESTClient - The HTTP client that does the heavy lifting.

=head1 DESCRIPTION

Currently this class uses L<HTTP::Tiny> and L<JSON::MaybeXS> to do its job.
This may change, and the interface may change, so documentation is lacking in
order to not mislead people.

If you do want to customize how this class works then take a look at the
source.

=head1 ATTRIBUTES

=head1 http_tiny_request

    my $req = $api->rest_client->http_tiny_request();

The most recent request arrayref as passed to L<HTTP::Tiny>.

If this is C<undef> then no request has been made.

=head1 http_tiny_response

    my $res = $api->rest_client->http_tiny_response();

The most recent response hashref as passed back from L<HTTP::Tiny>.

If this is C<undef> and L</request> is defined then no response was received
and you will have encountered an error when making the request

=cut

use Carp qw();
use HTTP::Tiny::Multipart;
use HTTP::Tiny;
use JSON::MaybeXS;
use Log::Any qw( $log );
use Path::Tiny;
use Try::Tiny;
use Types::Common::Numeric -types;
use Types::Common::String -types;
use Types::Standard -types;
use URI::Escape;
use URI;

use Moo;
use strictures 2;
use namespace::clean;

sub croak {
    local $Carp::Internal{ 'GitLab::API::v4' } = 1;
    local $Carp::Internal{ 'GitLab::API::v4::RESTClient' } = 1;

    return Carp::croak( @_ );
}

sub croakf {
    my $msg = shift;
    $msg = sprintf( $msg, @_ );
    return croak( $msg );
}

has _clean_base_url => (
    is       => 'lazy',
    init_arg => undef,
    builder  => '_build_clean_base_url',
);
sub _build_clean_base_url {
    my ($self) = @_;
    my $url = $self->base_url();

    # Remove any leading slash so that request() does not build URLs
    # with double slashes when joining the base_url with the path.
    # If double slashes were allowed then extra unecessary redirects
    # could happen.
    $url =~ s{/+$}{};

    return URI->new( $url )->canonical();
}

has base_url => (
    is       => 'ro',
    isa      => NonEmptySimpleStr,
    required => 1,
);

has retries => (
    is      => 'ro',
    isa     => PositiveOrZeroInt,
    default => 0,
);

has http_tiny => (
    is  => 'lazy',
    isa => InstanceOf[ 'HTTP::Tiny' ],
);
sub _build_http_tiny {
    return HTTP::Tiny->new( verify_SSL => 1 );
}

has json => (
    is  => 'lazy',
    isa => HasMethods[ 'encode', 'decode' ],
);
sub _build_json {
    return JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1);
}

has http_tiny_request => (
    is       => 'ro',
    writer   => '_set_request',
    clearer  => '_clear_request',
    init_arg => undef,
);

has http_tiny_response => (
    is       => 'ro',
    writer   => '_set_response',
    clearer  => '_clear_response',
    init_arg => undef,
);

# The purpose of this method is for tests to have a place to inject themselves.
sub _http_tiny_request {
    my ($self, $req_method, $req) = @_;

    return $self->http_tiny->$req_method( @$req );
}

sub request {
    my ($self, $verb, $raw_path, $path_vars, $options) = @_;

    $self->_clear_request();
    $self->_clear_response();

    $options = { %{ $options || {} } };
    my $query = delete $options->{query};
    my $content = delete $options->{content};
    my $headers = $options->{headers} = { %{ $options->{headers} || {} } };

    # Convert foo/:bar/baz into foo/%s/baz.
    my $path = $raw_path;
    $path =~ s{:[^/]+}{%s}g;
    # sprintf will throw if the number of %s doesn't match the size of @$path_vars.
    # Might be nice to catch that and provide a better error message, but that should
    # never happen as the API methods verify the argument size before we get here.
    $path = sprintf($path, (map { uri_escape($_) } @$path_vars)) if @$path_vars;

    $log->tracef( 'Making %s request against %s', $verb, $path );

    my $url = $self->_clean_base_url->clone();
    $url->path( $url->path() . '/' . $path );
    $url->query_form( $query ) if defined $query;
    $url = "$url"; # No more changes to the url from this point forward.

    my $req_method = 'request';
    my $req = [ $verb, $url, $options ];

    if ($verb eq 'POST' and ref($content) eq 'HASH' and $content->{file}) {
        $content = { %$content };
        my $file = path( delete $content->{file} );

        unless (-f $file and -r $file) {
            local $Carp::Internal{ 'GitLab::API::v4' } = 1;
            local $Carp::Internal{ 'GitLab::API::v4::RESTClient' } = 1;
            croak "File $file is not readable";
        }

        # Might as well mask the filename, but leave the extension.
        my $filename = $file->basename(); # foo/bar.txt => bar.txt

        my $data = {
            file => {
                filename => $filename,
                content  => $file->slurp(),
            },
        };

        $req->[0] = $req->[1]; # Replace method with url.
        $req->[1] = $data; # Put data where url was.
        # So, req went from [$verb,$url,$options] to [$url,$data,$options],
        # per the post_multipart interface.

        $req_method = 'post_multipart';
        $content = undef if ! %$content;
    }

    if (ref $content) {
        $content = $self->json->encode( $content );
        $headers->{'content-type'} = 'application/json';
        $headers->{'content-length'} = length( $content );
    }

    $options->{content} = $content if defined $content;

    $self->_set_request( $req );

    my $res;
    my $tries_left = $self->retries();
    do {
        $res = $self->_http_tiny_request( $req_method, $req );
        if ($res->{status} =~ m{^5}) {
            $tries_left--;
            $log->warn('Request failed; retrying...') if $tries_left > 0;
        }
        else {
            $tries_left = 0
        }
    } while $tries_left > 0;

    $self->_set_response( $res );

    if ($res->{status} eq '404' and $verb eq 'GET') {
        return undef;
    }

    # Special case for:
    # https://github.com/bluefeet/GitLab-API-v4/issues/35#issuecomment-515533017
    if ($res->{status} eq '403' and $verb eq 'GET' and $raw_path eq 'projects/:project_id/releases/:tag_name') {
        return undef;
    }

    if ($res->{success}) {
        return undef if $res->{status} eq '204';

        my $decode = $options->{decode};
        $decode = 1 if !defined $decode;
        return $res->{content} if !$decode;

        return try{
            $self->json->decode( $res->{content} );
        }
        catch {
            croakf(
                'Error decoding JSON (%s %s %s): ',
                $verb, $url, $res->{status}, $_,
            );
        };
    }

    my $glimpse = $res->{content} || '';
    $glimpse =~ s{\s+}{ }g;
    if ( length($glimpse) > 50 ) {
        $glimpse = substr( $glimpse, 0, 50 );
        $glimpse .= '...';
    }

    croakf(
        'Error %sing %s (HTTP %s): %s %s',
        $verb, $url,
        $res->{status}, ($res->{reason} || 'Unknown'),
        $glimpse,
    );
}

1;
__END__

=head1 SUPPORT

See L<GitLab::API::v4/SUPPORT>.

=head1 AUTHORS

See L<GitLab::API::v4/AUTHORS>.

=head1 LICENSE

See L<GitLab::API::v4/LICENSE>.

=cut