blib/lib/Net/Ethereum/Swarm.pm

filter: Statementstmt 41.5% 22 / 53 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 7 / 14 Podpod 100% 2 / 2 totaltotal 44.9% 31 / 69 CC 1 SCAR 4.1
CC
1
Cov
42%
new
2.0
_swarp_node_get_manifest
2.0
_swarp_node_get_file
2.0
CRAP 1.5
↑ Summary
1 package Net::Ethereum::Swarm;
2  
3 1 ▶use 5.020002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 our $VERSION = '0.04';
14  
15  
16  
17 =pod
18  
19 =encoding utf8
20  
21 =head1 NAME
22  
23 Net::Ethereum::Swarm - Perl Framework for a distributed storage platform and content distribution service Ethereum Swarm.
24  
25 =head1 SYNOPSIS
26  
27  
28 # Upload text file to Ethereum Swarm
29  
30 use Net::Ethereum::Swarm;
31 my $uploaded_file_path = $ARGV[0];
32 my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/');
33 my $rc = $sw_node->_swarp_node_upload_text_file($uploaded_file_path, 'plain/text; charset=UTF-8');
34 print Dumper($rc), "\n";
35  
36  
37 # Upload binary file to Ethereum Swarm
38  
39 use Net::Ethereum::Swarm;
40 my $uploaded_file_path = $ARGV[0];
41 my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/');
42 my $rc = $sw_node->_swarp_node_upload_binary_file($uploaded_file_path, 'image/jpeg');
43 print Dumper($rc), "\n";
44  
45  
46 # Get manifest by manifest id
47  
48 use Net::Ethereum::Swarm;
49 my $manifest_id = $ARGV[0];
50 my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/');
51 my $rc = $sw_node->_swarp_node_get_manifest($manifest_id);
52 print Dumper($rc), "\n";
53  
54  
55 # Get file from Ethereum Swarm
56  
57 use Net::Ethereum::Swarm;
58 my $manifest_id = $ARGV[0];
59 my $file_path_to_save = $ARGV[1];
60 my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/');
61 my $rc = $sw_node->_swarp_node_get_file($manifest_id, $file_path_to_save, 'plain/text; charset=UTF-8');
62 print Dumper($rc), "\n";
63  
64  
65  
66 =head1 DESCRIPTION
67  
68 Net::Ethereum::Swarm - Perl Framework for a distributed storage platform and content distribution service Ethereum Swarm.
69  
70 =head1 FUNCTIONS
71  
72  
73  
74 =head2 new()
75  
76 my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/');
77  
78 =cut
79  
80 sub new
81 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
82 0 my ($this, $swarm_api_url) = @_;
83 0 my $self = {};
84 0 bless( $self, $this );
85  
86 0 $self->{api_url} = $swarm_api_url;
87 0 $self->{debug} = 0;
88  
89 0 return $self;
90 }
91  
92 =pod
93  
94 =head2 _swarp_node_get_manifest
95  
96 Get manifest by manifest id
97 my $rc = $sw_node->_swarp_node_get_manifest($manifest_id);
98  
99 =cut
100  
101 sub _swarp_node_get_manifest()
102 0 ▶{
Subroutine _swarp_node_get_manifest: not called CC=1 SCAR=6.9
103 0 my ($this, $manifest_id) = @_;
104 0 my $header = ['Content-Type' => 'plain/text'];
105 0 my $rc = $this->_swarp_node_request('GET', 'bzz-list:/'.$manifest_id.'/', $header, '');
106 0 my $manifest = JSON::decode_json($rc);
107 0 return $manifest;
108 }
109  
110  
111 =pod
112  
113 =head2 _swarp_node_get_file
114  
115 Get file from Ethereum Swarm
116 my $rc = $sw_node->_swarp_node_get_file($manifest_id, $file_path_to_save, 'plain/text; charset=UTF-8');
117  
118 =cut
119  
120 sub _swarp_node_get_file()
121 0 ▶{
Subroutine _swarp_node_get_file: not called CC=1 SCAR=6.9
122 0 my ($this, $manifest_id, $content_type, $path) = @_;
123 0 my $header = ['Content-Type' => $content_type];
124 0 my $file_content = $this->_swarp_node_request('GET', 'bzz:/'.$manifest_id.'/'.$path, $header, '');
125 0 return $file_content;
126 }
127  
128  
129 =pod
130  
131 =head2 _swarp_node_upload_text_file
132  
133 Upload text file to Ethereum Swarm
134  
135 my $rc = $sw_node->_swarp_node_upload_text_file($uploaded_file_path, 'plain/text; charset=UTF-8');
136  
137 =cut
138  
139  
140 sub _swarp_node_upload_text_file()
141 0 ▶{
Subroutine _swarp_node_upload_text_file: not called CC=1 SCAR=6.9
142 0 my ($this, $uploaded_file_path, $content_type) = @_;
143  
144 0 my $file_content = read_file( $uploaded_file_path, scalar_ref => 1);
145 # my $header = ['Content-Type' => 'plain/text; charset=UTF-8'];
146 0 my $header = ['Content-Type' => $content_type];
147 0 my $ua_rc = $this->_swarp_node_request('POST', 'bzz:/', $header, $file_content);
148 0 return $ua_rc;
149 }
150  
151  
152 =pod
153  
154 =head2 _swarp_node_upload_binary_file
155  
156 Upload binary file to Ethereum Swarm
157  
158 my $rc = $sw_node->_swarp_node_upload_binary_file($uploaded_file_path, 'image/jpeg');
159  
160 =cut
161  
162  
163 sub _swarp_node_upload_binary_file()
164 0 ▶{
Subroutine _swarp_node_upload_binary_file: not called CC=1 SCAR=6.9
165 0 my ($this, $uploaded_file_path, $content_type) = @_;
166  
167 0 my $file_content = read_file( $uploaded_file_path , binmode => ':raw' , scalar_ref => 1 );
168 # my $header = ['Content-Type' => 'image/jpeg'];
169 0 my $header = ['Content-Type' => $content_type];
170 0 my $ua_rc = $this->_swarp_node_request('POST', 'bzz:/', $header, $file_content);
171 0 return $ua_rc;
172 }
173  
174  
175 =pod
176  
177 =head2 _swarp_node_request
178  
179 Internal method.
180 Send request to Ethereum Swarm
181  
182 my $ua_rc = $this->_swarp_node_request('POST', 'bzz:/', $header, $file_content);
183  
184 =cut
185  
186  
187 sub _swarp_node_request()
188 0 ▶{
Subroutine _swarp_node_request: not called CC=1 SCAR=6.9
189 0 my ($this, $rq_type, $bzz_protocol, $header, $content) = @_;
190 0 my $req = HTTP::Request->new($rq_type, $this->{api_url}.$bzz_protocol, $header, $content);
191 0 my $ua = LWP::UserAgent->new;
192 0 return $ua->request($req)->{ _content };
193 }
194  
195  
196  
197 =pod
198  
199 =head2 set_debug_mode
200  
201 Set dubug mode. Debug info printed to console.
202 $node->set_debug_mode($mode);
203  
204 $mode: 1 - debug on, 0 - debug off.
205  
206 =cut
207  
208 sub set_debug_mode()
209 0 ▶{
Subroutine set_debug_mode: not called CC=1 SCAR=6.9
Pod documented
210 0 my ($this, $debug_mode) = @_;
211 0 $this->{debug} = $debug_mode;
212 }
213  
214  
215  
216 1 1;
217 __END__
↑ Summary