File Coverage

blib/lib/Mslm/Common.pm
Criterion Covered Total %
statement 24 67 35.8
branch 0 12 0.0
condition 0 20 0.0
subroutine 8 15 53.3
pod 7 7 100.0
total 39 121 32.2


line stmt bran cond sub pod time code
1             package Mslm::Common;
2              
3 1     1   7 use strict;
  1         2  
  1         42  
4 1     1   8 use warnings;
  1         3  
  1         67  
5 1     1   959 use LWP::UserAgent;
  1         81125  
  1         47  
6 1     1   1129 use JSON;
  1         14128  
  1         11  
7 1     1   192 use URI;
  1         2  
  1         45  
8 1     1   6 use Exporter qw(import);
  1         1  
  1         114  
9             our @EXPORT_OK = qw($default_base_url $default_user_agent $default_api_key DEFAULT_TIMEOUT);
10              
11 1     1   7 use constant HTTP_TOO_MANY_REQUEST => 429;
  1         2  
  1         84  
12 1     1   7 use constant DEFAULT_TIMEOUT => 120;
  1         2  
  1         768  
13             our $default_base_url = 'https://mslm.io';
14             our $default_user_agent = 'mslm/perl/1.0';
15             our $default_api_key = '';
16              
17             sub new {
18 0     0 1   my ( $class, %opts ) = @_;
19             my $self = {
20             http_client => $opts{http_client}
21             || "", # HTTP client used for making requests.
22             base_url => $opts{base_url}
23             || "", # Base URL for API requests.
24             user_agent => $opts{user_agent}
25             || "", # User-agent used when communicating with the API.
26             api_key => $opts{api_key}
27 0   0       || "" # The API key used for authentication & authorization.
      0        
      0        
      0        
28             };
29              
30 0           bless $self, $class;
31 0           return $self;
32             }
33              
34             sub set_base_url {
35 0     0 1   my ( $self, $base_url_str ) = @_;
36 0           my $base_url = URI->new($base_url_str);
37 0           $self->{base_url} = $base_url;
38             }
39              
40             sub set_http_client {
41 0     0 1   my ( $self, $http_client ) = @_;
42 0           $self->{http_client} = $http_client;
43             }
44              
45             sub set_user_agent {
46 0     0 1   my ( $self, $user_agent ) = @_;
47 0           $self->{user_agent} = $user_agent;
48             }
49              
50             sub set_api_key {
51 0     0 1   my ( $self, $api_key ) = @_;
52 0           $self->{api_key} = $api_key;
53             }
54              
55             sub prepare_url {
56 0     0 1   my ( $self, $urlPath, $qp, %opts ) = @_;
57 0   0       my $reqUrl = $opts{base_url} || $self->{base_url};
58 0   0       my $api_key = $opts{api_key} || $self->{api_key};
59 0           $reqUrl->path($urlPath);
60              
61 0           my $reqUrlQp = {};
62 0           foreach my $key ( keys %$qp ) {
63 0           $reqUrlQp->{$key} = $qp->{$key};
64             }
65 0           $reqUrlQp->{'apikey'} = $api_key;
66 0 0         if ( $opts{disable_url_encoding} ) {
67 0           my $query_string = '';
68 0           foreach my $key ( keys %$reqUrlQp ) {
69 0           my $value = $reqUrlQp->{$key};
70 0 0 0       $query_string .= '&' if $query_string && $value;
71 0 0         $query_string .= "$key=$value" if $value;
72             }
73 0           $reqUrl->query($query_string);
74             }
75             else {
76 0           $reqUrl->query_form($reqUrlQp);
77             }
78              
79 0           return $reqUrl;
80             }
81              
82             sub req_and_resp {
83 0     0 1   my ( $self, $http_client, $method, $reqUrl, $data, %opts ) = @_;
84              
85 0   0       my $user_agent = $opts{user_agent} || $self->{user_agent};
86 0           my $req = HTTP::Request->new( $method, $reqUrl );
87 0 0         $req->content($data) if defined $data;
88 0           my $http_c = $http_client;
89 0           $http_c->agent($user_agent);
90              
91 0           my $response = $http_c->request($req);
92 0 0         if ( $response->is_success ) {
93 0           my $decoded_json = decode_json( $response->decoded_content );
94 0           return ( $decoded_json, '' );
95             }
96 0 0         if ( $response->code == HTTP_TOO_MANY_REQUEST ) {
97 0           return ( undef, 'Your API request limit has reached.' );
98             }
99              
100 0           return ( undef, $response->status_line );
101             }
102              
103             1;
104             =pod
105              
106             =head1 NAME
107              
108             Mslm::Common - Perl module containing common functions for API interactions
109              
110             =head1 SYNOPSIS
111              
112             use Mslm::Common;
113              
114             # Create a new instance
115             my $common = Mslm::Common->new(
116             http_client => $http_client,
117             base_url => $base_url,
118             user_agent => $user_agent,
119             api_key => $api_key
120             );
121              
122             # Set base URL
123             $common->set_base_url('https://example.com');
124              
125             # Set HTTP client
126             $common->set_http_client($custom_LWP_UserAgent);
127              
128             # Set user agent
129             $common->set_user_agent('my-custom-agent/1.0');
130              
131             # Set API key
132             $common->set_api_key('my-api-key');
133              
134             =head1 DESCRIPTION
135              
136             The Mslm::Common module contains common functions for handling API interactions such as setting base URL, HTTP client, user agent, and API key.
137              
138             =head1 METHODS
139              
140             =head2 new
141              
142             Creates a new instance of Mslm::Common.
143              
144             =head3 Arguments
145              
146             =over 4
147              
148             =item * C<%opts> (hash) - Optional parameters. You can pass in the following opts: C, C, C, and C. These settings can also be done via the setter functions named: C, C, C, C.
149              
150             =back
151              
152             =head2 set_base_url
153              
154             Sets the base URL for API requests.
155              
156             =head3 Arguments
157              
158             =over 4
159              
160             =item * C<$base_url_str> (string) - The base URL to be set for API requests.
161              
162             =back
163              
164             =head2 set_http_client
165              
166             Sets the HTTP client for making requests.
167              
168             =head3 Arguments
169              
170             =over 4
171              
172             =item * C<$http_client> (LWP::UserAgent) - The HTTP client to be set.
173              
174             =back
175              
176             =head2 set_user_agent
177              
178             Sets the user agent for API requests.
179              
180             =head3 Arguments
181              
182             =over 4
183              
184             =item * C<$user_agent> (string) - The user agent string to be set.
185              
186             =back
187              
188             =head2 set_api_key
189              
190             Sets the API key for authentication.
191              
192             =head3 Arguments
193              
194             =over 4
195              
196             =item * C<$api_key> (string) - The API key to be set.
197              
198             =back
199              
200             =head2 prepare_url
201              
202             Prepares the URL for making API requests.
203              
204             =head3 Arguments
205              
206             =over 4
207              
208             =item * C<$urlPath> (string) - The path for the API request.
209              
210             =item * C<$qp> (hash reference) - Query parameters for the API request.
211              
212             =item * C<%opts> (hash) - Optional parameters. You can pass in the following opts: C, C, and C. These options will only work for the current request.
213              
214             =back
215              
216             =head2 req_and_resp
217              
218             Performs the API request and fetches the response.
219              
220             =head3 Arguments
221              
222             =over 4
223              
224             =item * C<$http_client> (LWP::UserAgent) - The HTTP client to be used for the request.
225              
226             =item * C<$method> (string) - The HTTP method for the request.
227              
228             =item * C<$reqUrl> (string) - The URL for the request.
229              
230             =item * C<$data> (bytes)- Data to be sent with the request (if any). Must be a string of bytes.
231              
232             =item * C<%opts> (hash) - Optional parameters. You can pass in the opt: C.
233              
234             =back
235              
236             =head1 AUTHOR
237              
238             Mslm, C<< >>
239              
240             =head1 COPYRIGHT AND LICENSE
241              
242             Copyright (c) 2022-now mslm. All rights reserved.
243              
244             =cut
245              
246             # End of Mslm::Common