lib/Finance/IG/Record/REST/Client.pm

filter: Statementstmt 35.1% 90 / 256 Branchbran 1.5% 1 / 66 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 45.0% 18 / 40 Podpod 0.0% 0 / 2 totaltotal 27.9% 109 / 390 CC 44 SCAR 44.6
CC
44
Cov
27%
request
308.7
__ANON__
42.0
_prepareURL
29.5
CRAP 86.3
« lib/Finance/IG.pm ↑ Summary blib/script/igdisp.pl »
1 # So this package was used to generate the hashed response files for testing.
2 # It cannot be uploaded to cpan named REST::Client as I do not own that name space.
3 # You could rename it as below to use.
4 # A better approach is likely to use the true name below, and reloacte in the correct place and use Package::Alias
5 # to call as is now done with Finance::IG::REST::Client.
6 # The action in this package is under the hood, it calls the true REST::CLient and records the
7 # rsults in files that can later be used stand alone with no internet.
8 # So this is a bit broken right now, but only needed if you want to expand the tests for Finance::IG.
9 # Could do with some tests of its own!
10 package REST::Client;
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Digest::MD5 qw(md5 md5_hex md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 my $datapath;
19 1 our $fdebug=0; # set to 1 prints some diagnostics about whats going on
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 Finance::IG::Record::REST::Client, or more generally REST::Client
26  
27 =head1 Description.
28  
29 The aim of this module is to generate files used by the module Finance::IG::REST::Client which is a module that
30 mocks REST::Client by using pre-canned on disk responses to queries rather than the internet.
31  
32 THIS module is designedd to emulate REST::Client in providing data sourced from the internet, but also cans the
33 exchange for latter use by the mock version of REST::Client
34  
35 =head1 DESIGN
36  
37 There are a number of design issues that need to be handled, in particular interjecting this module as well as having the real module loaded.
38  
39 The latter is achieved by locating the real module, reading the file, changing the name to REST::Cli_orig, and then using an eval to load it.
40  
41 Its an important design decision that the unmodified Finance::IG.pm module is fitted up to use the this module or Finance::IG::REST::Client
42 entirely by manipulating %INC.
43  
44 =head1 VERSION
45  
46 Version 0.093
47  
48 =head1 SYNOPSIS
49  
50 See the included file Testing/test-record.pl
51  
52 =cut
53
54 1 our $VERSION=0.093;
55  
56 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=13.8
57 1 my $rcpath;
58 1 shift(@INC); # remove local path that was needed to find this file
59 1 my $thispath=$INC{'REST/Client.pm'};
60 1 $datapath=$thispath;
61 # die "datapath=$datapath"; # /home/mark/igrec/work/Finance-IG/Testing/../lib/Finance/IG/Record/REST/Client.pm
62 1 $datapath=~s#/[^./]+/\.\.##g; # /home/mark/igrec/work/Finance-IG/lib/Finance/IG/Record/REST/Client.pm
63 1 $datapath=~s#/[^/]+$##;
64 1 $datapath=~s#/[^/]+$##;
65 1 $datapath=~s#/[^/]+$##;
66 1 $datapath=~s#/[^/]+$##;
67 1 $datapath=~s#/[^/]+$##;
68 1 $datapath=~s#/[^/]+$##;
69 1 $datapath=$datapath."/Testing/Data";
70 # die "datapath=$datapath"; # /home/mark/igrec/work/Finance-IG/Testing/Data
71 1 mkdir $datapath;
72 1 delete $INC{'REST/Client.pm'}; #
73 1 require REST::Client; # determine path to original module.
74 1 $rcpath=$INC{'REST/Client.pm'}; # typically /usr/share/perl5/REST/Client.pm
75  
76 1 ▶ open(F,$rcpath) or die "Cannot find REST::Client not installed? ";
Branch: 1 branch
BranchTrueFalseTotal
open F, $rcpath or die "Cannot find REST::Client not installed? " 1 0 1
77 1 my $code=join("",<F>);
78 1 close F;
79 1 $code=~s/REST::Client/REST::Cli_orig/gm;
80 1 $code=~s/return if \$self->can\('setHost'\)/return if \$self->can('ContentFile')/gm;
81 1 $code=~s/no strict 'refs';/no strict 'refs';no warnings 'redefine';/gm;
82 0 ▶ eval $code ;
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine BEGIN: called CC=1 SCAR=5.0
Subroutine DELETE: not called CC=1 SCAR=5.0
Subroutine GET: not called CC=1 SCAR=5.0
Subroutine HEAD: not called CC=1 SCAR=5.0
Subroutine OPTIONS: not called CC=1 SCAR=5.0
Subroutine PATCH: not called CC=1 SCAR=5.0
Subroutine POST: not called CC=1 SCAR=5.0
Subroutine PUT: not called CC=1 SCAR=5.0
Subroutine _buildAccessors: not called CC=3 SCAR=21.8
Subroutine _buildUseragent: not called CC=2 SCAR=15.3
Subroutine _prepareURL: not called CC=6 SCAR=33.8
Subroutine addHeader: not called CC=2 SCAR=15.3
Subroutine buildQuery: not called CC=1 SCAR=5.0
Subroutine new: not called CC=4 SCAR=26.7
Subroutine request: not called CC=21 SCAR=57.3
Subroutine responseCode: not called CC=1 SCAR=5.0
Subroutine responseContent: not called CC=1 SCAR=5.0
Subroutine responseHeader: not called CC=2 SCAR=15.3
Subroutine responseHeaders: not called CC=1 SCAR=5.0
Subroutine responseXpath: not called CC=2 SCAR=15.3
Branch: 25 branches
BranchTrueFalseTotal
if $self->can("ContentFile") 0 0 0
if $self->getUseragent 0 0 0
if $url =~ /^https?:/ 0 0 0
if ($host) 0 0 0
unless $url =~ m[^/] 0 0 0
unless ($url =~ m[^\w+://]) 0 0 0
$self->getCert ? : 0 0 0
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (scalar @_ and scalar @_ % 2 == 0) { } 0 0 0
unless $url 0 0 0
if $headers and ref $headers ne "HASH" 0 0 0
if (defined $self->getTimeout) { } 0 0 0
if (defined $content and length $content) { } 0 0 0
if ($self->getCert) 0 0 0
unless $url =~ /^https/ 0 0 0
unless -f $self->getCert and -f $self->getKey 0 0 0
if (my $ca = $self->getCa) 0 0 0
unless -f $ca 0 0 0
if ($self->getPkcs12) 0 0 0
unless $url =~ /^https/ 0 0 0
unless -f $self->getPkcs12 0 0 0
if ($self->getPkcs12password) 0 0 0
$self->getFollow ? : 0 0 0
unless $header 0 0 0
if ($self->responseHeader('Content-type') =~ /html/) { } 0 0 0
Condition0%0/16
$self->{'_headers'} || {}
l!l
00
scalar @_ and scalar @_ % 2 == 0
!ll&&!rl&&r
000
$headers and ref $headers ne "HASH"
!ll&&!rl&&r
000
defined $content and length $content
!ll&&!rl&&r
000
$self->{'_headers'} || {}
l!l
00
-f $self->getCert and -f $self->getKey
!ll&&!rl&&r
000
MC/DC0%0/1
$self->{'_headers'} || {}
$self->{'_headers'}
MC/DC0%0/2
scalar @_ and scalar @_ % 2 == 0
scalar @_ scalar @_ % 2 == 0
MC/DC0%0/2
$headers and ref $headers ne "HASH"
$headers ref $headers ne "HASH"
MC/DC0%0/2
defined $content and length $content
defined $content length $content
MC/DC0%0/1
$self->{'_headers'} || {}
$self->{'_headers'}
MC/DC0%0/2
-f $self->getCert and -f $self->getKey
-f $self->getCert -f $self->getKey
Truth table0%0/2
A
A= $self->{'_headers'}
Aresult
11
00
Truth table0%0/3
A and B
A= scalar @_B= scalar @_ % 2 == 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $headersB= ref $headers ne "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->{'_headers'}
Aresult
11
00
Truth table0%0/3
A and B
A= -f $self->getCertB= -f $self->getKey
ABresult
0X0
100
111
83 }
84  
85 # Doesn't seem needed with global
86 # no warnings 'redefine';
87 # Subroutine REST::Client::PUT redefined at ...
88 #$SIG{__WARN__} = sub
89 #{
90 # my $warning = shift;
91 # my $m;
92 # warn $warning unless $warning =~ /Subroutine .* redefined at \(eval [0-9]+\)/;
93 # # Subroutine _buildUseragent redefined at (eval 179) line 491.
94 #};
95  
96 1 for my $name (qw/
97 PUT GET POST new _buildAccessors _buildUseragent getUseragent setUseragent
98 request _prepareURL getHost getTimeout
99 getCert getCa getPkcs12 getFollow getContentFile responseCode
100 responseHeader responseContent setHost
101 /
102 )
103 {
104 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 # no warnings 'redefine'; # did work but doesn't now. ?
106  
107 21 my $orig = \&{"REST::Cli_orig::$name"};
108 21 my $before= \&{"REST::Client::before_$name"};
109 21 my $after= \&{"REST::Client::after_$name"};
110 21 my $responseContent = \&{"REST::Cli_orig::responseContent"};
111 21 my $responseHeader = \&{"REST::Cli_orig::responseHeader"};
112  
113 21 *{"REST::Client::$name"} =
114 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
115 0 my $self=$_[0];
116 0 ▶ defined(&$before) and &$before(@_);
Branch: 1 branch
BranchTrueFalseTotal
defined &$before and &$before(@_) 0 0 0
117 0 ▶ my @return_values = wantarray ? $orig->(@_) : scalar $orig->(@_);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
118
119 0 ▶ if (defined(&$after))
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$after) 0 0 0
120 {
121 0 my $headers={};
122 0 for my $header (&{"REST::Cli_orig::responseHeaders"})
123 {
124 #print "retrieving header named $header\n";
125 0 my $value='';
126
127 0 $value=$self->responseHeader($header);
128 #print "$header=$value\n";
129 0 $headers->{$header}=$value;
130 }
131 0 &$after($self,
132 \@_,
133 JSON->new->canonical->encode(
134 { content=>decode_json($self->responseContent) ,
135 headers=>$headers
136 }
137 )
138 );
139 }
140 0 ▶ return wantarray ? @return_values : $return_values[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
141 21 };
142 }
143  
144  
145 # delete $SIG{__WARN__}; # This causes reinstatement of the warnings.
146  
147 #sub before_POST
148 #{
149 # print "b4 post @_\n";
150 # die;
151 #}
152 sub after_POST
153 0 ▶{
Subroutine after_POST: not called CC=4 SCAR=30.0
Pod undocumented
154 0 my ($self,$rparams,$content)=@_;
155 0 local $"=', ';
156 #die "@$rparams";
157 0 my ($url,$jdata,$headers)=@$rparams;
158 #my $jheaders=encode_json($headers);
159 #print "headers#1=$headers\n";
160 0 my $jheaders=JSON->new->canonical->encode($headers);
161 # print "After POST url=$url jdata=$jdata jheaders=$jheaders, content=\n". encode_json($content)."\n";
162 #### $content=JSON->new->canonical->encode(decode_json($content)->{$content});
163 0 $content=~s/("currentAccountId":)("[^"]+")/$1"REDACTED1"/g;
164 0 $content=~s/("clientId":)("[^"]+")/$1"REDACTED2"/g;
165 0 $content=~s/("accountId":)("[^"]+")/$1"REDACTED3"/g;
166 #$content=~s/\xfffd/£/gms;
167 0 $content=~s/\xA3/£/gms;
168 0 $content=~s/("X-SECURITY-TOKEN":)("[^"]+)"/$1"."/g;
169 0 $content=~s/("CST":)("[^"]+)"/$1"."/g;
170  
171 0 $jdata=~s/("identifier":)("[^"]+")/$1"igusername"/g;
172 0 $jdata=~s/("password":)("[^"]+")/$1"ig_correct_password"/g;
173 0 $jheaders=~s/("X-IG-API-KEY":)("[^"]+)"/$1"securitykey"/g;
174 0 $jheaders=~s/("X-SECURITY-TOKEN":)("[^"]+)"/$1"."/g;
175 0 $jheaders=~s/("CST":)("[^"]+)"/$1"."/g;
176 # print "After POST url=$url jdata=$jdata jheaders=$jheaders, content=\n". $content."\n";
177
178 0 ▶ $fdebug and print " url=$url\n jdata=$jdata\n jheaders=$jheaders\n";
Branch: 1 branch
BranchTrueFalseTotal
$REST::Client::fdebug and print " url=$url\n jdata=$jdata\n jheaders=$jheaders\n" 0 0 0
179 0 my $file="$datapath/".md5_hex($url,$jdata,$jheaders).".txt";
180 0 ▶ $fdebug and print "writing file $file\n";
Branch: 1 branch
BranchTrueFalseTotal
$REST::Client::fdebug and print "writing file $file\n" 0 0 0
181 0 ▶ open(F,">",$file) or die "Cannot open file $file for write";
Branch: 1 branch
BranchTrueFalseTotal
open F, ">", $file or die "Cannot open file $file for write" 0 0 0
182 0 print F $content."\n"; ;
183 0 close F;
184 }
185 sub after_GET
186 0 ▶{
Subroutine after_GET: not called CC=1 SCAR=6.9
Pod undocumented
187 0 my ($self,$rparams,$content)=@_;
188 #print "AFTER GET $$rparams[1]\n";
189 #print "rparams=@$rparams\n";
190 #my ($url,$headers)=@$rparams;
191 0 my ($url,$jdata,$headers)=@$rparams;
192 # my $jheaders=JSON->new->canonical->encode($headers);
193
194 #print "AFTER GET url=$url jdata=$jdata jheaders=$jheaders\n";
195 0 after_POST($self,[$url,$jdata,$headers] ,$content);
196 }
197 1 1;
198  
« lib/Finance/IG.pm ↑ Summary blib/script/igdisp.pl »