WWW-Suffit-UserAgent
view release on metacpan or search on metacpan
lib/WWW/Suffit/UserAgent.pm view on Meta::CPAN
Timeout for connections, requests and inactivity periods in seconds.
=item B<ua>
The Mojo UserAgent object
=item B<url>
Full URL of the WEB Server
=item B<username>
Default username for basic authentication
=back
=head1 METHODS
List of the User Agent interface methods
=head2 cleanup
$client->cleanup;
Cleanup all variable data in object and returns client object
=head2 code
my $code = $clinet->code;
$client = $clinet->code(200);
Returns HTTP code of the response
=head2 credentials
my $userinfo = $client->credentials(1);
Gets credentials for User Agent
=head2 error
print $clinet->error;
$clinet = $clinet->error("My error");
Returns error string
=head2 path2url
# For url = http://localhost:8695/api
my $url_str = $client->path2url("/foo/bar");
# http://localhost:8695/api/foo/bar
Merges path to tail of url
# For url = http://localhost:8695/api
my $url_str = $client->path2url("/foo/bar", 1);
# http://localhost:8695/foo/bar
Sets path to url
=head2 private_key
$clinet = $clinet->private_key("---- BEGIN ... END -----");
my $private_key = $client->private_key;
Sets or returns RSA private key
=head2 public_key
$clinet = $clinet->public_key("---- BEGIN ... END -----");
my $public_key = $client->public_key;
Sets or returns RSA public key
=head2 proxy
my $proxy = $client->proxy;
$client->proxy('http://47.88.62.42:80');
Get or set proxy
=head2 req
my $request = $clinet->req;
Returns Mojo::Message::Request object
=head2 request
my $json = $clinet->request("METHOD", "PATH", ...ATTRIBUTES...);
Send request
=head2 res
my $response = $clinet->res;
Returns Mojo::Message::Response object
=head2 status
my $status = $clinet->status;
$clinet = $clinet->status(1);
Returns object status value. 0 - Error; 1 - Ok
=head2 str2url
# http://localhost/api -> http://localhost/api/foo/bar
my $url = $self->str2url("foo/bar");
# http://localhost/api -> http://localhost/foo/bar
my $url = $self->str2url("/foo/bar");
# http://localhost/api/baz -> http://localhost/api/baz
my $url = $self->str2url("http://localhost/api/baz");
Returns URL from specified sting
=head2 token
$clinet = $clinet->token("abc123...fcd");
my $token = $client->token;
lib/WWW/Suffit/UserAgent.pm view on Meta::CPAN
=head1 COPYRIGHT
Copyright (C) 1998-2026 D&D Corporation
=head1 LICENSE
This program is distributed under the terms of the Artistic License Version 2.0
See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
=cut
our $VERSION = '1.02';
use Mojo::UserAgent;
use Mojo::UserAgent::Proxy;
use Mojo::Asset::File;
use Mojo::URL;
use Mojo::Util qw/steady_time b64_encode/;
use WWW::Suffit::Const qw/ DEFAULT_URL TOKEN_HEADER_NAME /;
use Acrux::Util qw/ fbytes fduration /;
use constant {
MAX_REDIRECTS => 10,
CONNECT_TIMEOUT => 10,
INACTIVITY_TIMEOUT => 30,
REQUEST_TIMEOUT => 180,
TRANSACTION_MASK => "%s %s >>> %s %s [%s in %s%s]", # GET /info >>> 200 OK [1.04 KB in 0.0242 seconds (43.1 KB/sec)]
CONTENT_TYPE => 'application/json',
REALM => 'Restricted zone',
};
sub new {
my $class = shift;
my %args = @_;
# General
$args{status} = 1; # Boolean status: 0 - error, 1 - ok
$args{error} = ""; # Error string (message) or HTTP Error message
$args{code} = 0; # HTTP Error code (integer) or error code string value (default is integer)
# Base URL & URL Prefix
$args{url} = Mojo::URL->new($args{url} || DEFAULT_URL); # base url
$args{prefix} = $args{url}->path->to_string // ''; $args{prefix} =~ s/\/+$//;
# HTTP Basic Authorization credentials
$args{credentials} = "";
$args{auth_scheme} ||= "";
$args{username} //= $args{url}->username // '';
$args{password} //= $args{url}->password // '';
$args{ask_credentials} ||= 0;
# API/Access/Session token
$args{token} //= "";
$args{token_name} ||= TOKEN_HEADER_NAME;
# Security
$args{public_key} //= "";
$args{private_key} //= "";
# Proxy string
$args{proxy} //= "";
# Transaction (tx)
$args{trace} = []; # trace pool
$args{tx_string} = "";
$args{tx_time} = 0;
$args{req} = undef;
$args{res} = undef;
# User Agent
my $ua = $args{ua};
unless ($ua) {
# Create the instance
$ua = Mojo::UserAgent->new(
max_redirects => $args{max_redirects} || MAX_REDIRECTS,
connect_timeout => $args{connect_timeout} || CONNECT_TIMEOUT,
inactivity_timeout => $args{inactivity_timeout} || INACTIVITY_TIMEOUT,
request_timeout => $args{request_timeout} || REQUEST_TIMEOUT,
insecure => $args{insecure} || 0,
);
$ua->transactor->name(sprintf("%s/%s", __PACKAGE__, __PACKAGE__->VERSION));
# Set proxy
my $proxy = Mojo::UserAgent::Proxy->new;
$ua->proxy($proxy->http($args{proxy})->https($args{proxy})) if $args{proxy};
$args{ua} = $ua;
}
my $self = bless {%args}, $class;
return $self;
}
## INTERFACE METHODS
sub error {
my $self = shift;
my $e = shift;
if (defined $e) {
$self->{error} = $e;
return $self;
}
return $self->{error};
}
sub status {
my $self = shift;
my $s = shift;
if (defined $s) {
$self->{status} = $s;
return $self;
}
return $self->{status};
}
sub code {
my $self = shift;
my $c = shift;
if (defined $c) {
$self->{code} = $c;
return $self;
}
return $self->{code};
}
sub trace {
my $self = shift;
my $v = shift;
if (defined($v)) {
my $a = $self->{trace};
push @$a, $v;
return $v;
}
my $trace = $self->{trace} || [];
return join("\n",@$trace);
}
sub token {
my $self = shift;
my $t = shift;
if (defined $t) {
$self->{token} = $t;
return $self;
}
return $self->{token};
}
sub public_key {
my $self = shift;
my $k = shift;
if (defined $k) {
$self->{public_key} = $k;
return $self;
}
return $self->{public_key};
}
sub private_key {
my $self = shift;
my $k = shift;
if (defined $k) {
$self->{private_key} = $k;
return $self;
}
return $self->{private_key};
}
sub proxy {
my $self = shift;
my $p = shift;
return $self->{proxy} unless defined $p;
$self->{proxy} = $p;
# Set proxy
$self->ua->proxy->http($p)->https($p) if length $p;
return $self;
}
sub cleanup {
my $self = shift;
$self->{status} = 1;
$self->{error} = "";
$self->{code} = 0;
$self->{tx_string} = "";
undef $self->{req};
$self->{req} = undef;
undef $self->{res};
$self->{res} = undef;
undef $self->{trace};
$self->{trace} = [];
return $self;
}
sub req {
my $self = shift;
return $self->{req};
}
sub res {
my $self = shift;
return $self->{res};
}
sub url {
my $self = shift;
return $self->{url};
}
sub tx_string {
my $self = shift;
return $self->{tx_string} // '';
}
sub path2url {
my $self = shift;
my $p = shift // "/";
my $r = shift; # Is root, no use preffix
my $url = $self->url->clone;
my $path = $r ? $p : sprintf("%s/%s", $self->{prefix}, $p);
$path =~ s/\/{2,}/\//g;
return $url->path_query($path)->to_string;
}
sub str2url {
my $self = shift;
my $str = shift // "";
if ($str =~ /^https?\:\/\//) { # url (http/https)
return $str;
} elsif ($str =~ /^\//) { # absolute path (started from root, e.g.: /foo/bar)
return $self->path2url($str, 1);
} elsif (length $str) { # relative path (started from tail of base url, e.g.: foo/bar)
return $self->path2url($str);
( run in 0.789 second using v1.01-cache-2.11-cpan-39bf76dae61 )