Client.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 44 Conditioncond n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 184 CC 23 SCAR 33.7
CC
23
Cov
0%
analyse_image
56.0
new
42.0
get_result
30.0
CRAP 29.0
1 package IDCHECKIO::Client;
2  
3 #use strict;
4 use REST::Client;
5 use MIME::Base64;
6 use Cpanel::JSON::XS qw(encode_json);
7 use IDCHECKIO::ResponseIDCIO;
8 use JSON::Parse 'parse_json';
9  
10  
11 =head1 NAME
12  
13 IDCHECKIO::Client - Client to use the IDCHECKIO API easily
14  
15 =head1 SYNOPSIS
16  
17 To complete
18  
19 =head1 DESCRIPTION
20  
21 ...
22  
23 =cut
24  
25  
26 our $VERSION = '0.03';
27  
28 sub new {
29 my $class = shift;
30 my $self = {
31 _user => shift,
32 _pwd => shift,
33 _language => shift,
34 _host => shift,
35 _protocol => shift,
36 _port => shift,
37 _verify => shift,
38 };
39 $self->{_language} = "en" if !defined($self->{_language});
40 $self->{_host} = "idcheck.io" if !defined($self->{_host});
41 $self->{_protocol} = "https" if !defined($self->{_protocol});
42 $self->{_port} = "443" if !defined($self->{_port});
43 $self->{_verify} = "True" if !defined($self->{_verify});
44  
45 $self->{_client} = REST::Client->new();
46 $self->{_client}->setHost("$self->{_protocol}://$self->{_host}:$self->{_port}");
47 $self->{_client}->addHeader("Content-Type", "application/json");
48 $self->{_client}->addHeader("Authorization", "");
49
50 my $auth = encode_base64("$self->{_user}:$self->{_pwd}");
51 $self->{_client}->addHeader("Content-Type", "application/json");
52 $self->{_client}->addHeader("Authorization", "Basic $auth");
53 $self->{_client}->addHeader("Accept-Language", "$self->{_language}");
54  
55 bless $self, $class;
56 return $self;
57 }
58  
59 sub analyse_mrz {
60 my ( $self, $line1, $line2, $line3, $async ) = @_;
61 $async = "False" if !defined($async);
62 $line3 = "" if !defined($line3);
63  
64 my $method = "/rest/v0/task/mrz";
65 my $arguments = "?async=$async";
66 my $url = "$method$arguments";
67  
68 my $data = {
69 'line1' => $line1,
70 'line2' => $line2,
71 'line3' => $line3,
72 };
73 my $json_data = encode_json $data;
74  
75 my $result;
76 $self->{_client}->POST($url, $json_data);
77 my $json = parse_json($self->{_client}->responseContent());
78 if( $self->{_client}->responseCode() eq '200' ){
79 $result = ResponseIDCIO->new($self->{_client}->responseCode(), $json->{uid}, $json);
80 }
81 else {
82 $result = ResponseIDCIO->new($self->{_client}->responseCode(), 0, $json);
83 }
84 return $result;
85 }
86  
87  
88 sub analyse_image {
89 my ( $self, $recto, $verso, $async, $base64 ) = @_;
90 $async = "False" if !defined($async);
91 $verso = "" if !defined($verso);
92 $base64 = "False" if !defined($async);
93  
94 my $method = "/rest/v0/task/image";
95 my $arguments = "?async=$async";
96 my $url = "$method$arguments";
97
98 my ( $frontImage, $backImage ) = ( "", "" );
99  
100 my $buffer;
101 my $temp;
102 my $encoded_recto = "";
103 my $encoded_verso = "";
104 if ( $base64 ){
105 $encoded_recto = $recto;
106 $encoded_verso = $verso;
107 } else {
108 seek($recto, 0, 0);
109 binmode $recto;
110 while ( read( $recto, $buffer, 4096 ) ) {
111 #while ( read( INFILE, $buffer, 4096 ) ) {
112 $temp = encode_base64($buffer, "");
113 $encoded_recto = "$encoded_recto$temp";
114 }
115 if ( $verso != "" ){
116 #open INFILE, '<', $verso or die "Unable to open file $verso";
117 #binmode INFILE;
118 seek($verso, 0, 0);
119 binmode $verso;
120 #while ( read( INFILE, $buffer, 4096 ) ) {
121 while ( read( $verso, $buffer, 40096 ) ) {
122 $temp = encode_base64($buffer, "");
123 $encoded_verso = "$encoded_verso$temp";
124 }
125 }
126 }
127 my $data = {
128 'frontImage' => $encoded_recto,
129 'backImage' => $encoded_verso,
130 };
131 my $json_data = encode_json $data;
132
133 my $result;
134 $self->{_client}->POST($url, $json_data);
135 my $json = parse_json($self->{_client}->responseContent());
136 if( $self->{_client}->responseCode() eq '200' ){
137 $result = ResponseIDCIO->new($self->{_client}->responseCode(), $json->{uid}, $json);
138 }
139 else {
140 $result = ResponseIDCIO->new($self->{_client}->responseCode(), 0, $json);
141 }
142 return $result;
143 }
144  
145  
146 sub get_result {
147 my ( $self, $uid, $rectoImageCropped, $faceImageCropped, $signatureImageCropped ) = @_;
148 $rectoImageCropped = "False" if !defined($rectoImageCropped);
149 $faceImageCropped = "False" if !defined($faceImageCropped);
150 $signatureImageCropped = "False" if !defined($signatureImageCropped);
151  
152 my $method = "/rest/v0/result/";
153 my $arguments = "?rectoImageCropped=$rectoImageCropped&faceImageCropped=$faceImageCropped&signatureImageCropped=$signatureImageCropped";
154 my $url = "$method$uid/$arguments";
155  
156 my $result;
157 $self->{_client}->GET($url);
158 my $json = parse_json($self->{_client}->responseContent());
159 if( $self->{_client}->responseCode() eq '200' ){
160 $result = ResponseIDCIO->new($self->{_client}->responseCode(), $json->{uid}, $json);
161 }
162 else {
163 $result = ResponseIDCIO->new($self->{_client}->responseCode(), 0, $json);
164 }
165 return $result;
166 }
167  
168 sub get_report {
169 my ( $self, $uid, $path ) = @_;
170 $path = "" if !defined($path);
171  
172 my $method = "/rest/v0/pdfreport/";
173 my $arguments = "";
174 my $url = "$method$uid$arguments";
175  
176 my $result;
177 $self->{_client}->GET($url);
178 my $json = parse_json($self->{_client}->responseContent());
179 if( $self->{_client}->responseCode() eq '200' ){
180 $result = ResponseIDCIO->new($self->{_client}->responseCode(), $json->{uid}, $json);
181 }
182 else {
183 $result = ResponseIDCIO->new($self->{_client}->responseCode(), 0, $json);
184 }
185 return $result;
186 }
187  
188 sub get_status {
189 my ( $self, $uid, $wait ) = @_;
190 $wait = 0 if !defined($wait);
191  
192 my $method = "/rest/v0/task/";
193 my $arguments = "?wait=$wait";
194 my $url = "$method$uid/$arguments";
195  
196 my $result;
197 $self->{_client}->GET($url);
198 my $json = parse_json($self->{_client}->responseContent());
199 if( $self->{_client}->responseCode() eq '200' ){
200 $result = ResponseIDCIO->new($self->{_client}->responseCode(), $json->{uid}, $json);
201 }
202 else {
203 $result = ResponseIDCIO->new($self->{_client}->responseCode(), 0, $json);
204 }
205 return $result;
206 }
207  
208 1;