Picnic.pm

filter: Statementstmt 75.6% 127 / 168 Branchbran 32.1% 9 / 28 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 87.8% 36 / 41 Podpod 94.1% 16 / 17 totaltotal 69.2% 194 / 280 CC 22 SCAR 17.0
CC
22
Cov
67%
verify_2fa_code
30.0
generate_2fa_code
12.0
picnic_auth
9.8
CRAP 5.4
« Picnic/Result.pm ↑ Summary Picnic/Result/Cart.pm »
1 package WWW::Picnic;
2 # ABSTRACT: Library to access Picnic Supermarket API
3 2 our $VERSION = '0.101';
4 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use WWW::Picnic::Result::Login;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use WWW::Picnic::Result::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use WWW::Picnic::Result::Cart;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use WWW::Picnic::Result::DeliverySlots;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use WWW::Picnic::Result::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use WWW::Picnic::Result::Article;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use WWW::Picnic::Result::Categories;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use WWW::Picnic::Result::Suggestions;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 2 has user => (
23 is => 'ro',
24 required => 1,
25 );
26  
27  
28 2 has pass => (
29 is => 'ro',
30 required => 1,
31 );
32  
33  
34 has client_id => (
35 is => 'ro',
36 1 ▶ default => sub { 30100 },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 2 );
38  
39  
40 has api_version => (
41 1 ▶ isa => sub { $_[0] >= 15 },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 is => 'ro',
43 1 ▶ default => sub { 15 },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 2 );
45  
46  
47 has country => (
48 is => 'ro',
49 0 ▶ default => sub { 'de' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 2 );
51  
52  
53 14 ▶sub api_endpoint {
Subroutine api_endpoint: called CC=1 SCAR=0.0
Pod undocumented
54 14 my ( $self ) = @_;
55 14 return sprintf('https://storefront-prod.%s.picnicinternational.com/api/%s', $self->country, "".$self->api_version."");
56 }
57  
58 has http_agent => (
59 is => 'ro',
60 lazy => 1,
61 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 my $self = shift;
63 0 my $ua = LWP::UserAgent->new;
64 0 $ua->agent($self->http_agent_name);
65 0 return $ua;
66 },
67 2 );
68  
69  
70 has http_agent_name => (
71 is => 'ro',
72 lazy => 1,
73 0 ▶ default => sub { 'okhttp/3.12.2' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 2 );
75  
76  
77 has picnic_agent => (
78 is => 'ro',
79 lazy => 1,
80 1 ▶ default => sub { '30100;1.15.232-15154' },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 2 );
82  
83  
84 has picnic_did => (
85 is => 'ro',
86 lazy => 1,
87 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 # Generate a random device ID (16 hex chars)
89 1 my @chars = ('0'..'9', 'A'..'F');
90 1 return join '', map { $chars[rand @chars] } 1..16;
91 },
92 2 );
93  
94  
95 has json => (
96 is => 'ro',
97 lazy => 1,
98 1 ▶ default => sub { return JSON::MaybeXS->new( utf8 => 1, convert_blessed => 1 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 2 );
100  
101 has _auth_cache => (
102 is => 'ro',
103 1 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
104 2 );
105  
106 1 ▶sub login {
Subroutine login: called CC=5 SCAR=16.9
Pod documented
107 1 my ( $self ) = @_;
108 1 my $url = URI->new(join('/',$self->api_endpoint,'user','login'));
109 1 my $request = HTTP::Request->new( POST => $url );
110 1 $request->header('Accept' => 'application/json');
111 1 $request->header('Content-Type' => 'application/json; charset=UTF-8');
112 1 $request->content($self->json->encode({
113 key => $self->user,
114 secret => md5_hex($self->pass),
115 client_id => $self->client_id,
116 }));
117 1 my $response = $self->http_agent->request($request);
118 1 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 1 0 1
119 1 my $auth = $response->header('X-Picnic-Auth');
120 1 my $data = $self->json->decode($response->content);
121 1 $data->{auth_key} = $auth;
122 1 ▶ if ($auth && $data->{user_id} && !$data->{second_factor_authentication_required}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth and $data->{"user_id"} and not $data->{"second_factor_authentication_required"}) 1 0 1
Condition33%2/6
$auth and $data->{"user_id"} and not $data->{"second_factor_authentication_required"}
!ll&&!rl&&r
001
$auth and $data->{"user_id"}
!ll&&!rl&&r
001
MC/DC0%0/3
$auth and $data->{"user_id"} and not $data->{"second_factor_authentication_required"}
$auth $data->{"user_id"} not $data->{"second_factor_authentication_required"}
Truth table25%1/4
A and B and not(C)
A= $authB= $data->{"user_id"}C= not $data->{"second_factor_authentication_required"}
ABCresult
0XX0
10X0
1100
1111
123 1 $self->_auth_cache->{auth} = $auth;
124 1 $self->_auth_cache->{time} = time;
125 1 $self->_auth_cache->{user_id} = $data->{user_id};
126 }
127 1 return WWW::Picnic::Result::Login->new($data);
128 } else {
129 0 croak __PACKAGE__.": login failed! ".$response->status_line;
130 }
131 }
132  
133  
134 0 ▶sub generate_2fa_code {
Subroutine generate_2fa_code: not called CC=3 SCAR=24.8
Pod documented
135 0 my ( $self, $channel ) = @_;
136 0 ▶ $channel //= 'SMS';
Condition0%0/2
$channel //= "SMS"
l!l
00
MC/DC0%0/1
$channel //= "SMS"
$channel
Truth table0%0/2
A
A= $channel
Aresult
11
00
137 0 my $url = URI->new(join('/',$self->api_endpoint,'user','2fa','generate'));
138 0 my $request = HTTP::Request->new( POST => $url );
139 0 $request->header('Accept' => 'application/json');
140 0 $request->header('Content-Type' => 'application/json; charset=UTF-8');
141 0 $request->content($self->json->encode({ channel => $channel }));
142 0 my $response = $self->http_agent->request($request);
143 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
144 0 croak __PACKAGE__.": 2FA code generation failed! ".$response->status_line;
145 }
146 0 return 1;
147 }
148  
149  
150 0 ▶sub verify_2fa_code {
Subroutine verify_2fa_code: not called CC=5 SCAR=34.0
Pod documented
151 0 my ( $self, $code ) = @_;
152 0 ▶ croak __PACKAGE__.": 2FA code required" unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
153 0 my $url = URI->new(join('/',$self->api_endpoint,'user','2fa','verify'));
154 0 my $request = HTTP::Request->new( POST => $url );
155 0 $request->header('Accept' => 'application/json');
156 0 $request->header('Content-Type' => 'application/json; charset=UTF-8');
157 0 $request->content($self->json->encode({ otp => $code }));
158 0 my $response = $self->http_agent->request($request);
159 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
160 0 my $auth = $response->header('X-Picnic-Auth');
161 0 ▶ croak __PACKAGE__.": 2FA verify success, but no auth token!" unless $auth;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth 0 0 0
162 0 my $data = $self->json->decode($response->content);
163 0 $self->_auth_cache->{auth} = $auth;
164 0 $self->_auth_cache->{time} = time;
165 0 ▶ $self->_auth_cache->{user_id} = $data->{user_id} if $data->{user_id};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"user_id"} 0 0 0
166 0 return 1;
167 } else {
168 0 croak __PACKAGE__.": 2FA verification failed! ".$response->status_line;
169 }
170 }
171  
172  
173 12 ▶sub picnic_auth {
Subroutine picnic_auth: called CC=4 SCAR=22.9
Pod documented
174 12 my ( $self ) = @_;
175 12 ▶ unless (defined $self->_auth_cache->{auth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_auth_cache->{"auth"}) 0 12 12
176 0 my $login = $self->login;
177 0 ▶ if ($login->requires_2fa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($login->requires_2fa) 0 0 0
178 0 croak __PACKAGE__.": 2FA required! Call login() and handle 2FA flow manually.";
179 }
180 0 ▶ unless ($self->_auth_cache->{auth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_auth_cache->{"auth"}) 0 0 0
181 0 croak __PACKAGE__.": login failed to obtain auth token!";
182 }
183 }
184 12 return $self->_auth_cache->{auth};
185 }
186  
187  
188 12 ▶sub request {
Subroutine request: called CC=6 SCAR=18.2
Pod documented
189 12 my ( $self, @original_args ) = @_;
190 12 my ( $method, $path, $data, %params ) = @original_args;
191 12 ▶ $data = [] if $method eq 'PUT' and !$data;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "PUT" and not $data 0 12 12
Condition33%1/3
$method eq "PUT" and not $data
!ll&&!rl&&r
1200
MC/DC0%0/2
$method eq "PUT" and not $data
$method eq "PUT" not $data
Truth table33%1/3
A and not(B)
A= $method eq "PUT"B= not $data
ABresult
0X0
100
111
192 12 my $url = URI->new(join('/',$self->api_endpoint,$path));
193 12 ▶ if (%params) {
Branch: 1 branch
BranchTrueFalseTotal
if (%params) 3 9 12
194 3 $url->query_form(%params);
195 }
196 12 my $request = HTTP::Request->new( $method => $url );
197 12 $request->header('Accept' => 'application/json');
198 12 $request->header('X-Picnic-Auth' => $self->picnic_auth );
199 12 $request->header('X-Picnic-Agent' => $self->picnic_agent );
200 12 $request->header('X-Picnic-Did' => $self->picnic_did );
201 12 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 3 9 12
202 3 $request->header('Content-Type' => 'application/json');
203 3 $request->content($self->json->encode($data));
204 }
205 12 my $response = $self->http_agent->request($request);
206 12 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 12 12
207 0 croak __PACKAGE__.": request to ".$url->as_string." failed! ".$response->status_line;
208 }
209 12 return $self->json->decode($response->content);
210 }
211  
212  
213 2 ▶sub get_user {
Subroutine get_user: called CC=1 SCAR=0.0
Pod documented
214 2 my ( $self ) = @_;
215 2 return WWW::Picnic::Result::User->new( $self->request( GET => 'user' ) );
216 }
217  
218  
219 1 ▶sub get_cart {
Subroutine get_cart: called CC=1 SCAR=0.0
Pod documented
220 1 my ( $self ) = @_;
221 1 return WWW::Picnic::Result::Cart->new( $self->request( GET => 'cart' ) );
222 }
223  
224  
225 1 ▶sub clear_cart {
Subroutine clear_cart: called CC=1 SCAR=0.0
Pod documented
226 1 my ( $self ) = @_;
227 1 return WWW::Picnic::Result::Cart->new( $self->request( POST => 'cart/clear' ) );
228 }
229  
230  
231 1 ▶sub get_delivery_slots {
Subroutine get_delivery_slots: called CC=1 SCAR=0.0
Pod documented
232 1 my ( $self ) = @_;
233 1 return WWW::Picnic::Result::DeliverySlots->new( $self->request( GET => 'cart/delivery_slots' ) );
234 }
235  
236  
237 1 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
238 1 my ( $self, $term ) = @_;
239 1 return WWW::Picnic::Result::Search->new( $self->request( GET => 'pages/search-page-results', undef, search_term => $term ) );
240 }
241  
242  
243 1 ▶sub get_article {
Subroutine get_article: called CC=1 SCAR=0.0
Pod documented
244 1 my ( $self, $product_id ) = @_;
245 1 return WWW::Picnic::Result::Article->new( $self->request( GET => "articles/$product_id" ) );
246 }
247  
248  
249 1 ▶sub add_to_cart {
Subroutine add_to_cart: called CC=2 SCAR=7.1
Pod documented
250 1 my ( $self, $product_id, $count ) = @_;
251 1 ▶ $count //= 1;
Condition50%1/2
$count //= 1
l!l
10
MC/DC0%0/1
$count //= 1
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
252 1 return WWW::Picnic::Result::Cart->new(
253 $self->request( POST => 'cart/add_product', { product_id => $product_id, count => $count } )
254 );
255 }
256  
257  
258 1 ▶sub remove_from_cart {
Subroutine remove_from_cart: called CC=2 SCAR=7.1
Pod documented
259 1 my ( $self, $product_id, $count ) = @_;
260 1 ▶ $count //= 1;
Condition50%1/2
$count //= 1
l!l
10
MC/DC0%0/1
$count //= 1
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
261 1 return WWW::Picnic::Result::Cart->new(
262 $self->request( POST => 'cart/remove_product', { product_id => $product_id, count => $count } )
263 );
264 }
265  
266  
267 1 ▶sub set_delivery_slot {
Subroutine set_delivery_slot: called CC=1 SCAR=0.0
Pod documented
268 1 my ( $self, $slot_id ) = @_;
269 1 return WWW::Picnic::Result::Cart->new(
270 $self->request( POST => 'cart/set_delivery_slot', { slot_id => $slot_id } )
271 );
272 }
273  
274  
275 1 ▶sub get_categories {
Subroutine get_categories: called CC=2 SCAR=7.1
Pod documented
276 1 my ( $self, $depth ) = @_;
277 1 ▶ $depth //= 0;
Condition50%1/2
$depth //= 0
l!l
01
MC/DC0%0/1
$depth //= 0
$depth
Truth table50%1/2
A
A= $depth
Aresult
11
00
278 1 return WWW::Picnic::Result::Categories->new( $self->request( GET => 'my_store', undef, depth => $depth ) );
279 }
280  
281  
282 1 ▶sub get_suggestions {
Subroutine get_suggestions: called CC=1 SCAR=0.0
Pod documented
283 1 my ( $self, $term ) = @_;
284 1 return WWW::Picnic::Result::Suggestions->new( $self->request( GET => 'suggest', undef, search_term => $term ) );
285 }
286  
287  
288 2 1;
289  
290 __END__
« Picnic/Result.pm ↑ Summary Picnic/Result/Cart.pm »