File Coverage

blib/lib/WWW/Suffit/Client.pm
Criterion Covered Total %
statement 15 43 34.8
branch 0 12 0.0
condition 0 16 0.0
subroutine 5 12 41.6
pod 7 7 100.0
total 27 90 30.0


line stmt bran cond sub pod time code
1             package WWW::Suffit::Client;
2 4     4   329951 use warnings;
  4         8  
  4         252  
3 4     4   29 use strict;
  4         16  
  4         139  
4 4     4   1236 use utf8;
  4         573  
  4         21  
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, L
98              
99             =head1 TO DO
100              
101             See C file
102              
103             =head1 SEE ALSO
104              
105             L, L
106              
107             =head1 AUTHOR
108              
109             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
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 file or L for details
120              
121             =cut
122              
123             our $VERSION = '1.04';
124              
125 4     4   1128 use parent qw/ WWW::Suffit::UserAgent /;
  4         673  
  4         52  
126              
127 4     4   2536310 use WWW::Suffit::Const qw/ :MIME /;
  4         14  
  4         3578  
128              
129             ## SUFFIT API COMMON METHODS
130              
131             sub apierr {
132 0     0 1   my $self = shift;
133 0   0       return $self->res->json("/message") || $self->error || $self->res->message
134             }
135              
136             sub apicode {
137 0     0 1   my $self = shift;
138 0   0       return $self->res->json("/code") || ($self->res->json("/status")
139             ? 'E0000'
140             : sprintf("E0%s", $self->code || '200'));
141             }
142              
143             sub api_check { # /api/check
144 0     0 1   my $self = shift;
145 0   0       my $url = shift || 'check'; # URL or String (e.g.: api/check)
146 0           my $status = $self->request(GET => $self->str2url($url),
147             { # Headers
148             Accept => CONTENT_TYPE_JSON, # "*/*"
149             }
150             );
151 0 0         return 0 unless $status;
152              
153             # Check API status
154 0 0         return 0 unless $self->res->json("/status");
155              
156             # Check code
157 0   0       my $error_code = $self->res->json("/code") || 'E0000';
158 0 0         return 0 unless $error_code eq 'E0000';
159              
160 0           return $status;
161             }
162             sub api_status { # /api/status
163 0     0 1   my $self = shift;
164 0   0       my $url = shift || 'status'; # URL or String (e.g.: api/status)
165 0           return $self->request(GET => $self->str2url($url),
166             { # Headers
167             Accept => CONTENT_TYPE_JSON, # "*/*"
168             }
169             );
170             }
171             sub api_data { # /api
172 0     0 1   my $self = shift;
173 0   0       my $url = shift || ''; # URL or String (e.g.: api)
174 0           return $self->request(GET => $self->str2url($url),
175             { # Headers
176             Accept => CONTENT_TYPE_JSON, # "*/*"
177             }
178             );
179             }
180             sub api_token { # /api/user/token
181 0     0 1   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             sub authorize { # System authorization, NO USER PUBLIC method!
189 0     0 1   my $self = shift;
190 0           my $username = shift;
191 0           my $password = shift;
192 0   0       my $options = shift || {};
193 0 0         $options = {} unless ref $options eq 'HASH';
194 0 0         $options->{username} = $username if defined $username;
195 0 0         $options->{password} = $password if defined $password;
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             1;
206              
207             __END__