File Coverage

blib/lib/WWW/Suffit/Client/NoAPI.pm
Criterion Covered Total %
statement 24 51 47.0
branch 0 4 0.0
condition n/a
subroutine 8 12 66.6
pod 4 4 100.0
total 36 71 50.7


line stmt bran cond sub pod time code
1             package WWW::Suffit::Client::NoAPI;
2 1     1   131516 use warnings;
  1         2  
  1         48  
3 1     1   4 use strict;
  1         1  
  1         23  
4 1     1   343 use utf8;
  1         194  
  1         4  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             WWW::Suffit::Client::NoAPI - The Suffit API client library for NoAPI methods
11              
12             =head1 SYNOPSIS
13              
14             use WWW::Suffit::Client::NoAPI;
15              
16             =head1 DESCRIPTION
17              
18             This library provides NoAPI methods for access to Suffit API servers
19              
20             =head1 NOAPI METHODS
21              
22             List of predefined the Suffit NoAPI methods
23              
24             =head2 download
25              
26             my $status = $client->download("file.txt", "/tmp/file.txt");
27              
28             Request for download an file from the server by file path.
29             The method returns status of operation: 0 - Error; 1 - Ok
30              
31             =head2 manifest
32              
33             my $status = $client->manifest;
34              
35             Gets list of files (manifest) from server
36             The method returns status of operation: 0 - Error; 1 - Ok
37              
38             =head2 remove
39              
40             my $status = $client->remove("/foo/bar/file.txt");
41              
42             Request for deleting the file from server.
43             The method returns status of operation: 0 - Error; 1 - Ok
44              
45             =head2 upload
46              
47             my $status = $clinet->upload("/tmp/file.txt", "/foo/bar/file.txt");
48              
49             Upload an file to the server by file path
50              
51             =head1 DEPENDENCIES
52              
53             L, L
54              
55             =head1 TO DO
56              
57             See C file
58              
59             =head1 SEE ALSO
60              
61             L, L
62              
63             =head1 AUTHOR
64              
65             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
66              
67             =head1 COPYRIGHT
68              
69             Copyright (C) 1998-2026 D&D Corporation
70              
71             =head1 LICENSE
72              
73             This program is distributed under the terms of the Artistic License Version 2.0
74              
75             See the C file or L for details
76              
77             =cut
78              
79 1     1   370 use parent qw/ WWW::Suffit::Client /;
  1         214  
  1         6  
80              
81 1     1   81 use Mojo::Asset::File;
  1         15  
  1         15  
82 1     1   47 use Mojo::File qw/path/;
  1         2  
  1         67  
83              
84 1     1   7 use WWW::Suffit::Const qw/ :MIME /;
  1         2  
  1         144  
85 1     1   653 use WWW::Suffit::Util qw/ md5sum /;
  1         3754  
  1         631  
86              
87             ## SUFFIT NoAPI METHODS
88              
89             sub manifest {
90 0     0 1   my $self = shift;
91 0           return $self->request(GET => $self->str2url("file"), # e.g.: api/file
92             { # Headers
93             Accept => CONTENT_TYPE_JSON, # "*/*"
94             }
95             );
96             }
97             sub download {
98 0     0 1   my $self = shift;
99 0           my $rfile = shift; # Remote file: t.txt
100 0           my $lfile = shift; # Local file (full file path to save)
101              
102             # Remote file
103 0           $rfile =~ s/^\/+//;
104 0           my $status = $self->request(GET => $self->str2url(sprintf("file/%s", $rfile)));
105 0 0         return $status unless $status;
106              
107             # Local file
108 0           my $filepath = path($lfile);
109 0           my $filename = $filepath->basename;
110 0           $self->res->save_to($lfile);
111 0 0         return 1 if $filepath->stat->size;
112 0           $self->error("Can't download file $filename");
113 0           $self->status(0);
114 0           return 0;
115             }
116             sub upload {
117 0     0 1   my $self = shift;
118 0           my $lfile = shift; # Local file (full file path to save)
119 0           my $rfile = shift; # Remote file: t.txt
120              
121             # Local file
122 0           my $filepath = path($lfile);
123 0           my $filename = $filepath->basename;
124 0           my $asset_file = Mojo::Asset::File->new(path => $filepath);
125              
126             # Remote file
127 0           $rfile =~ s/^\/+//;
128              
129             # Request
130 0           return $self->request(PUT => $self->str2url(sprintf("file/%s", $rfile)) =>
131             { # Headers
132             'Content-Type' => 'multipart/form-data',
133             },
134             form => {
135             size => $asset_file->size,
136             md5 => md5sum($asset_file->path),
137             fileraw => {
138             file => $asset_file,
139             filename => $filename,
140             'Content-Type' => 'application/octet-stream',
141             },
142             },
143             );
144             }
145             sub remove {
146 0     0 1   my $self = shift;
147 0           my $rfile = shift;
148 0           $rfile =~ s/^\/+//;
149 0           return $self->request(DELETE => $self->str2url(sprintf("file/%s", $rfile)));
150             }
151              
152             1;
153              
154             __END__