| 1 | package API::INSEE::Sirene; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use HTTP::Request::Common qw/ GET POST /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use HTTP::Status ':constants'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use List::Util 'any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use POSIX 'strftime'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Switch; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = 4.04; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | API_AUTH_URL => 'https://api.insee.fr/token', | |||||||||||||||||||||||||||
| 19 | API_BASE_URL => 'https://api.insee.fr/entreprises/sirene/V3', | ||||||||||||||||||||||||||||
| 20 | DEFAULT_MAX_RESULTS => 20, # from documentation | ||||||||||||||||||||||||||||
| 21 | DEFAULT_TIMEOUT => 20, | ||||||||||||||||||||||||||||
| 22 | HARD_MAX_RESULTS => 1_000, # from documentation | ||||||||||||||||||||||||||||
| 23 | MAX_SIREN_LENGTH => 9, | ||||||||||||||||||||||||||||
| 24 | MAX_SIRET_LENGTH => 14, | ||||||||||||||||||||||||||||
| 25 | MIN_LENGTH => 3, | ||||||||||||||||||||||||||||
| 26 | 1 | }; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | my $EMPTY = q//; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | my $historized_fields = { | |||||||||||||||||||||||||||
| 31 | siren => [ qw/ | ||||||||||||||||||||||||||||
| 32 | dateFin dateDebut | ||||||||||||||||||||||||||||
| 33 | etatAdministratifUniteLegale changementEtatAdministratifUniteLegale | ||||||||||||||||||||||||||||
| 34 | nomUniteLegale changementNomUniteLegale nomUsageUniteLegale changementNomUsageUniteLegale | ||||||||||||||||||||||||||||
| 35 | denominationUniteLegale changementDenominationUniteLegale denominationUsuelle1UniteLegale | ||||||||||||||||||||||||||||
| 36 | denominationUsuelle2UniteLegale denominationUsuelle3UniteLegale changementDenominationUsuelleUniteLegale | ||||||||||||||||||||||||||||
| 37 | categorieJuridiqueUniteLegale changementCategorieJuridiqueUniteLegale activitePrincipaleUniteLegale | ||||||||||||||||||||||||||||
| 38 | nomenclatureActivitePrincipaleUniteLegale changementActivitePrincipaleUniteLegale nicSiegeUniteLegale | ||||||||||||||||||||||||||||
| 39 | changementNicSiegeUniteLegale economieSocialeSolidaireUniteLegale changementEconomieSocialeSolidaireUniteLegale | ||||||||||||||||||||||||||||
| 40 | caractereEmployeurUniteLegale changementCaractereEmployeurUniteLegale | ||||||||||||||||||||||||||||
| 41 | / ], | ||||||||||||||||||||||||||||
| 42 | siret => [ qw/ | ||||||||||||||||||||||||||||
| 43 | etatAdministratifEtablissement changementEtatAdministratifEtablissement | ||||||||||||||||||||||||||||
| 44 | enseigne1Etablissement enseigne2Etablissement enseigne3Etablissement changementEnseigneEtablissement | ||||||||||||||||||||||||||||
| 45 | denominationUsuelleEtablissement changementDenominationUsuelleEtablissement | ||||||||||||||||||||||||||||
| 46 | activitePrincipaleEtablissement nomenclatureActivitePrincipaleEtablissement changementActivitePrincipaleEtablissement | ||||||||||||||||||||||||||||
| 47 | caractereEmployeurEtablissement changementCaractereEmployeurEtablissement | ||||||||||||||||||||||||||||
| 48 | / ], | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | my $useful_fields_legal_unit = [ | |||||||||||||||||||||||||||
| 52 | qw/ | ||||||||||||||||||||||||||||
| 53 | siren | ||||||||||||||||||||||||||||
| 54 | dateCreationUniteLegale | ||||||||||||||||||||||||||||
| 55 | sigleUniteLegale | ||||||||||||||||||||||||||||
| 56 | categorieEntreprise | ||||||||||||||||||||||||||||
| 57 | denominationUniteLegale denominationUsuelle1UniteLegale nomUniteLegale | ||||||||||||||||||||||||||||
| 58 | categorieJuridiqueUniteLegale | ||||||||||||||||||||||||||||
| 59 | activitePrincipaleUniteLegale nomenclatureActivitePrincipaleUniteLegale | ||||||||||||||||||||||||||||
| 60 | nicSiegeUniteLegale | ||||||||||||||||||||||||||||
| 61 | / | ||||||||||||||||||||||||||||
| 62 | ]; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | my $useful_fields_establishment = [ | |||||||||||||||||||||||||||
| 65 | qw/ | ||||||||||||||||||||||||||||
| 66 | siren siret | ||||||||||||||||||||||||||||
| 67 | denominationUsuelleEtablissement denominationUniteLegale denominationUsuelle1UniteLegale nomUniteLegale | ||||||||||||||||||||||||||||
| 68 | activitePrincipaleUniteLegale | ||||||||||||||||||||||||||||
| 69 | numeroVoieEtablissement typeVoieEtablissement libelleVoieEtablissement | ||||||||||||||||||||||||||||
| 70 | codePostalEtablissement libelleCommuneEtablissement | ||||||||||||||||||||||||||||
| 71 | / | ||||||||||||||||||||||||||||
| 72 | ]; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | my $useful_fields_aliases = { | |||||||||||||||||||||||||||
| 75 | nicSiege => 'nicSiegeUniteLegale', | ||||||||||||||||||||||||||||
| 76 | nom => [ 'denominationUniteLegale', 'nomUniteLegale' ], | ||||||||||||||||||||||||||||
| 77 | dateCreation => 'dateCreationUniteLegale', | ||||||||||||||||||||||||||||
| 78 | sigle => 'sigleUniteLegale', | ||||||||||||||||||||||||||||
| 79 | categorieJuridique => 'categorieJuridiqueUniteLegale', | ||||||||||||||||||||||||||||
| 80 | nomenclatureActivitePrincipale => 'nomenclatureActivitePrincipaleUniteLegale', | ||||||||||||||||||||||||||||
| 81 | activitePrincipale => 'activitePrincipaleUniteLegale', | ||||||||||||||||||||||||||||
| 82 | numvoie => 'numeroVoieEtablissement', | ||||||||||||||||||||||||||||
| 83 | typevoie => 'typeVoieEtablissement', | ||||||||||||||||||||||||||||
| 84 | nomvoie => 'libelleVoieEtablissement', | ||||||||||||||||||||||||||||
| 85 | codePostal => 'codePostalEtablissement', | ||||||||||||||||||||||||||||
| 86 | nomCommune => 'libelleCommuneEtablissement', | ||||||||||||||||||||||||||||
| 87 | adresseComplete => [ | ||||||||||||||||||||||||||||
| 88 | 'numeroVoieEtablissement', | ||||||||||||||||||||||||||||
| 89 | 'typeVoieEtablissement', 'libelleVoieEtablissement', | ||||||||||||||||||||||||||||
| 90 | 'codePostalEtablissement', 'libelleCommuneEtablissement' | ||||||||||||||||||||||||||||
| 91 | ], | ||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 96 | 1 | my ($credentials, $timeout, $max_results, $proxy) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | my $self = bless { | |||||||||||||||||||||||||||
| 99 | credentials => $credentials, | ||||||||||||||||||||||||||||
| 100 | user_agent => undef, | ||||||||||||||||||||||||||||
| 101 | token_expiration => undef, | ||||||||||||||||||||||||||||
| 102 | max_results => undef, | ||||||||||||||||||||||||||||
| 103 | debug_mode => 0, | ||||||||||||||||||||||||||||
| 104 | current_endpoint => undef, | ||||||||||||||||||||||||||||
| 105 | }, $class; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | $self->_initUserAgent(); | |||||||||||||||||||||||||||
| 108 | 1 | $self->setProxy($proxy); | |||||||||||||||||||||||||||
| 109 | 1 | $self->setMaxResults($max_results); | |||||||||||||||||||||||||||
| 110 | 1 | $self->setTimeout($timeout); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | return $self; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub setCredentials { | ||||||||||||||||||||||||||
|
Subroutine
setCredentials: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($self, $credentials) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | $self->{'credentials'} = $credentials; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub setMaxResults { | ||||||||||||||||||||||||||
|
Subroutine
setMaxResults: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 1 | my ($self, $max_results) = @_; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | $max_results //= DEFAULT_MAX_RESULTS; | ||||||||||||||||||||||||||
|
Condition50%1/2
$max_results //= 20
MC/DC0%0/1
$max_results //= 20
$max_results
Truth table50%1/2
A
A= $max_results
| |||||||||||||||||||||||||||||
| 125 | 1 | ▶ | $self->{'max_results'} = $max_results > HARD_MAX_RESULTS ? HARD_MAX_RESULTS : $max_results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | ▶ | sub setDebugMode { | ||||||||||||||||||||||||||
|
Subroutine
setDebugMode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 1 | my ($self, $debug_value) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | $self->{'debug_mode'} = $debug_value; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub setProxy { | ||||||||||||||||||||||||||
|
Subroutine
setProxy: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 1 | my ($self, $proxy) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | defined $proxy ? $self->{'user_agent'}->proxy([ 'http', 'https' ], $proxy) : $self->{'user_agent'}->env_proxy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | sub setTimeout { | ||||||||||||||||||||||||||
|
Subroutine
setTimeout: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 1 | my ($self, $timeout) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | ▶ | $timeout //= DEFAULT_TIMEOUT; | ||||||||||||||||||||||||||
|
Condition50%1/2
$timeout //= 20
MC/DC0%0/1
$timeout //= 20
$timeout
Truth table50%1/2
A
A= $timeout
| |||||||||||||||||||||||||||||
| 144 | 1 | $self->{'user_agent'}->timeout($timeout); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 5 | ▶ | sub setCurrentEndpoint { | ||||||||||||||||||||||||||
|
Subroutine
setCurrentEndpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 5 | my ($self, $endpoint) = @_; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 5 | $self->{'current_endpoint'} = $endpoint; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 4 | ▶ | sub _dumpRequest { | ||||||||||||||||||||||||||
|
Subroutine
_dumpRequest: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 154 | 4 | my ($self, $request, $response) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4 | my $dump = sprintf "Sent request:\n%s\n", $request->as_string; | |||||||||||||||||||||||||||
| 157 | 4 | ▶ | $dump .= sprintf "Received response:\n%s\n", $response->as_string if defined $response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 4 | return $dump; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | sub _initUserAgent { | ||||||||||||||||||||||||||
|
Subroutine
_initUserAgent: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | $self->{'user_agent'} = LWP::UserAgent->new(protocols_allowed => [ 'http', 'https' ]); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 1 | $self->{'user_agent'}->agent("Perl API::INSEE::Sirene V$VERSION"); | |||||||||||||||||||||||||||
| 168 | 1 | $self->{'user_agent'}->default_header('Accept' => 'application/json'); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub _getToken { | ||||||||||||||||||||||||||
|
Subroutine
_getToken: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | croak 'Please provide your credentials.' if !defined $self->{'credentials'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | my $request = POST API_AUTH_URL, | |||||||||||||||||||||||||||
| 177 | Authorization => "Basic $self->{'credentials'}", | ||||||||||||||||||||||||||||
| 178 | Content => [ grant_type => 'client_credentials' ]; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my $response = $self->{'user_agent'}->request($request); | |||||||||||||||||||||||||||
| 181 | 0 | my $json_obj; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ($response->content_type =~ m/^application\/json/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | $json_obj = decode_json($response->content); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | else { | ||||||||||||||||||||||||||||
| 187 | 0 | return 1, $self->_dumpRequest($request, $response); # the API may return xml intead of json... | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | switch ($response->code) { | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | case HTTP_OK { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | $self->{'token_expiration'} = time + $json_obj->{'expires_in'}; | |||||||||||||||||||||||||||
| 193 | 0 | $self->{'user_agent'}->default_header( Authorization => "Bearer $json_obj->{'access_token'}" ); | |||||||||||||||||||||||||||
| 194 | 0 | return 0; | |||||||||||||||||||||||||||
| 195 | 0 | } | |||||||||||||||||||||||||||
| 196 | 0 | ▶ | case HTTP_UNAUTHORIZED { # wrong credentials | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 0 | return 1 , $json_obj->{'error_description'}; | |||||||||||||||||||||||||||
| 198 | 0 | } | |||||||||||||||||||||||||||
| 199 | else { # oh dear we are in trouble | ||||||||||||||||||||||||||||
| 200 | 0 | return 1, $self->_dumpRequest($request, $response); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 0 | } | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 4 | ▶ | sub _sendRequest { | ||||||||||||||||||||||||||
|
Subroutine
_sendRequest: called CC=10 SCAR=39.0
| |||||||||||||||||||||||||||||
| 206 | 4 | my ($self, $parameters) = @_; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 4 | my $request; | |||||||||||||||||||||||||||
| 209 | 4 | ▶ | if (!exists $parameters->{'q'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 2 | my @url_parameters; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 2 | foreach my $key (keys %{ $parameters }) { | |||||||||||||||||||||||||||
| 213 | 4 | push @url_parameters, join '=', $key, $parameters->{$key}; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 2 | my $endpoint = join '?', $self->{'current_endpoint'}, join '&', @url_parameters; | |||||||||||||||||||||||||||
| 217 | 2 | $request = GET join '/', API_BASE_URL, $endpoint; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||
| 220 | $request = POST join('/', API_BASE_URL, $self->{'current_endpoint'}), | ||||||||||||||||||||||||||||
| 221 | 2 | Content => [ %{ $parameters } ]; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 4 | ▶ | if ($self->{'debug_mode'}) { # Requests will not be sent in debug mode | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 4 | return 0, $self->_dumpRequest($request); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | if (!defined $self->{'token_expiration'} || $self->{'token_expiration'} < time) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $self->{"token_expiration"} or $self->{"token_expiration"} < time
MC/DC0%0/2
not defined $self->{"token_expiration"} or $self->{"token_expiration"} < time
not defined $self->{"token_expiration"}
$self->{"token_expiration"} < time
Truth table0%0/3
not(A) or B
A= not defined $self->{"token_expiration"}B= $self->{"token_expiration"} < time
| |||||||||||||||||||||||||||||
| 229 | 0 | my ($err, $msg) = $self->_getToken(); | |||||||||||||||||||||||||||
| 230 | 0 | ▶ | croak $msg if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | my $response = $self->{'user_agent'}->request($request); | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | switch ($response->code) { | |||||||||||||||||||||||||||
| 236 | 0 | ▶ | case [ HTTP_OK, HTTP_NOT_FOUND ] { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 0 | return 0, $response->content; | |||||||||||||||||||||||||||
| 238 | 0 | } | |||||||||||||||||||||||||||
| 239 | 0 | ▶ | case HTTP_MOVED_PERMANENTLY { # duplicated legal unit/ establishment | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 0 | return 1, sprintf "%s\n%s", $response->message, $response->header('Location'); | |||||||||||||||||||||||||||
| 241 | 0 | } | |||||||||||||||||||||||||||
| 242 | 0 | ▶ | case [ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | HTTP_REQUEST_URI_TOO_LARGE, HTTP_TOO_MANY_REQUESTS, | ||||||||||||||||||||||||||||
| 244 | HTTP_UNAUTHORIZED, HTTP_FORBIDDEN, | ||||||||||||||||||||||||||||
| 245 | HTTP_SERVICE_UNAVAILABLE | ||||||||||||||||||||||||||||
| 246 | 0 | ] { | |||||||||||||||||||||||||||
| 247 | # There is no syntax error in request, the http message should be sufficient to understand the problem | ||||||||||||||||||||||||||||
| 248 | 0 | return 1, $response->message; | |||||||||||||||||||||||||||
| 249 | 0 | } | |||||||||||||||||||||||||||
| 250 | else { # case HTTP_BAD_REQUEST || HTTP_INTERNAL_SERVER_ERROR | ||||||||||||||||||||||||||||
| 251 | 0 | return 1, $self->_dumpRequest($request, $response); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | 0 | } | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 4 | ▶ | sub _buildParameters { | ||||||||||||||||||||||||||
|
Subroutine
_buildParameters: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 257 | 4 | my ($self, $usefull_fields, $desired_fields, $criteria) = @_; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # Parameters names come from the documentation | ||||||||||||||||||||||||||||
| 260 | my $parameters = { | ||||||||||||||||||||||||||||
| 261 | date => strftime('%Y-%m-%d', localtime), | ||||||||||||||||||||||||||||
| 262 | 4 | nombre => $self->{'max_results'}, | |||||||||||||||||||||||||||
| 263 | }; | ||||||||||||||||||||||||||||
| 264 | 4 | ▶ | $parameters->{'champs'} = $self->_buildFields($usefull_fields, $desired_fields) if (defined $desired_fields && $desired_fields ne 'all'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $desired_fields and $desired_fields ne "all"
MC/DC0%0/2
defined $desired_fields and $desired_fields ne "all"
defined $desired_fields
$desired_fields ne "all"
Truth table33%1/3
A and B
A= defined $desired_fieldsB= $desired_fields ne "all"
| |||||||||||||||||||||||||||||
| 265 | 4 | ▶ | $parameters->{'q'} = sprintf('(%s)', $criteria) if defined $criteria; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 4 | return $parameters; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub _buildFields { | ||||||||||||||||||||||||||
|
Subroutine
_buildFields: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 271 | 0 | my ($self, $usefull_fields, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if (defined $desired_fields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 0 | return $self->_mapAliases($desired_fields); | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | else { | ||||||||||||||||||||||||||||
| 277 | 0 | return join ',', @{ $usefull_fields }; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | ▶ | sub _mapAliases { | ||||||||||||||||||||||||||
|
Subroutine
_mapAliases: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 282 | 0 | my ($self, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | my @desired_fields = ref $desired_fields eq 'ARRAY' ? @{ $desired_fields } : $desired_fields; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | foreach my $desired_field (@desired_fields) { | |||||||||||||||||||||||||||
| 287 | 0 | ▶ | if (exists $useful_fields_aliases->{$desired_field}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if (ref $useful_fields_aliases->{$desired_field} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 0 | $desired_field = join ',', @{ $useful_fields_aliases->{$desired_field} }; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | else { | ||||||||||||||||||||||||||||
| 292 | 0 | $desired_field = $useful_fields_aliases->{$desired_field}; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | return join ',', @desired_fields; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 13 | ▶ | sub getCustomCriteria { | ||||||||||||||||||||||||||
|
Subroutine
getCustomCriteria: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||
| 301 | 13 | my ($self, $field_name, $value, $search_mode) = @_; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 13 | ▶ | croak 'No endpoint specified.' if !defined $self->{'current_endpoint'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 13 | ▶ | $search_mode //= 'aproximate'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$search_mode //= "aproximate"
MC/DC100%1/1
$search_mode //= "aproximate"
$search_mode
Truth table100%2/2
A
A= $search_mode
| |||||||||||||||||||||||||||||
| 306 | 13 | ▶ | if (exists $useful_fields_aliases->{$field_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 1 | ▶ | if (ref $useful_fields_aliases->{$field_name} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 0 | croak "Can't use the alias $field_name in custom criteria"; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 1 | $field_name = $useful_fields_aliases->{$field_name}; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 13 | ▶ | if ($search_mode eq 'aproximate') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | 6 | my @criteria; | |||||||||||||||||||||||||||
| 315 | 6 | my @words = split /[ \/-]/, $value; | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 6 | foreach my $word (@words) { | |||||||||||||||||||||||||||
| 318 | 6 | $word =~ s/&/%26/ig; | |||||||||||||||||||||||||||
| 319 | 6 | $word = sprintf '(%s:"%s"~ OR %s:*%s*)', $field_name, $word, $field_name, $word; | |||||||||||||||||||||||||||
| 320 | 6 | ▶ | $word = "periode$word" if any { $_ eq $field_name } @{ $historized_fields->{$self->{'current_endpoint'}} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 6 | push @criteria, $word; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 6 | return join ' AND ', @criteria; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 7 | my $criteria; | |||||||||||||||||||||||||||
| 330 | 7 | $value =~ s/&/%26/ig; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 7 | ▶ | if ($search_mode eq 'exact') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 333 | 5 | $criteria = sprintf '%s:%s', $field_name, $value; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | elsif ($search_mode eq 'begin') { | ||||||||||||||||||||||||||||
| 336 | 2 | $criteria = sprintf '%s:%s*', $field_name, $value; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 7 | ▶ | $criteria = "periode($criteria)" if any { $_ eq $field_name } @{ $historized_fields->{$self->{'current_endpoint'}} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 7 | return $criteria; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 2 | ▶ | sub searchByCustomCriteria { | ||||||||||||||||||||||||||
|
Subroutine
searchByCustomCriteria: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 345 | 2 | my ($self, $criteria, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 2 | my $parameters; | |||||||||||||||||||||||||||
| 348 | 2 | switch ($self->{'current_endpoint'}) { | |||||||||||||||||||||||||||
| 349 | 0 | ▶ | case 'siren' { $parameters = $self->_buildParameters($useful_fields_legal_unit, $desired_fields, $criteria) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 0 | ▶ | case 'siret' { $parameters = $self->_buildParameters($useful_fields_establishment, $desired_fields, $criteria) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | else { croak 'Bad endpoint specified.' } | |||||||||||||||||||||||||||
| 352 | 0 | } | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 2 | return $self->_sendRequest($parameters); | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 1 | ▶ | sub getLegalUnitBySIREN { | ||||||||||||||||||||||||||
|
Subroutine
getLegalUnitBySIREN: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 358 | 1 | my ($self, $siren_number, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 0 | ▶ | return 1, "Invalid SIREN $siren_number -> Must be a ${ \MAX_SIREN_LENGTH } digits number." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 1 | if $siren_number !~ m/^\d{${ \MAX_SIREN_LENGTH }}$/; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 1 | $self->setCurrentEndpoint("siren/$siren_number"); | |||||||||||||||||||||||||||
| 364 | 1 | my $parameters = $self->_buildParameters($useful_fields_legal_unit, $desired_fields); | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 1 | return $self->_sendRequest($parameters); | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 0 | ▶ | sub searchLegalUnitBySIREN { | ||||||||||||||||||||||||||
|
Subroutine
searchLegalUnitBySIREN: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 370 | 0 | my ($self, $siren_number, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 0 | ▶ | return 1, "Invalid SIREN $siren_number -> Must be a ${ \MIN_LENGTH } digits min and ${ \MAX_SIREN_LENGTH } digits number max." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 0 | if $siren_number !~ m/^\d{${ \MIN_LENGTH },${ \MAX_SIREN_LENGTH }}$/; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | $self->setCurrentEndpoint('siren'); | |||||||||||||||||||||||||||
| 376 | 0 | my $criteria = $self->getCustomCriteria('siren', $siren_number, 'begin'); | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 0 | return $self->searchByCustomCriteria($criteria, $desired_fields); | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 1 | ▶ | sub getEstablishmentBySIRET { | ||||||||||||||||||||||||||
|
Subroutine
getEstablishmentBySIRET: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 382 | 1 | my ($self, $siret_number, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | return 1, "Invalid SIRET $siret_number -> Must be a ${ \MAX_SIRET_LENGTH } digits number." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | 1 | if $siret_number !~ m/^\d{${ \MAX_SIRET_LENGTH }}$/; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 1 | $self->setCurrentEndpoint("siret/$siret_number"); | |||||||||||||||||||||||||||
| 388 | 1 | my $parameters = $self->_buildParameters($useful_fields_establishment, $desired_fields); | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 1 | return $self->_sendRequest($parameters); | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub getEstablishmentsBySIREN { | ||||||||||||||||||||||||||
|
Subroutine
getEstablishmentsBySIREN: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 394 | 0 | my ($self, $siren_number, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 0 | ▶ | return (1, "Invalid SIREN $siren_number -> Must be a ${ \MAX_SIREN_LENGTH } digits number.") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | 0 | if $siren_number !~ m/^\d{${ \MAX_SIREN_LENGTH }}$/; | |||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 0 | $self->setCurrentEndpoint('siret'); | |||||||||||||||||||||||||||
| 400 | 0 | my $criteria = $self->getCustomCriteria('siren', $siren_number); | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | return $self->searchByCustomCriteria($criteria, $desired_fields); | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub searchEstablishmentBySIRET { | ||||||||||||||||||||||||||
|
Subroutine
searchEstablishmentBySIRET: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 406 | 0 | my ($self, $siret_number, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 0 | ▶ | return 1, "Invalid SIRET $siret_number -> Must be a ${ \MIN_LENGTH } digits min and a ${ \MAX_SIRET_LENGTH } digits number max." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | 0 | if $siret_number !~ m/^\d{${ \MIN_LENGTH },${ \MAX_SIRET_LENGTH }}$/; | |||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 0 | $self->setCurrentEndpoint('siret'); | |||||||||||||||||||||||||||
| 412 | 0 | my $criteria = $self->getCustomCriteria('siret', $siret_number); | |||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 0 | return $self->searchByCustomCriteria($criteria, $desired_fields); | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 0 | ▶ | sub getLegalUnitsByName { | ||||||||||||||||||||||||||
|
Subroutine
getLegalUnitsByName: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 418 | 0 | my ($self, $name, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | $self->setCurrentEndpoint('siren'); | |||||||||||||||||||||||||||
| 421 | 0 | my $criteria = $self->getCustomCriteria('denominationUniteLegale', $name); | |||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | return $self->searchByCustomCriteria($criteria, $desired_fields); | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 1 | ▶ | sub getEstablishmentsByName { | ||||||||||||||||||||||||||
|
Subroutine
getEstablishmentsByName: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 427 | 1 | my ($self, $name, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 1 | $self->setCurrentEndpoint('siret'); | |||||||||||||||||||||||||||
| 430 | 1 | my $criteria = $self->getCustomCriteria('denominationUniteLegale', $name); | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 1 | return $self->searchByCustomCriteria($criteria, $desired_fields); | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 0 | ▶ | sub getLegalUnitsByUsualName { | ||||||||||||||||||||||||||
|
Subroutine
getLegalUnitsByUsualName: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 436 | 0 | my ($self, $name, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 0 | $self->setCurrentEndpoint('siren'); | |||||||||||||||||||||||||||
| 440 | 0 | my $criteria = $self->getCustomCriteria('denominationUsuelle1UniteLegale', $name); | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | return $self->searchByCustomCriteria($criteria, $desired_fields); | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 1 | ▶ | sub getEstablishmentsByUsualName { | ||||||||||||||||||||||||||
|
Subroutine
getEstablishmentsByUsualName: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 446 | 1 | my ($self, $name, $desired_fields) = @_; | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 1 | $self->setCurrentEndpoint('siret'); | |||||||||||||||||||||||||||
| 449 | 1 | my $criteria = $self->getCustomCriteria('denominationUsuelle1UniteLegale', $name); | |||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 1 | return $self->searchByCustomCriteria($criteria, $desired_fields); | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 1 | 1; | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | __END__ | ||||||||||||||||||||||||||||