Perl: download La chiamata usa la funzione download_document use v5.10; use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common; use JSON::XS qw(decode_json encode_json);
my $SERVER_URL
= 'https://docubank.kpnqwest.it/backend/v1.0/public/script/'; my $INTERFACE_CODE = 'INTERFACE_code'; my $INTERFACE_PASSWORD = 'INTERFACE_pass'; my $USER_LOGIN = 'user@acme.it'; my $USER_PASSWORD = 'user_pass'; #my $USER_TOKEN = 'fd8dfdfndfjhv87'; my $ORGANIZATION_CODE = 'TestOrg'; my $DOCUMENT_ID = '3b7be077be65dcaa2417cf63baa0aaba'; my $PATH_TO_FILE = 'path/to/file.pdf';
my $user_agent = LWP::UserAgent->new;
my $data = { 'message_type' => 'DOWNLOAD_DOCUMENT', 'version' => '1.0', 'authentication' => { 'interface_code' => $INTERFACE_CODE, 'interface_password' => $INTERFACE_PASSWORD, 'login' => $USER_LOGIN, 'password' => $USER_PASSWORD, # 'token' => $USER_TOKEN, }, 'organization_code' => $ORGANIZATION_CODE, 'document_id' => $DOCUMENT_ID, };
my $response = $user_agent->request( POST $SERVER_URL, 'Content_Type' => 'application/json', 'Content' => encode_json($data) );
if ( $response->is_success ) { open( my $pdf_fh, '>', $PATH_TO_FILE ) or
die "Can't open file $PATH_TO_FILE: $!"; print {$pdf_fh} $response->decoded_content; close $pdf_fh;
print "File in $PATH_TO_FILE\n"; } else { my $parsed_response = decode_json( $response->decoded_content );
given ( $parsed_response->{'status'} ) { when ('REQUIRED_CAPABILITY_NOT_AVAILABLE') { # ... } when ('DOCUMENT_NOT_FOUND') { # ... } when ('DOCUMENT_DELETED') { # ... } when ('DOCUMENT_NOT_VISIBLE_TO_USER') { # ... } when ('DOCUMENT_NOT_YET_ARCHIVED') { # ... } when ('DOCUMENT_NOT_ARCHIVED_DUE_TO_ERROR') { # ... } when ('ORGANIZATION_NOT_FOUND') { # ... } when ('BAD_INTERFACE_CODE_PASSWORD') { # ... } when ('USER_AUTHENTICATION_FAILURE') { # ... } when ('BACKEND_SERVER_ERROR') { # ... } default { die 'Should not arrive in here' } } } |