Eulerian/EDW/Request.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 137 CC 11 SCAR 26.9
CC
11
Cov
0%
_request
42.0
json
20.0
is_json
12.0
CRAP 14.7
1 #/usr/bin/env perl
2 ###############################################################################
3 #
4 # @file Request.pm
5 #
6 # @brief API::Eulerian::EDW Request module used to send HTTP request to remote Peer.
7 #
8 # @author Thorillon Xavier:x.thorillon@eulerian.com
9 #
10 # @date 26/11/2021
11 #
12 # @version 1.0
13 #
14 ###############################################################################
15 #
16 # Setup module name.
17 #
18 package API::Eulerian::EDW::Request;
19 #
20 # Enforce compilor rules
21 #
22 use strict; use warnings;
23 #
24 # Import API::Eulerian::EDW::Status
25 #
26 use API::Eulerian::EDW::Status;
27 #
28 # Import HTTP::Headers
29 #
30 use HTTP::Headers;
31 #
32 # Import HTTP::Request
33 #
34 use HTTP::Request;
35 #
36 # Import LWP::UserAgent
37 #
38 use LWP::UserAgent;
39 #
40 # Import IO::Socket::SSL
41 #
42 use IO::Socket::SSL;
43 #
44 # Import HTTP::Status
45 #
46 use HTTP::Status qw( :constants :is status_message );
47 #
48 # Import JSON
49 #
50 use JSON;
51 #
52 # Import Encode
53 #
54 use Encode;
55 #
56 # @brief Create new HTTP Headers.
57 #
58 # @param $class - API::Eulerian::EDW::HTTP class.
59 #
60 # @return HTTP Headers.
61 #
62 sub headers
63 {
64 return HTTP::Headers->new();
65 }
66 #
67 # @brief Test if the content type of given HTTP response is a
68 # JSON format.
69 #
70 # @param $class - API::Eulerian::EDW::Request Class.
71 # @param $response - HTTP response.
72 #
73 # @return 1 - Content type is JSON.
74 # @return 0 - Content type isnt JSON.
75 #
76 sub is_json
77 {
78 my ( $class, $response ) = @_;
79 my $type;
80  
81 # Get content type value from HTTP response
82 $type = $response->header( 'content-type' );
83 if( defined( $type ) ) {
84 # Split content type into an array.
85 my @subtypes = split( '; ', $type );
86 # Iterate on subtypes entries
87 foreach my $subtype ( @subtypes ) {
88 # Test if subtype is JSON format
89 if( $subtype eq 'application/json' ) {
90 return 1;
91 }
92 }
93 }
94  
95 return 0;
96 }
97 #
98 # @brief Get JSON object from HTTP response.
99 #
100 # @param $class - API::Eulerian::EDW::Request class.
101 # @param $response - HTTP response.
102 #
103 # @return JSON object.
104 #
105 sub json
106 {
107 my ( $class, $response ) = @_;
108 my $data = undef;
109  
110 if( $class->is_json( $response ) ) {
111 $data = $response->decoded_content;
112 if( defined( $data ) ) {
113 chomp( $data );
114 if( length( $data ) > 0 ) {
115 $data = encode( 'utf-8', $data );
116 $data = decode_json( $data );
117 } else {
118 $data = undef;
119 }
120 }
121 }
122  
123 return $data;
124 }
125 #
126 # @brief Do HTTP Get on given URL.
127 #
128 # @param $class - API::Eulerian::EDW::HTTP class.
129 # @param $url - Remote URL.
130 # @param $headers - HTTP::Headers.
131 # @param $file - Local file path.
132 #
133 # @return API::Eulerian::EDW::Status instance.
134 #
135 sub get
136 {
137 my ( $class, $url, $headers, $file ) = @_;
138 return $class->_request( 'GET', $url, $headers, undef, undef, $file );
139 }
140 #
141 # @brief Do HTTP Post on given URL.
142 #
143 # @param $class - API::Eulerian::EDW::HTTP class.
144 # @param $url - Remote URL.
145 # @param $headers - HTTP::Headers.
146 # @param $what - Request Data.
147 # @param $type - Request Data Type.
148 #
149 # @return API::Eulerian::EDW::Status instance.
150 #
151 sub post
152 {
153 my ( $class, $url, $headers, $what, $type ) = @_;
154 return $class->_request( 'POST', $url, $headers, $what, $type );
155 }
156 #
157 # @brief Send HTTP request on given url.
158 #
159 # @param $class - API::Eulerian::EDW Request class.
160 # @param $method - HTTP method.
161 # @param $url - Remote URL.
162 # @param $headers - HTTP headers.
163 # @param $what - Data of POST request.
164 # @param $type - Data type of POST request
165 # @param $file - Local file path used to store HTTP reply.
166 #
167 # @return API::Eulerian::EDW::Status instance.
168 #
169 use Data::Dumper;
170 sub _request
171 {
172 my ( $class, $method, $url, $headers, $what, $type, $file ) = @_;
173 my $status = API::Eulerian::EDW::Status->new();
174 my $endpoint;
175 my $request;
176  
177 # Save url.
178 $status->url( $url );
179  
180 # Ensure default type
181 $type = $type || 'application/json';
182  
183 # Sanity check POST arguments
184 if( $method eq 'POST' ) {
185 if( ! ( defined( $what ) && defined( $type ) ) ) {
186 $status->error( 1 );
187 $status->msg( "Mandatory argument to post request is/are missing" );
188 $status->code( 400 );
189 return $status;
190 } else {
191 # Setup Content_Length and Content_Type
192 $headers->push_header( Content_Length => length( $what ) );
193 $headers->push_header( Content_Type => $type );
194 }
195 }
196  
197 # Create HTTP Request
198 $request = HTTP::Request->new( $method, $url, $headers, $what );
199  
200 # Create End Point used to communicate with remote server
201 $endpoint = LWP::UserAgent->new(
202 keep_alive => 0,
203 cookie_jar => {},
204 ssl_opts => {
205 SSL_verifycn_publicsuffix => '',
206 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
207 verify_hostname => 0,
208 SSL_hostname => '',
209 },
210 );
211
212 # Increase Read Timeout on TCP socket to avoid being disconnected
213 $endpoint->timeout( 1800 );
214  
215 # Send Request, wait response if file is defined reply content is
216 # writen into local file.
217 my $response = $endpoint->request( $request, $file );
218 my $json = API::Eulerian::EDW::Request->json( $response );
219
220 $status->{ response } = $response;
221  
222 if( $response->code != HTTP_OK ) {
223 $status->error( 1 );
224 $status->code( $response->code );
225 $status->msg(
226 defined( $json ) ?
227 encode_json( $json ) : $response->content()
228 );
229 } else {
230 if( defined( $response->header( 'content-encoding' ) ) ) {
231 $status->{ encoding } = $response->header( 'content-encoding' );
232 }
233 }
234  
235 return $status;
236 }
237 #
238 # End up module properly
239 #
240 1;
241  
242 __END__