Client.pm

filter: Statementstmt 68.0% 153 / 225 Branchbran 16.6% 4 / 24 Conditioncond 6.6% 2 / 30 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 87.2% 48 / 55 Podpod 100% 6 / 6 totaltotal 60.1% 214 / 356 CC 25 SCAR 33.8
CC
25
Cov
57%
_request
156.0
call
42.0
call_raw
20.0
CRAP 29.2
↑ Summary Domain.pm »
1 package Tomba::Client;
2  
3 4 ▶use 5.026001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '1.1.1';
13  
14 # Tomba base url
15 4 ▶use constant DEFAULT_BASE_URL => 'https://api.tomba.io/v1';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Account path
18 4 ▶use constant ACCOUNT_PATH => "/me";
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # Usage path
20 4 ▶use constant USAGE_PATH => "/usage";
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # Logs path
22 4 ▶use constant LOGS_PATH => "/logs";
Subroutine BEGIN: called CC=1 SCAR=0.0
23 # Search path
24 4 ▶use constant SEARCH_PATH => "/domain-search";
Subroutine BEGIN: called CC=1 SCAR=0.0
25 # Finder path
26 4 ▶use constant FINDER_PATH => "/email-finder";
Subroutine BEGIN: called CC=1 SCAR=0.0
27 # Enrichment path
28 4 ▶use constant ENRICHMENT_PATH => "/enrich";
Subroutine BEGIN: called CC=1 SCAR=0.0
29 # Author path
30 4 ▶use constant AUTHOR_PATH => "/author-finder";
Subroutine BEGIN: called CC=1 SCAR=0.0
31 # Linkedin path
32 4 ▶use constant LINKEDIN_PATH => "/linkedin";
Subroutine BEGIN: called CC=1 SCAR=0.0
33 # Verifier path
34 4 ▶use constant VERIFIER_PATH => "/email-verifier";
Subroutine BEGIN: called CC=1 SCAR=0.0
35 # Email Sources path
36 4 ▶use constant SOURCES_PATH => "/email-sources";
Subroutine BEGIN: called CC=1 SCAR=0.0
37 # Email Count path
38 4 ▶use constant COUNT_PATH => "/email-count";
Subroutine BEGIN: called CC=1 SCAR=0.0
39 # Domain status path
40 4 ▶use constant STATUS_PATH => "/domain-status";
Subroutine BEGIN: called CC=1 SCAR=0.0
41 # Autocomplete path
42 4 ▶use constant AUTOCOMPLETE_PATH => "/domain-suggestions";
Subroutine BEGIN: called CC=1 SCAR=0.0
43 # Phone Finder path
44 4 ▶use constant PHONE_FINDER_PATH => "/phone-finder";
Subroutine BEGIN: called CC=1 SCAR=0.0
45 # Phone Validator path
46 4 ▶use constant PHONE_VALIDATOR_PATH => "/phone-validator";
Subroutine BEGIN: called CC=1 SCAR=0.0
47 # Email Format path
48 4 ▶use constant EMAIL_FORMAT_PATH => "/email-format";
Subroutine BEGIN: called CC=1 SCAR=0.0
49 # Location path
50 4 ▶use constant LOCATION_PATH => "/location";
Subroutine BEGIN: called CC=1 SCAR=0.0
51 # Similar path
52 4 ▶use constant SIMILAR_PATH => "/similar";
Subroutine BEGIN: called CC=1 SCAR=0.0
53 # Technology path
54 4 ▶use constant TECHNOLOGY_PATH => "/technology";
Subroutine BEGIN: called CC=1 SCAR=0.0
55 # Person Find (Clearbit-compatible)
56 4 ▶use constant PERSON_FIND_PATH => "/people/find";
Subroutine BEGIN: called CC=1 SCAR=0.0
57 # Company Find (Clearbit-compatible)
58 4 ▶use constant COMPANY_FIND_PATH => "/companies/find";
Subroutine BEGIN: called CC=1 SCAR=0.0
59 # Combined Find (Clearbit-compatible)
60 4 ▶use constant COMBINED_FIND_PATH => "/combined/find";
Subroutine BEGIN: called CC=1 SCAR=0.0
61 # Companies search (Reveal)
62 4 ▶use constant COMPANIES_SEARCH_PATH => "/reveal/search";
Subroutine BEGIN: called CC=1 SCAR=0.0
63 # Keys path
64 4 ▶use constant KEYS_PATH => "/keys";
Subroutine BEGIN: called CC=1 SCAR=0.0
65 # Flag path
66 4 ▶use constant FLAG_PATH => "/flag";
Subroutine BEGIN: called CC=1 SCAR=0.0
67 # Leads path
68 4 ▶use constant LEADS_PATH => "/leads";
Subroutine BEGIN: called CC=1 SCAR=0.0
69 # Leads Lists path
70 4 ▶use constant LEADS_LISTS_PATH => "/leads_lists";
Subroutine BEGIN: called CC=1 SCAR=0.0
71 # Attributes path
72 4 ▶use constant ATTRIBUTES_PATH => "/attributes";
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 # Bulk type paths
75 4 ▶use constant BULK_SEARCH_PATH => "/bulk/search";
Subroutine BEGIN: called CC=1 SCAR=0.0
76 4 ▶use constant BULK_FINDER_PATH => "/bulk/finder";
Subroutine BEGIN: called CC=1 SCAR=0.0
77 4 ▶use constant BULK_VERIFIER_PATH => "/bulk/verifier";
Subroutine BEGIN: called CC=1 SCAR=0.0
78 4 ▶use constant BULK_ENRICH_PATH => "/bulk/enrich";
Subroutine BEGIN: called CC=1 SCAR=0.0
79 4 ▶use constant BULK_AUTHOR_PATH => "/bulk/author";
Subroutine BEGIN: called CC=1 SCAR=0.0
80 4 ▶use constant BULK_LINKEDIN_PATH => "/bulk/linkedin";
Subroutine BEGIN: called CC=1 SCAR=0.0
81 4 ▶use constant BULK_COMPANY_PATH => "/bulk/company";
Subroutine BEGIN: called CC=1 SCAR=0.0
82 4 ▶use constant BULK_PHONE_FINDER_PATH => "/bulk/phone-finder";
Subroutine BEGIN: called CC=1 SCAR=0.0
83 4 ▶use constant BULK_PHONE_VALIDATOR_PATH => "/bulk/phone-validator";
Subroutine BEGIN: called CC=1 SCAR=0.0
84 4 ▶use constant BULK_SIMILAR_PATH => "/bulk/similar";
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 #-------------------------------------------------------------------------------
87  
88 =head1 NAME
89  
90 Tomba::Client - Base HTTP client for the Tomba.io API
91  
92 =head1 SYNOPSIS
93  
94 use Tomba::Client;
95  
96 my $client = Tomba::Client->new("ta_xxxxx", "ts_xxxxx");
97  
98 # GET request
99 my $result = $client->call("/me");
100  
101 # POST request
102 my $result = $client->post("/leads", { email => "test@example.com" });
103  
104 =head1 DESCRIPTION
105  
106 This module provides the base HTTP client used by all Tomba service modules.
107 It handles authentication, request construction, and JSON parsing.
108  
109 Normally you would not use this module directly; instead use
110 L<Tomba::Finder> or the individual service modules (L<Tomba::Account>,
111 L<Tomba::Domain>, etc.) which inherit from this class.
112  
113 =cut
114  
115 #-------------------------------------------------------------------------------
116 # Constructor
117 #-------------------------------------------------------------------------------
118  
119 =head2 new
120  
121 my $client = Tomba::Client->new($api_key, $api_secret);
122 my $client = Tomba::Client->new($api_key, $api_secret, $base_url);
123  
124 Creates a new Tomba::Client instance.
125  
126 See L<https://docs.tomba.io/authentication>
127  
128 =cut
129  
130 8 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
131 8 my ($class, $apiKey, $apiSecret, $baseUrl) = @_;
132  
133 8 my $self = {};
134  
135 8 ▶ $self->{baseUrl} = $baseUrl || DEFAULT_BASE_URL;
Condition100%2/2
$baseUrl || 'https://api.tomba.io/v1'
l!l
26
MC/DC100%1/1
$baseUrl || 'https://api.tomba.io/v1'
$baseUrl
Truth table100%2/2
A
A= $baseUrl
Aresult
11
00
136  
137 8 ▶ if (length($apiKey) < 39) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $apiKey < 39) { } 2 6 8
138 2 croak "Invalid Tomba api key";
139 } else {
140 6 $self->{apiKey} = $apiKey;
141 }
142  
143 6 ▶ if (length($apiSecret) < 39) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $apiSecret < 39) { } 2 4 6
144 2 croak "Invalid Tomba api secret";
145 } else {
146 4 $self->{apiSecret} = $apiSecret;
147 }
148  
149 4 bless $self, $class;
150  
151 4 return $self;
152 }
153  
154 #-------------------------------------------------------------------------------
155 # HTTP Methods
156 #-------------------------------------------------------------------------------
157  
158 =head2 call
159  
160 my $result = $client->call($path, $params);
161  
162 Makes a GET request to the Tomba API.
163  
164 Arg 1: Str $path - the path portion of the URL to request
165 Arg 2: HashRef $params - a hashref of query parameters to include in the URL
166 Returns: JSON response as a Perl hashref, or undef on error
167  
168 =cut
169  
170 0 ▶sub call {
Subroutine call: not called CC=6 SCAR=37.4
Pod documented
171 0 my ($self, $path, $params) = @_;
172  
173 0 $self->{ua} = LWP::UserAgent->new;
174 $self->{ua}->default_headers(HTTP::Headers->new(
175 'X-Tomba-Key' => $self->{apiKey},
176 'X-Tomba-Secret' => $self->{apiSecret},
177 0 'Accept' => 'application/json',
178 'Content-Type' => 'application/json',
179 ));
180 0 $self->{ua}->agent("Tomba-Finder/Perl/$VERSION");
181 0 $self->{ua}->timeout(120);
182  
183 0 my $url = $self->{baseUrl} . $path;
184  
185 0 ▶ if ($params && ref($params) eq 'HASH' && %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and ref $params eq "HASH" and %$params) 0 0 0
Condition0%0/6
$params and ref $params eq "HASH" and %$params
!ll&&!rl&&r
000
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$params and ref $params eq "HASH" and %$params
$params ref $params eq "HASH" %$params
Truth table0%0/4
A and B and C
A= $paramsB= ref $params eq "HASH"C= %$params
ABCresult
0XX0
10X0
1100
1111
186 0 $url .= '?' . join('&', map { uri_escape($_) . '=' . uri_escape($params->{$_}) } keys %$params);
187 }
188  
189 0 my $response = $self->{ua}->get($url);
190 0 my $content = $response->decoded_content;
191 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
192 0 $self->{body} = decode_json($content);
193 } else {
194 0 ▶ $self->{body} = eval { decode_json($content) } || { status => $response->code, message => $content };
Condition0%0/2
eval { do { &decode_json($content) } } || {'status', $response->code, 'message', $content}
l!l
00
MC/DC0%0/1
eval { do { &decode_json($content) } } || {'status', $response->code, 'message', $content}
eval { do { &decode_json($content) } }
Truth table0%0/2
A
A= eval { do { &decode_json($content) } }
Aresult
11
00
195 }
196  
197 0 my $rate_limit = _parse_rate_limit_headers($response);
198  
199 0 return { data => $self->{body}, rate_limit => $rate_limit };
200 }
201  
202 =head2 call_raw
203  
204 my $result = $client->call_raw($path, $params);
205  
206 Makes a GET request to the Tomba API and returns the raw response body
207 as a string without JSON decoding. Useful for endpoints that return
208 non-JSON content such as CSV downloads.
209  
210 Arg 1: Str $path - the path portion of the URL to request
211 Arg 2: HashRef $params - a hashref of query parameters to include in the URL
212 Returns: HashRef with raw response string in 'data' and rate_limit info
213  
214 =cut
215  
216 0 ▶sub call_raw {
Subroutine call_raw: not called CC=4 SCAR=30.0
Pod documented
217 0 my ($self, $path, $params) = @_;
218  
219 0 $self->{ua} = LWP::UserAgent->new;
220 $self->{ua}->default_headers(HTTP::Headers->new(
221 'X-Tomba-Key' => $self->{apiKey},
222 'X-Tomba-Secret' => $self->{apiSecret},
223 0 'Accept' => 'application/json',
224 'Content-Type' => 'application/json',
225 ));
226 0 $self->{ua}->agent("Tomba-Finder/Perl/$VERSION");
227 0 $self->{ua}->timeout(120);
228  
229 0 my $url = $self->{baseUrl} . $path;
230  
231 0 ▶ if ($params && ref($params) eq 'HASH' && %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and ref $params eq "HASH" and %$params) 0 0 0
Condition0%0/6
$params and ref $params eq "HASH" and %$params
!ll&&!rl&&r
000
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$params and ref $params eq "HASH" and %$params
$params ref $params eq "HASH" %$params
Truth table0%0/4
A and B and C
A= $paramsB= ref $params eq "HASH"C= %$params
ABCresult
0XX0
10X0
1100
1111
232 0 $url .= '?' . join('&', map { uri_escape($_) . '=' . uri_escape($params->{$_}) } keys %$params);
233 }
234  
235 0 my $response = $self->{ua}->get($url);
236  
237 0 my $rate_limit = _parse_rate_limit_headers($response);
238  
239 0 return { data => $response->decoded_content, rate_limit => $rate_limit };
240 }
241  
242 =head2 post
243  
244 my $result = $client->post($path, $body, $params);
245  
246 Makes a POST request to the Tomba API with a JSON body.
247  
248 Arg 1: Str $path - the path portion of the URL
249 Arg 2: HashRef $body - a hashref to be encoded as JSON in the request body
250 Arg 3: HashRef $params (optional) - query parameters
251 Returns: JSON response as a Perl hashref, or undef on error
252  
253 =cut
254  
255 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
256 0 my ($self, $path, $body, $params) = @_;
257 0 return $self->_request('POST', $path, $body, $params);
258 }
259  
260 =head2 put
261  
262 my $result = $client->put($path, $body, $params);
263  
264 Makes a PUT request to the Tomba API with a JSON body.
265  
266 Arg 1: Str $path - the path portion of the URL
267 Arg 2: HashRef $body - a hashref to be encoded as JSON in the request body
268 Arg 3: HashRef $params (optional) - query parameters
269 Returns: JSON response as a Perl hashref, or undef on error
270  
271 =cut
272  
273 0 ▶sub put {
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
274 0 my ($self, $path, $body, $params) = @_;
275 0 return $self->_request('PUT', $path, $body, $params);
276 }
277  
278 =head2 delete_request
279  
280 my $result = $client->delete_request($path, $params);
281  
282 Makes a DELETE request to the Tomba API.
283  
284 Arg 1: Str $path - the path portion of the URL
285 Arg 2: HashRef $params (optional) - query parameters
286 Returns: JSON response as a Perl hashref, or undef on error
287  
288 =cut
289  
290 0 ▶sub delete_request {
Subroutine delete_request: not called CC=1 SCAR=6.9
Pod documented
291 0 my ($self, $path, $params) = @_;
292 0 return $self->_request('DELETE', $path, undef, $params);
293 }
294  
295 # Internal method for POST/PUT/DELETE requests
296 0 ▶sub _request {
Subroutine _request: not called CC=12 SCAR=50.5
297 0 my ($self, $method, $path, $body, $params) = @_;
298  
299 0 $self->{ua} = LWP::UserAgent->new;
300 $self->{ua}->default_headers(HTTP::Headers->new(
301 'X-Tomba-Key' => $self->{apiKey},
302 'X-Tomba-Secret' => $self->{apiSecret},
303 0 'Accept' => 'application/json',
304 'Content-Type' => 'application/json',
305 ));
306 0 $self->{ua}->agent("Tomba-Finder/Perl/$VERSION");
307 0 $self->{ua}->timeout(120);
308  
309 0 my $url = $self->{baseUrl} . $path;
310  
311 0 ▶ if ($params && ref($params) eq 'HASH' && %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and ref $params eq "HASH" and %$params) 0 0 0
Condition0%0/6
$params and ref $params eq "HASH" and %$params
!ll&&!rl&&r
000
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$params and ref $params eq "HASH" and %$params
$params ref $params eq "HASH" %$params
Truth table0%0/4
A and B and C
A= $paramsB= ref $params eq "HASH"C= %$params
ABCresult
0XX0
10X0
1100
1111
312 0 $url .= '?' . join('&', map { uri_escape($_) . '=' . uri_escape($params->{$_}) } keys %$params);
313 }
314  
315 0 my $json_body = '';
316 0 ▶ if ($body && ref($body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($body and ref $body eq "HASH") 0 0 0
Condition0%0/3
$body and ref $body eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$body and ref $body eq "HASH"
$body ref $body eq "HASH"
Truth table0%0/3
A and B
A= $bodyB= ref $body eq "HASH"
ABresult
0X0
100
111
317 0 $json_body = encode_json($body);
318 }
319  
320 0 my $response;
321 0 ▶ if ($method eq 'POST') {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'POST') { } 0 0 0
elsif ($method eq 'PUT') { } 0 0 0
elsif ($method eq 'DELETE') { } 0 0 0
322 0 $response = $self->{ua}->post($url, Content => $json_body);
323 } elsif ($method eq 'PUT') {
324 0 $response = $self->{ua}->put($url, Content => $json_body);
325 } elsif ($method eq 'DELETE') {
326 0 my $req = HTTP::Request->new(DELETE => $url);
327 0 $req->header('X-Tomba-Key' => $self->{apiKey});
328 0 $req->header('X-Tomba-Secret' => $self->{apiSecret});
329 0 $req->header('Accept' => 'application/json');
330 0 $req->header('Content-Type' => 'application/json');
331 0 $response = $self->{ua}->request($req);
332 }
333  
334 0 my $content = $response->decoded_content;
335 0 ▶ if ($content && length($content) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content and length $content > 0) { } 0 0 0
Condition0%0/3
$content and length $content > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$content and length $content > 0
$content length $content > 0
Truth table0%0/3
A and B
A= $contentB= length $content > 0
ABresult
0X0
100
111
336 0 ▶ $self->{body} = eval { decode_json($content) } || { status => $response->code, message => $content };
Condition0%0/2
eval { do { &decode_json($content) } } || {'status', $response->code, 'message', $content}
l!l
00
MC/DC0%0/1
eval { do { &decode_json($content) } } || {'status', $response->code, 'message', $content}
eval { do { &decode_json($content) } }
Truth table0%0/2
A
A= eval { do { &decode_json($content) } }
Aresult
11
00
337 } else {
338 0 $self->{body} = { status => $response->code };
339 }
340  
341 0 my $rate_limit = _parse_rate_limit_headers($response);
342  
343 0 return { data => $self->{body}, rate_limit => $rate_limit };
344 }
345  
346 # Internal: extract rate-limit headers from an HTTP::Response
347 0 ▶sub _parse_rate_limit_headers {
Subroutine _parse_rate_limit_headers: not called CC=3 SCAR=24.8
348 0 my ($response) = @_;
349 0 my @header_names = qw(
350 x-second-rate-limit
351 x-minute-rate-limit
352 x-daily-rate-limit
353 x-minute-request-left
354 x-daily-request-left
355 x-minute-reset-seconds
356 x-daily-reset-seconds
357 Retry-After
358 RateLimit-Policy
359 RateLimit
360 );
361 0 my %rate_limit;
362 0 for my $name (@header_names) {
363 0 my $val = $response->header($name);
364 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
365 0 $rate_limit{lc($name)} = $val;
366 }
367 }
368 0 return \%rate_limit;
369 }
370  
371 4 1;
372 __END__
↑ Summary Domain.pm »