| 1 | package Net::Async::Webservice::DHL; | ||||||||||||||
| 2 | 4 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 4 | ▶ | use Types::Standard qw(Str Bool Object Dict Num Optional ArrayRef HashRef Undef Optional); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use Types::URI qw(Uri); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | use Types::DateTime | ||||||||||||||
| 6 | 4 | ▶ | DateTime => { -as => 'DateTimeT' }, | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 4 | Format => { -as => 'DTFormat' }; | |||||||||||||
| 8 | 4 | ▶ | use Net::Async::Webservice::DHL::Types qw(Address RouteType RegionCode CountryCode); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | ▶ | use Net::Async::Webservice::DHL::Exception; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use Type::Params qw(compile); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use Error::TypeTiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 4 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 4 | ▶ | use List::AllUtils 'pairwise'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 4 | ▶ | use HTTP::Request; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 4 | ▶ | use XML::Compile::Cache; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 4 | ▶ | use XML::Compile::Util 'type_of_node'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 4 | ▶ | use XML::LibXML; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 4 | ▶ | use Encode; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 4 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 4 | ▶ | use Future; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 4 | ▶ | use DateTime; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 4 | ▶ | use File::ShareDir 'dist_dir'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 4 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 4 | our $VERSION = '1.2.2'; # VERSION | |||||||||||||
| 25 | |||||||||||||||
| 26 | # ABSTRACT: DHL API client, non-blocking | ||||||||||||||
| 27 | |||||||||||||||
| 28 | |||||||||||||||
| 29 | 4 | my %base_urls = ( | |||||||||||||
| 30 | live => 'https://xmlpi-ea.dhl.com/XMLShippingServlet', | ||||||||||||||
| 31 | test => 'https://xmlpitest-ea.dhl.com/XMLShippingServlet', | ||||||||||||||
| 32 | ); | ||||||||||||||
| 33 | |||||||||||||||
| 34 | |||||||||||||||
| 35 | has live_mode => ( | ||||||||||||||
| 36 | is => 'rw', | ||||||||||||||
| 37 | isa => Bool, | ||||||||||||||
| 38 | trigger => 1, | ||||||||||||||
| 39 | 2 | ▶ | default => sub { 0 }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 4 | ); | |||||||||||||
| 41 | |||||||||||||||
| 42 | |||||||||||||||
| 43 | 4 | has base_url => ( | |||||||||||||
| 44 | is => 'lazy', | ||||||||||||||
| 45 | isa => Str, | ||||||||||||||
| 46 | clearer => '_clear_base_url', | ||||||||||||||
| 47 | ); | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 2 | ▶ | sub _trigger_live_mode { ## no critic(ProhibitUnusedPrivateSubroutines) | ||||||||||||
|
Subroutine
_trigger_live_mode: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | 2 | my ($self) = @_; | |||||||||||||
| 51 | |||||||||||||||
| 52 | 2 | $self->_clear_base_url; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | 3 | ▶ | sub _build_base_url { ## no critic(ProhibitUnusedPrivateSubroutines) | ||||||||||||
|
Subroutine
_build_base_url: called CC=2 SCAR=6.9
| |||||||||||||||
| 55 | 3 | my ($self) = @_; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 3 | ▶ | return $base_urls{$self->live_mode ? 'live' : 'test'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | |||||||||||||||
| 61 | 4 | has username => ( | |||||||||||||
| 62 | is => 'ro', | ||||||||||||||
| 63 | isa => Str, | ||||||||||||||
| 64 | required => 1, | ||||||||||||||
| 65 | ); | ||||||||||||||
| 66 | 4 | has password => ( | |||||||||||||
| 67 | is => 'ro', | ||||||||||||||
| 68 | isa => Str, | ||||||||||||||
| 69 | required => 1, | ||||||||||||||
| 70 | ); | ||||||||||||||
| 71 | |||||||||||||||
| 72 | |||||||||||||||
| 73 | 4 | with 'Net::Async::Webservice::Common::WithUserAgent'; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 4 | has _xml_cache => ( | |||||||||||||
| 76 | is => 'lazy', | ||||||||||||||
| 77 | ); | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 2 | ▶ | sub _build__xml_cache { ## no critic(ProhibitUnusedPrivateSubroutines) | ||||||||||||
|
Subroutine
_build__xml_cache: called CC=2 SCAR=6.9
| |||||||||||||||
| 80 | 2 | my ($self) = @_; | |||||||||||||
| 81 | |||||||||||||||
| 82 | 2 | my $dir = dist_dir('Net-Async-Webservice-DHL'); | |||||||||||||
| 83 | 2 | my $c = XML::Compile::Cache->new( | |||||||||||||
| 84 | schema_dirs => [ $dir ], | ||||||||||||||
| 85 | opts_rw => { | ||||||||||||||
| 86 | elements_qualified => 'TOP', | ||||||||||||||
| 87 | }, | ||||||||||||||
| 88 | ); | ||||||||||||||
| 89 | 2 | for my $f (qw(datatypes datatypes_global | |||||||||||||
| 90 | DCT-req DCTRequestdatatypes | ||||||||||||||
| 91 | DCT-Response DCTResponsedatatypes | ||||||||||||||
| 92 | routing-global-req routing-global-res | ||||||||||||||
| 93 | routing-err-res err-res)) { | ||||||||||||||
| 94 | 20 | $c->importDefinitions("$f.xsd"); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | 2 | $c->declare('WRITER' => '{http://www.dhl.com}DCTRequest'); | |||||||||||||
| 97 | 2 | $c->declare('READER' => '{http://www.dhl.com}DCTResponse'); | |||||||||||||
| 98 | |||||||||||||||
| 99 | 2 | $c->declare('WRITER' => '{http://www.dhl.com}RouteRequest'); | |||||||||||||
| 100 | 2 | $c->declare('READER' => '{http://www.dhl.com}RouteResponse'); | |||||||||||||
| 101 | 2 | $c->declare('READER' => '{http://www.dhl.com}RoutingErrorResponse'); | |||||||||||||
| 102 | |||||||||||||||
| 103 | 2 | $c->declare('READER' => '{http://www.dhl.com}ErrorResponse'); | |||||||||||||
| 104 | |||||||||||||||
| 105 | 2 | $c->compileAll; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 2 | return $c; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | |||||||||||||||
| 111 | 4 | with 'Net::Async::Webservice::Common::WithConfigFile'; | |||||||||||||
| 112 | |||||||||||||||
| 113 | |||||||||||||||
| 114 | 5 | ▶ | sub _mr { | ||||||||||||
|
Subroutine
_mr: called CC=2 SCAR=8.1
| |||||||||||||||
| 115 | 5 | ▶ | if ($_[0]->{message_reference}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | 0 | return ( message_reference => $_[0]->{message_reference} ); | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | 5 | return; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 3 | ▶ | sub get_capability { | ||||||||||||
|
Subroutine
get_capability: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||
| 122 | 3 | state $argcheck = compile( | |||||||||||||
| 123 | Object, | ||||||||||||||
| 124 | Dict[ | ||||||||||||||
| 125 | from => Address, | ||||||||||||||
| 126 | to => Address, | ||||||||||||||
| 127 | is_dutiable => Bool, | ||||||||||||||
| 128 | currency_code => Str, | ||||||||||||||
| 129 | shipment_value => Num, | ||||||||||||||
| 130 | product_code => Optional[Str], | ||||||||||||||
| 131 | date => Optional[DateTimeT->plus_coercions(DTFormat['ISO8601'])], | ||||||||||||||
| 132 | message_reference => Optional[Str], | ||||||||||||||
| 133 | ], | ||||||||||||||
| 134 | ); | ||||||||||||||
| 135 | 3 | my ($self,$args) = $argcheck->(@_); | |||||||||||||
| 136 | |||||||||||||||
| 137 | $args->{date} = $args->{date} | ||||||||||||||
| 138 | 3 | ▶ | ? $args->{date}->clone->set_time_zone('UTC') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | : DateTime->now(time_zone => 'UTC'); | ||||||||||||||
| 140 | |||||||||||||||
| 141 | my $req = { | ||||||||||||||
| 142 | From => $args->{from}->as_hash('capability'), | ||||||||||||||
| 143 | To => $args->{to}->as_hash('capability'), | ||||||||||||||
| 144 | BkgDetails => { | ||||||||||||||
| 145 | PaymentCountryCode => $args->{to}->country_code, | ||||||||||||||
| 146 | Date => $args->{date}->ymd, | ||||||||||||||
| 147 | ReadyTime => 'PT' . $args->{date}->hour . 'H' . $args->{date}->minute . 'M', | ||||||||||||||
| 148 | DimensionUnit => 'CM', | ||||||||||||||
| 149 | WeightUnit => 'KG', | ||||||||||||||
| 150 | IsDutiable => ($args->{is_dutiable} ? 'Y' : 'N'), | ||||||||||||||
| 151 | NetworkTypeCode => 'AL', | ||||||||||||||
| 152 | ( defined $args->{product_code} ? ( | ||||||||||||||
| 153 | QtdShp => { | ||||||||||||||
| 154 | GlobalProductCode => $args->{product_code}, | ||||||||||||||
| 155 | QtdShpExChrg => { | ||||||||||||||
| 156 | SpecialServiceType => 'OSINFO', | ||||||||||||||
| 157 | }, | ||||||||||||||
| 158 | }, | ||||||||||||||
| 159 | ) : () ), | ||||||||||||||
| 160 | }, | ||||||||||||||
| 161 | Dutiable => { | ||||||||||||||
| 162 | DeclaredCurrency => $args->{currency_code}, | ||||||||||||||
| 163 | DeclaredValue => $args->{shipment_value}, | ||||||||||||||
| 164 | }, | ||||||||||||||
| 165 | 3 | ▶ | }; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 166 | |||||||||||||||
| 167 | return $self->xml_request({ | ||||||||||||||
| 168 | data => $req, | ||||||||||||||
| 169 | request_method => 'GetCapability', | ||||||||||||||
| 170 | _mr($args), | ||||||||||||||
| 171 | })->then( | ||||||||||||||
| 172 | 3 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 173 | 3 | my ($response) = @_; | |||||||||||||
| 174 | 3 | return Future->wrap($response); | |||||||||||||
| 175 | }, | ||||||||||||||
| 176 | 3 | ); | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | |||||||||||||||
| 180 | 2 | ▶ | sub route_request { | ||||||||||||
|
Subroutine
route_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 181 | 2 | state $argcheck = compile( | |||||||||||||
| 182 | Object, | ||||||||||||||
| 183 | Dict[ | ||||||||||||||
| 184 | region_code => RegionCode, | ||||||||||||||
| 185 | routing_type => RouteType, | ||||||||||||||
| 186 | address => Address, | ||||||||||||||
| 187 | origin_country_code => CountryCode, | ||||||||||||||
| 188 | message_reference => Optional[Str], | ||||||||||||||
| 189 | ], | ||||||||||||||
| 190 | ); | ||||||||||||||
| 191 | 2 | my ($self,$args) = $argcheck->(@_); | |||||||||||||
| 192 | |||||||||||||||
| 193 | my $req = { | ||||||||||||||
| 194 | RequestType => $args->{routing_type}, | ||||||||||||||
| 195 | RegionCode => $args->{region_code}, | ||||||||||||||
| 196 | OriginCountryCode => $args->{origin_country_code}, | ||||||||||||||
| 197 | 2 | %{$args->{address}->as_hash('route')}, | |||||||||||||
| 198 | schemaVersion => '1.0', | ||||||||||||||
| 199 | }; | ||||||||||||||
| 200 | |||||||||||||||
| 201 | return $self->xml_request({ | ||||||||||||||
| 202 | data => $req, | ||||||||||||||
| 203 | request_method => 'RouteRequest', | ||||||||||||||
| 204 | _mr($args), | ||||||||||||||
| 205 | })->then( | ||||||||||||||
| 206 | 1 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 207 | 1 | my ($response) = @_; | |||||||||||||
| 208 | 1 | return Future->wrap($response); | |||||||||||||
| 209 | }, | ||||||||||||||
| 210 | 2 | ); | |||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | |||||||||||||||
| 214 | 4 | my %request_type_map = ( | |||||||||||||
| 215 | GetCapability => ['GetCapability','DCTRequest','DCTResponse'], | ||||||||||||||
| 216 | RouteRequest => ['','RouteRequest','RouteResponse'], | ||||||||||||||
| 217 | ); | ||||||||||||||
| 218 | |||||||||||||||
| 219 | 5 | ▶ | sub xml_request { | ||||||||||||
|
Subroutine
xml_request: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 220 | 5 | state $argcheck = compile( | |||||||||||||
| 221 | Object, | ||||||||||||||
| 222 | Dict[ | ||||||||||||||
| 223 | data => HashRef, | ||||||||||||||
| 224 | request_method => Str, | ||||||||||||||
| 225 | message_time => Optional[DateTimeT->plus_coercions(DTFormat['ISO8601'])], | ||||||||||||||
| 226 | message_reference => Optional[Str], | ||||||||||||||
| 227 | ], | ||||||||||||||
| 228 | ); | ||||||||||||||
| 229 | 5 | my ($self, $args) = $argcheck->(@_); | |||||||||||||
| 230 | |||||||||||||||
| 231 | my ($top_level_elemel,$req_type,$res_type) = | ||||||||||||||
| 232 | 5 | ▶ | @{ $request_type_map{$args->{request_method}} || [] }; | ||||||||||||
|
Condition50%1/2
$request_type_map{$args->{'request_method'}} || []
MC/DC0%0/1
$request_type_map{$args->{'request_method'}} || []
$request_type_map{$args->{'request_method'}}
Truth table50%1/2
A
A= $request_type_map{$args->{'request_method'}}
| |||||||||||||||
| 233 | |||||||||||||||
| 234 | $args->{message_time} = $args->{message_time} | ||||||||||||||
| 235 | 5 | ▶ | ? $args->{message_time}->clone->set_time_zone('UTC') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 236 | : DateTime->now(time_zone => 'UTC'); | ||||||||||||||
| 237 | |||||||||||||||
| 238 | 5 | my $doc = XML::LibXML::Document->new('1.0','utf-8'); | |||||||||||||
| 239 | |||||||||||||||
| 240 | 5 | my $writer = $self->_xml_cache->writer("{http://www.dhl.com}$req_type"); | |||||||||||||
| 241 | |||||||||||||||
| 242 | my $req = { | ||||||||||||||
| 243 | Request => { | ||||||||||||||
| 244 | ServiceHeader => { | ||||||||||||||
| 245 | MessageTime => $args->{message_time}->iso8601, | ||||||||||||||
| 246 | SiteID => $self->username, | ||||||||||||||
| 247 | Password => $self->password, | ||||||||||||||
| 248 | MessageReference => (sprintf '% 28s',($args->{message_reference} // time())), | ||||||||||||||
| 249 | }, | ||||||||||||||
| 250 | }, | ||||||||||||||
| 251 | 5 | ▶ | %{$args->{data}}, | ||||||||||||
|
Condition50%1/2
$args->{"message_reference"} // time
MC/DC0%0/1
$args->{"message_reference"} // time
$args->{"message_reference"}
Truth table50%1/2
A
A= $args->{"message_reference"}
| |||||||||||||||
| 252 | }; | ||||||||||||||
| 253 | |||||||||||||||
| 254 | 5 | ▶ | if ($top_level_elemel) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 255 | 3 | $req = { $top_level_elemel => $req }; | |||||||||||||
| 256 | } | ||||||||||||||
| 257 | |||||||||||||||
| 258 | 5 | my $docElem = $writer->($doc,$req); | |||||||||||||
| 259 | 5 | $doc->setDocumentElement($docElem); | |||||||||||||
| 260 | |||||||||||||||
| 261 | 5 | my $request = $doc->toString(1); | |||||||||||||
| 262 | |||||||||||||||
| 263 | return $self->post( $self->base_url, $request )->then( | ||||||||||||||
| 264 | 5 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 265 | 5 | my ($response_string) = @_; | |||||||||||||
| 266 | |||||||||||||||
| 267 | 5 | my $response_doc = XML::LibXML->load_xml( | |||||||||||||
| 268 | string=>\$response_string, | ||||||||||||||
| 269 | load_ext_dtd => 0, | ||||||||||||||
| 270 | expand_xincludes => 0, | ||||||||||||||
| 271 | no_network => 1, | ||||||||||||||
| 272 | ); | ||||||||||||||
| 273 | |||||||||||||||
| 274 | 5 | my $type = type_of_node $response_doc->documentElement; | |||||||||||||
| 275 | |||||||||||||||
| 276 | 5 | my $reader = $self->_xml_cache->reader($type); | |||||||||||||
| 277 | 5 | my $response = $reader->($response_doc); | |||||||||||||
| 278 | |||||||||||||||
| 279 | 5 | ▶ | if ($response_doc->documentElement->nodeName =~ /Error/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 280 | return Future->new->fail( | ||||||||||||||
| 281 | Net::Async::Webservice::DHL::Exception::DHLError->new({ | ||||||||||||||
| 282 | error => $response->{Response}{Status} | ||||||||||||||
| 283 | 1 | }), | |||||||||||||
| 284 | 'dhl', | ||||||||||||||
| 285 | ); | ||||||||||||||
| 286 | } | ||||||||||||||
| 287 | else { | ||||||||||||||
| 288 | 4 | return Future->wrap($response); | |||||||||||||
| 289 | } | ||||||||||||||
| 290 | } | ||||||||||||||
| 291 | 5 | ); | |||||||||||||
| 292 | } | ||||||||||||||
| 293 | |||||||||||||||
| 294 | |||||||||||||||
| 295 | 4 | with 'Net::Async::Webservice::Common::WithRequestWrapper'; | |||||||||||||
| 296 | |||||||||||||||
| 297 | 4 | 1; | |||||||||||||
| 298 | |||||||||||||||
| 299 | __END__ | ||||||||||||||