1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 package Arvados::Request;
11 @Arvados::HTTP::ISA = qw(LWP::UserAgent);
17 bless ($self, $class);
18 return $self->_init(@_);
24 $self->{'ua'} = new LWP::UserAgent(@_);
25 $self->{'ua'}->agent ("libarvados-perl/".$Arvados::VERSION);
32 $self->{'uri'} = shift;
40 my $method = $self->{'method'};
41 if ($method eq 'GET' || $method eq 'HEAD') {
42 $content{'_method'} = $method;
45 $req{$method} = $self->{'uri'};
46 $self->{'req'} = new HTTP::Request (%req);
47 $self->{'req'}->header('Authorization' => ('OAuth2 ' . $self->{'authToken'})) if $self->{'authToken'};
48 $self->{'req'}->header('Accept' => 'application/json');
50 # allow_nonref lets us encode JSON::true and JSON::false, see #12078
51 my $json = JSON->new->allow_nonref;
53 while (($p, $v) = each %{$self->{'queryParams'}}) {
54 $content{$p} = (ref($v) eq "") ? $v : $json->encode($v);
57 while (($p, $v) = each %content) {
58 $content .= '&' unless $content eq '';
59 $content .= uri_escape($p);
61 $content .= uri_escape($v);
63 $self->{'req'}->content_type("application/x-www-form-urlencoded; charset='utf8'");
64 $self->{'req'}->content(Encode::encode('utf8', $content));
65 $self->{'res'} = $self->{'ua'}->request ($self->{'req'});
71 return ($self->{'res'}->code(),
72 $self->{'res'}->message());
78 return $self->{'res'}->content;
84 $self->{'method'} = shift;
90 $self->{'queryParams'} = shift;
96 $self->{'authToken'} = shift;