| 1 | package oEdtk::ECM; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ECM injector, ported from the fork's oEUser::ECM at S22b (decision D22): upload | ||||||||||||||||||||||||||||
| 4 | # a document to the ECM API, or stamp the mailed date on an existing document. The | ||||||||||||||||||||||||||||
| 5 | # fork pinned a corporate CA and disabled host-name verification inline here; that | ||||||||||||||||||||||||||||
| 6 | # is now the shared, safe-by-default transport of oEdtk::Net, and every corporate | ||||||||||||||||||||||||||||
| 7 | # value (URL, API key) is read from the configuration. The oEUser::ECM name stays | ||||||||||||||||||||||||||||
| 8 | # available as a delegate for the fork's applications. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Configuration keys: | ||||||||||||||||||||||||||||
| 11 | # APIECM_URL base URL of the ECM API | ||||||||||||||||||||||||||||
| 12 | # APIECM_APIKEY_EDITIQUE value put in the Authorization header | ||||||||||||||||||||||||||||
| 13 | # EDTK_SSL_VERIFY transport verification, ON by default (oEdtk::Net) | ||||||||||||||||||||||||||||
| 14 | # EDTK_SSL_CA_PATH optional CA bundle (oEdtk::Net) | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use HTTP::Request::Common qw(POST PUT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use HTTP::Status (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use oEdtk::Config qw(config_read); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use oEdtk::Net qw(make_ua); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | our $VERSION = 2.1129; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # The wire contract, unchanged from the fork. | ||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use constant USER_AGENT => 'oEUser'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use constant API_ROLE => 'EDITIQUE'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use constant UPLOAD_PATH => '/v1/documents/upload'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use constant DOCUMENT_PATH => '/v1/documents/%s'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use constant MAILED_STATE => 'MAILED'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use constant DATE_IN_FORMAT => '%Y%m%d'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use constant DATE_OUT_FORMAT => '%Y-%m-%dT12:%M:%S.000Z'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | use constant STATUS_CREATED => HTTP::Status::HTTP_CREATED; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use constant STATUS_OK => HTTP::Status::HTTP_OK; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # A configuration value by key; a missing or empty one is fatal and named. | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub _config ( $cfg, $key ) { | ||||||||||||||||||||||||||
|
Subroutine
_config: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 47 | 0 | my $value = $cfg->{$key}; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | croak "ECM: missing configuration key $key" unless defined $value && length $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $value and length $value
MC/DC0%0/2
defined $value and length $value
defined $value
length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||
| 49 | 0 | return $value; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Service URL for a path, with the API role query, as the fork built it. | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub _url ( $cfg, $path ) { | ||||||||||||||||||||||||||
|
Subroutine
_url: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 0 | return _config( $cfg, 'APIECM_URL' ) . $path . '?apirole=' . API_ROLE; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # The User-Agent and Authorization headers, exactly as the fork sent them. | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub _headers ($cfg) { | ||||||||||||||||||||||||||
|
Subroutine
_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | return ( | ||||||||||||||||||||||||||||
| 60 | 0 | 'User-Agent' => USER_AGENT, | |||||||||||||||||||||||||||
| 61 | 'Authorization' => _config( $cfg, 'APIECM_APIKEY_EDITIQUE' ), | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Upload a document to the ECM. | ||||||||||||||||||||||||||||
| 66 | # $file_url path of the file to send; its base name becomes the ECM file name | ||||||||||||||||||||||||||||
| 67 | # $metadatas hashref of index metadata, sent as the JSON "text" form part | ||||||||||||||||||||||||||||
| 68 | # $cfg optional configuration hashref (defaults to config_read()) | ||||||||||||||||||||||||||||
| 69 | # Returns the decoded JSON response; croaks on a non-201 status. | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub send_document ( $file_url, $metadatas, $cfg = undef ) { | ||||||||||||||||||||||||||
|
Subroutine
send_document: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $cfg //= config_read(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$cfg //= config_read()
MC/DC0%0/2
$cfg //= config_read()
$cfg
config_read()
Truth table0%0/3
A //= B
A= $cfgB= config_read()
| |||||||||||||||||||||||||||||
| 72 | 0 | my $filedata = _get_file_content($file_url); | |||||||||||||||||||||||||||
| 73 | 0 | my $filename = basename($file_url); | |||||||||||||||||||||||||||
| 74 | 0 | my $req = POST _url( $cfg, UPLOAD_PATH ), _headers($cfg), | |||||||||||||||||||||||||||
| 75 | Content_Type => 'form-data', | ||||||||||||||||||||||||||||
| 76 | Content => [ | ||||||||||||||||||||||||||||
| 77 | 'filepdf' => [ undef, $filename, Content => $filedata ], | ||||||||||||||||||||||||||||
| 78 | 'text' => JSON::encode_json($metadatas), | ||||||||||||||||||||||||||||
| 79 | ]; | ||||||||||||||||||||||||||||
| 80 | 0 | my $res = make_ua($cfg)->request($req); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | croak 'ECM upload failed: ' . $res->content unless $res->code == STATUS_CREATED; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | return JSON::decode_json( $res->content ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Stamp an existing ECM document as mailed on a date. | ||||||||||||||||||||||||||||
| 86 | # $docid ECM document id | ||||||||||||||||||||||||||||
| 87 | # $date mailing date, "YYYYMMDD"; the fork's fixed 12:MM:SS wire form is kept | ||||||||||||||||||||||||||||
| 88 | # $cfg optional configuration hashref (defaults to config_read()) | ||||||||||||||||||||||||||||
| 89 | # Returns the decoded JSON response; croaks on a non-200 status. | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub http_put_envoidate_for_iddoc ( $docid, $date, $cfg = undef ) { | ||||||||||||||||||||||||||
|
Subroutine
http_put_envoidate_for_iddoc: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $cfg //= config_read(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$cfg //= config_read()
MC/DC0%0/2
$cfg //= config_read()
$cfg
config_read()
Truth table0%0/3
A //= B
A= $cfgB= config_read()
| |||||||||||||||||||||||||||||
| 92 | 0 | my $dateiso = Time::Piece->strptime( $date, DATE_IN_FORMAT )->strftime(DATE_OUT_FORMAT); | |||||||||||||||||||||||||||
| 93 | 0 | my $body = JSON::encode_json( { last_state => MAILED_STATE, envoi_date => $dateiso } ); | |||||||||||||||||||||||||||
| 94 | 0 | my $req = PUT _url( $cfg, sprintf( DOCUMENT_PATH, $docid ) ), _headers($cfg), Content => $body; | |||||||||||||||||||||||||||
| 95 | 0 | my $res = make_ua($cfg)->request($req); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | croak 'ECM mailed-date update failed: ' . $res->content unless $res->code == STATUS_OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | return JSON::decode_json( $res->content ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Whole-file slurp (the fork read it in one go with "local $RS"). | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _get_file_content ($filename_arg) { | ||||||||||||||||||||||||||
|
Subroutine
_get_file_content: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | open my $fh, '<', $filename_arg or croak "ECM: cannot open file $filename_arg"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | my $contents = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | close $fh or croak "ECM: cannot close file $filename_arg"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | return $contents; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 NAME | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | oEdtk::ECM - inject documents into the ECM and stamp their mailed date | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | use oEdtk::ECM; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | my $doc = oEdtk::ECM::send_document( $file, { id => 42 } ); # => decoded JSON | ||||||||||||||||||||||||||||
| 119 | oEdtk::ECM::http_put_envoidate_for_iddoc( $doc->{iDoc}, '20260113' ); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Two calls against the ECM API, ported unchanged from the fork's | ||||||||||||||||||||||||||||
| 124 | C<oEUser::ECM> (decision D22): | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =over 4 | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item * | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | C<send_document> uploads a file, with its index metadata as a JSON "text" form | ||||||||||||||||||||||||||||
| 131 | part, to C</v1/documents/upload?apirole=EDITIQUE> and returns the decoded JSON of | ||||||||||||||||||||||||||||
| 132 | the C<201> response. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =item * | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | C<http_put_envoidate_for_iddoc> stamps the C<MAILED> state and an C<envoi_date> on | ||||||||||||||||||||||||||||
| 137 | an existing document via C<PUT /v1/documents/I<id>?apirole=EDITIQUE>, and returns | ||||||||||||||||||||||||||||
| 138 | the decoded JSON of the C<200> response. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =back | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Both authenticate with a static API key, not a token: the C<Authorization> header | ||||||||||||||||||||||||||||
| 143 | is C<APIECM_APIKEY_EDITIQUE> verbatim. The base URL is C<APIECM_URL>; transport | ||||||||||||||||||||||||||||
| 144 | security comes from L<oEdtk::Net> (verification ON by default). Each function | ||||||||||||||||||||||||||||
| 145 | takes an optional trailing configuration hashref -- the fork's two-argument form | ||||||||||||||||||||||||||||
| 146 | still works, defaulting to L<oEdtk::Config/config_read>. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | The fork's own name L<oEUser::ECM> remains available as a delegate to this module. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 send_document | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | my $response = send_document($file_url, \%metadatas, $cfg); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Upload C<$file_url> with its index metadata as a JSON form part, and return the | ||||||||||||||||||||||||||||
| 157 | decoded JSON of the C<201> response. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 http_put_envoidate_for_iddoc | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | my $response = http_put_envoidate_for_iddoc($docid, $date, $cfg); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Stamp the C<MAILED> state and the mailed date on document C<$docid>, and return | ||||||||||||||||||||||||||||
| 164 | the decoded JSON of the C<200> response. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Both calls take an optional trailing configuration hash reference, defaulting to | ||||||||||||||||||||||||||||
| 167 | L<oEdtk::Config/config_read>. The internal helpers C<_config>, C<_url>, | ||||||||||||||||||||||||||||
| 168 | C<_headers> and C<_get_file_content> are not part of the public API. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | The oEdtk team. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||