| 1 | package WWW::Suffit::Client; | ||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | WWW::Suffit::Client - The Suffit API client library | ||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | use WWW::Suffit::Client; | ||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | my $clinet = WWW::Suffit::Client->new( | ||||||||||||||||||||||||||||||||||||||
| 17 | url => "https://localhost", | ||||||||||||||||||||||||||||||||||||||
| 18 | username => "username", # optional | ||||||||||||||||||||||||||||||||||||||
| 19 | password => "password", # optional | ||||||||||||||||||||||||||||||||||||||
| 20 | max_redirects => 2, # Default: 10 | ||||||||||||||||||||||||||||||||||||||
| 21 | connect_timeout => 3, # Default: 10 sec | ||||||||||||||||||||||||||||||||||||||
| 22 | inactivity_timeout => 5, # Default: 30 sec | ||||||||||||||||||||||||||||||||||||||
| 23 | request_timeout => 10, # Default: 5 min (300 sec) | ||||||||||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||||||||||
| 25 | my $status = $client->check(); | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | if ($status) { | ||||||||||||||||||||||||||||||||||||||
| 28 | print STDOUT $client->res->body; | ||||||||||||||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||||||||||||
| 30 | print STDERR $clinet->error; | ||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | This library provides methods for access to Suffit API servers | ||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | List of extended API methods | ||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | =head2 apicode | ||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | my $apicode = $client->apicode; | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | This method returns the API error code in format: Exxxx | ||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | =head2 apierr | ||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | die $client->apierr; | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | This method returns the value of the "/message" API parameter or a client error if no message found. | ||||||||||||||||||||||||||||||||||||||
| 52 | Otherwise, this method returns a string containing an HTTP error message | ||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | =head1 API METHODS | ||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | List of predefined the Suffit API methods | ||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | =head2 api_check | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | my $status = $client->api_check; | ||||||||||||||||||||||||||||||||||||||
| 61 | my $status = $client->api_check( URLorPath ); | ||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | Returns API check-status. 0 - Error; 1 - Ok | ||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | =head2 api_data | ||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | my $status = $client->api_data; | ||||||||||||||||||||||||||||||||||||||
| 68 | my $status = $client->api_data( URLorPath ); | ||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | Gets API data | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | =head2 api_status | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | my $status = $client->api_status; | ||||||||||||||||||||||||||||||||||||||
| 75 | my $status = $client->api_status( URLorPath ); | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | Gets API stat data | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | =head2 api_token | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | my $status = $client->api_token; | ||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||
| 83 | Gets API token | ||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | =head2 authorize | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | my $status = $client->authorize($username, $password, { | ||||||||||||||||||||||||||||||||||||||
| 88 | encrypted => \0, | ||||||||||||||||||||||||||||||||||||||
| 89 | foo => \1, | ||||||||||||||||||||||||||||||||||||||
| 90 | }); | ||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | Performs authorization on the server and returns access token. | ||||||||||||||||||||||||||||||||||||||
| 93 | This is private method! | ||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||
| 95 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | L<Mojolicious>, L<WWW::Suffit> | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | =head1 TO DO | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | See C<TODO> file | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | L<Mojo::UserAgent>, L<WWW::Suffit::UserAgent> | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | Copyright (C) 1998-2026 D&D Corporation | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | This program is distributed under the terms of the Artistic License Version 2.0 | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details | ||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | 4 | our $VERSION = '1.04'; | |||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||
| 125 | 4 | ▶ | use parent qw/ WWW::Suffit::UserAgent /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||
| 127 | 4 | ▶ | use WWW::Suffit::Const qw/ :MIME /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | ## SUFFIT API COMMON METHODS | ||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub apierr { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
apierr: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | return $self->res->json("/message") || $self->error || $self->res->message | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->res->json('/message') || $self->error || $self->res->message
$self->res->json("/message") or $self->error
MC/DC0%0/3
$self->res->json('/message') || $self->error || $self->res->message
$self->res->json("/message")
$self->error
$self->res->message
Truth table0%0/4
A or B || C
A= $self->res->json("/message")B= $self->errorC= $self->res->message
| |||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub apicode { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
apicode: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | return $self->res->json("/code") || ($self->res->json("/status") | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$self->res->json('/code') || ($self->res->json('/status') ? 'E0000' : sprintf('E0%s', $self->code || '200'))
$self->code || '200'
MC/DC0%0/2
$self->res->json('/code') || ($self->res->json('/status') ? 'E0000' : sprintf('E0%s', $self->code || '200'))
$self->res->json('/code')
($self->res->json('/status') ? 'E0000' : sprintf('E0%s', $self->code || '200'))
MC/DC0%0/1
$self->code || '200'
$self->code
Truth table0%0/3
A || B
A= $self->res->json('/code')B= ($self->res->json('/status') ? 'E0000' : sprintf('E0%s', $self->code || '200'))
Truth table0%0/2
A
A= $self->code
| |||||||||||||||||||||||||||||||||||||||
| 139 | ? 'E0000' | ||||||||||||||||||||||||||||||||||||||
| 140 | : sprintf("E0%s", $self->code || '200')); | ||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub api_check { # /api/check | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api_check: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 144 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $url = shift || 'check'; # URL or String (e.g.: api/check) | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 'check'
MC/DC0%0/1
shift() || 'check'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $status = $self->request(GET => $self->str2url($url), | |||||||||||||||||||||||||||||||||||||
| 147 | { # Headers | ||||||||||||||||||||||||||||||||||||||
| 148 | Accept => CONTENT_TYPE_JSON, # "*/*" | ||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | return 0 unless $status; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||
| 153 | # Check API status | ||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | return 0 unless $self->res->json("/status"); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||
| 156 | # Check code | ||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my $error_code = $self->res->json("/code") || 'E0000'; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->res->json('/code') || 'E0000'
MC/DC0%0/1
$self->res->json('/code') || 'E0000'
$self->res->json('/code')
Truth table0%0/2
A
A= $self->res->json('/code')
| |||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | return 0 unless $error_code eq 'E0000'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 0 | return $status; | |||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub api_status { # /api/status | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api_status: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 163 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my $url = shift || 'status'; # URL or String (e.g.: api/status) | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 'status'
MC/DC0%0/1
shift() || 'status'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 165 | 0 | return $self->request(GET => $self->str2url($url), | |||||||||||||||||||||||||||||||||||||
| 166 | { # Headers | ||||||||||||||||||||||||||||||||||||||
| 167 | Accept => CONTENT_TYPE_JSON, # "*/*" | ||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub api_data { # /api | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api_data: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my $url = shift || ''; # URL or String (e.g.: api) | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 174 | 0 | return $self->request(GET => $self->str2url($url), | |||||||||||||||||||||||||||||||||||||
| 175 | { # Headers | ||||||||||||||||||||||||||||||||||||||
| 176 | Accept => CONTENT_TYPE_JSON, # "*/*" | ||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub api_token { # /api/user/token | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api_token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 182 | 0 | return $self->request(POST => $self->str2url("user/token"), # e.g.: api/user/token | |||||||||||||||||||||||||||||||||||||
| 183 | { # Headers | ||||||||||||||||||||||||||||||||||||||
| 184 | Accept => CONTENT_TYPE_JSON, # "*/*" | ||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||
| 186 | ); | ||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub authorize { # System authorization, NO USER PUBLIC method! | ||||||||||||||||||||||||||||||||||||
|
Subroutine
authorize: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $username = shift; | |||||||||||||||||||||||||||||||||||||
| 191 | 0 | my $password = shift; | |||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | my $options = shift || {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | $options = {} unless ref $options eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | $options->{username} = $username if defined $username; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | $options->{password} = $password if defined $password; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | 0 | return $self->request(POST => $self->str2url("authorize"), | |||||||||||||||||||||||||||||||||||||
| 198 | { # Headers | ||||||||||||||||||||||||||||||||||||||
| 199 | Accept => CONTENT_TYPE_JSON, # "*/*" | ||||||||||||||||||||||||||||||||||||||
| 200 | }, | ||||||||||||||||||||||||||||||||||||||
| 201 | json => $options, | ||||||||||||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||
| 205 | 4 | 1; | |||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||||||||||||||||||||