The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

WWW::Suffit::UserAgent - Suffit API user agent library

VERSION

Version 1.00

SYNOPSIS

    use WWW::Suffit::UserAgent;

    my $clinet = WWW::Suffit::UserAgent->new(
        url                 => "https://localhost",
        username            => "username", # optional
        password            => "password", # optional
        max_redirects       => 2, # Default: 10
        connect_timeout     => 3, # Default: 10 sec
        inactivity_timeout  => 5, # Default: 30 sec
        request_timeout     => 10, # Default: 5 min (300 sec)
    );
    my $status = $client->check();

    if ($status) {
        print STDOUT $client->res->body;
    } else {
        print STDERR $clinet->error;
    }

DESCRIPTION

Suffit API user agent library

new

    my $clinet = WWW::Suffit::UserAgent->new(
        url                 => "https://localhost",
        username            => "username", # optional
        password            => "password", # optional
        max_redirects       => 2, # Default: 10
        connect_timeout     => 3, # Default: 10 sec
        inactivity_timeout  => 5, # Default: 30 sec
        request_timeout     => 10, # Default: 5 min (300 sec)
    );

Returns the client instance

auth_scheme

Sets the authentication scheme. HTTP Authentication Schemes: Bearer, Basic, ApiKey

Default: ApiKey (use token header)

ask_credentials

Enables ask username and password from terminal

max_redirects

Maximum number of redirects the user agent will follow before it fails. Default - 10

password

Default password for basic authentication

*timeout

Timeout for connections, requests and inactivity periods in seconds.

ua

The Mojo UserAgent object

url

Full URL of the WEB Server

username

Default username for basic authentication

METHODS

List of the User Agent interface methods

cleanup

    $client->cleanup;

Cleanup all variable data in object and returns client object

code

    my $code = $clinet->code;
    $client  = $clinet->code(200);

Returns HTTP code of the response

credentials

    my $userinfo = $client->credentials(1);

Gets credentials for User Agent

error

    print $clinet->error;
    $clinet = $clinet->error("My error");

Returns error string

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

private_key

    $clinet = $clinet->private_key("---- BEGIN ... END -----");
    my $private_key = $client->private_key;

Sets or returns RSA private key

public_key

    $clinet = $clinet->public_key("---- BEGIN ... END -----");
    my $public_key = $client->public_key;

Sets or returns RSA public key

proxy

    my $proxy = $client->proxy;
    $client->proxy('http://47.88.62.42:80');

Get or set proxy

req

    my $request = $clinet->req;

Returns Mojo::Message::Request object

request

    my $json = $clinet->request("METHOD", "PATH", ...ATTRIBUTES...);

Send request

res

    my $response = $clinet->res;

Returns Mojo::Message::Response object

status

    my $status = $clinet->status;
    $clinet    = $clinet->status(1);

Returns object status value. 0 - Error; 1 - Ok

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

token

    $clinet = $clinet->token("abc123...fcd");
    my $token = $client->token;

Returns token

trace

    my $trace = $client->trace;
    print $client->trace("New trace record");

Gets trace stack or pushes new trace record to trace stack

tx

    my $status = $clinet->tx($tx);

Works with Mojo::Transaction object, interface with it

tx_string

    print $client->tx_string;

Retruns transaction status string

ua

    my $ua = $clinet->ua;

Returns Mojo::UserAgent object

url

    my $url_object = $clinet->url;

Returns Mojo::URL object

API METHODS

List of predefined the Suffit API methods

check

    my $status = $client->check;
    my $status = $client->check( URLorPath );

Returns check-status of server. 0 - Error; 1 - Ok

HTTP BASIC AUTHORIZATION

For pass HTTP Basic Authorization with ask user credentials from console use follow code:

    my $client = WWW::Suffit::UserAgent->new(
        ask_credentials => 1,
        auth_scheme => 'Basic',
        # ...
    );

... and without ask:

    my $client = WWW::Suffit::UserAgent->new(
        username => 'test',
        password => 'test',
        # ...
    );

You can also use credentials in the userinfo part of a base URL:

    my $client = WWW::Suffit::UserAgent->new(
        url => 'https://test:test@localhost',
        # ...
    )

TLS CLIENT CERTIFICATES

    $client->ua->cert('tls.crt')->key('tls.key')->ca('ca.crt');

See "cert" in Mojo::UserAgent, "key" in Mojo::UserAgent, "ca" in Mojo::UserAgent and "tls_options" in Mojo::UserAgent

PROXY

In constructor:

    my $client = WWW::Suffit::UserAgent->new(
        proxy => 'http://47.88.62.42:80',
        # ...
    );

Before request:

    my $status = $client
        ->proxy('http://47.88.62.42:80')
        ->request(GET => $client->str2url('http://ifconfig.io/all.json'));

    # Socks5
    my $status = $client
        ->proxy('socks://socks:socks@192.168.201.129:1080')
        ->request(GET => $client->str2url('http://ifconfig.io/all.json'));

Directly:

    $client->ua->proxy
        ->http('http://47.88.62.42:80')
        ->https('http://188.125.173.185:8080');

    my $status = $client
        ->proxy('http://47.88.62.42:80')
        #->proxy('socks://socks:socks@192.168.201.129:1080')
        ->request(GET => $client->str2url('http://ifconfig.io/all.json'));

DEPENDENCIES

Mojolicious, Mojo::UserAgent

TO DO

See TODO file

SEE ALSO

Mojo::UserAgent

AUTHOR

Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>

COPYRIGHT

Copyright (C) 1998-2023 D&D Corporation. All Rights Reserved

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See LICENSE file and https://dev.perl.org/licenses/