| 1 | # Copyright (C) 2017 Koha-Suomi | ||
| 2 | # | ||
| 3 | # This file is part of Pootle-Client. | ||
| 4 | |||
| 5 | package Pootle::Agent; | ||
| 6 | |||
| 7 | 5 | ▶ | use Modern::Perl '2015'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 5 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | binmode STDOUT, ':encoding(UTF-8)'; | ||
| 10 | binmode STDERR, ':encoding(UTF-8)'; | ||
| 11 | 5 | ▶ | use feature 'signatures'; no warnings "experimental::signatures"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 5 | ▶ | use Carp::Always; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 5 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 5 | ▶ | use Scalar::Util qw(blessed); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | |||
| 16 | =head2 Pootle::Agent | ||
| 17 | |||
| 18 | LWP::Curl wrapper to deal with various types of exceptions transparently | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | 5 | ▶ | use Params::Validate qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 5 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 5 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 5 | ▶ | use MIME::Base64; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 5 | ▶ | use JSON::XS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 0 | ▶ | use File::Slurp; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 28 | |||
| 29 | use Pootle::Logger; | ||
| 30 | my $l = bless({}, 'Pootle::Logger'); #Lazy load package logger this way to avoid circular dependency issues with logger includes from many packages | ||
| 31 | |||
| 32 | use Pootle::Exception; | ||
| 33 | use Pootle::Exception::HTTP::MethodNotAllowed; | ||
| 34 | use Pootle::Exception::HTTP::NotFound; | ||
| 35 | use Pootle::Exception::Credentials; | ||
| 36 | |||
| 37 | sub new($class, @params) { | ||
| 38 | $l->debug("Initializing '$class' with parameters: ".$l->flatten(@params)) if $l->is_debug(); | ||
| 39 | my %self = validate(@params, { | ||
| 40 | baseUrl => 1, | ||
| 41 | credentials => 1, | ||
| 42 | }); | ||
| 43 | my $s = bless(\%self, $class); | ||
| 44 | |||
| 45 | $s->{credentials} = $s->_loadCredentials(); | ||
| 46 | |||
| 47 | $s->{ua} = LWP::UserAgent->new( | ||
| 48 | default_headers => HTTP::Headers->new(Authorization => $s->_authorization()), | ||
| 49 | ); | ||
| 50 | |||
| 51 | return $s; | ||
| 52 | } | ||
| 53 | |||
| 54 | =head2 _authorization | ||
| 55 | |||
| 56 | @RETURNS HTTP Basic authorization header content, eg. 'Basic QWxhZGRpbjpPcGVuU2VzYW1l' | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub _authorization($s) { | ||
| 61 | return 'Basic '.MIME::Base64::encode(Encode::encode('UTF-8', $s->credentials()), ''); #Turn $credentials into a byte/octet stream, and encode that as base64, with no eol | ||
| 62 | } | ||
| 63 | |||
| 64 | =head2 request | ||
| 65 | |||
| 66 | Make requests and deal with logging and error handling | ||
| 67 | |||
| 68 | @RETURNS List of 0 - HTTP::Response | ||
| 69 | 1 - HASHRef of response JSON payload | ||
| 70 | @THROWS Pootle::Exception::HTTP::MethodNotAllowed endpoint doesn't support the given method | ||
| 71 | @THROWS Pootle::Exception::HTTP::NotFound endpoint not found? | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub request($s, $verb, $apiUrl, $params) { | ||
| 76 | my $response = $s->ua->$verb($s->baseUrl.'/'.$apiUrl); | ||
| 77 | my $contentHash; | ||
| 78 | try { | ||
| 79 | $contentHash = $s->_getContent($response); | ||
| 80 | $l->trace("\$response: ".$s->_httpResponseToLoggableFromSuccess($response, $contentHash)) if $l->is_trace(); | ||
| 81 | } catch { | ||
| 82 | if ($_ =~ /^malformed JSON string/) { #Presumably this is a JSON::XS issue | ||
| 83 | my $errorStr = $s->_httpResponseToLoggableFromFail($response); | ||
| 84 | $l->trace("\$response: ".$errorStr) if $l->is_trace(); | ||
| 85 | Pootle::Exception::HTTP::MethodNotAllowed->throw(error => $errorStr) if $errorStr =~ /405 METHOD NOT ALLOWED$/sm; | ||
| 86 | Pootle::Exception::HTTP::NotFound->throw(error => $errorStr) if $errorStr =~ /404 Not Found$/sm; | ||
| 87 | Pootle::Exception::rethrowDefaults($errorStr); | ||
| 88 | } | ||
| 89 | Pootle::Exception::rethrowDefaults($_); | ||
| 90 | }; | ||
| 91 | return ($response, $contentHash); | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 _getContent | ||
| 95 | |||
| 96 | @RETURNS HASHRef, Content's JSON payload decoded to Perl's internal UTF-8 representation | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub _getContent($s, $response) { | ||
| 101 | my $content = $response->content(); | ||
| 102 | return JSON::XS->new->utf8->decode($content); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _httpResponseToLoggableFromSuccess($s, $response, $contentHash) { | ||
| 106 | return join("\n", | ||
| 107 | $s->_httpResponseToLoggableHeader($response), | ||
| 108 | scalar(Data::Dumper->new([$contentHash],[])->Terse(1)->Indent(1)->Varname('')->Maxdepth(0)->Sortkeys(1)->Quotekeys(1)->Dump()), | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _httpResponseToLoggableFromFail($s, $response) { | ||
| 113 | return join("\n", | ||
| 114 | $s->_httpResponseToLoggableHeader($response), | ||
| 115 | $response->content(), | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _httpResponseToLoggableHeader($s, $response) { | ||
| 120 | my $status_line = $response->status_line; | ||
| 121 | my $proto = $response->protocol; | ||
| 122 | $status_line = "$proto $status_line" if $proto; | ||
| 123 | return join("\n", $status_line, $response->headers_as_string("\n"),''); #Includes empty line to signal the start of HTTP payload | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _loadCredentials($s) { | ||
| 127 | my $c = $s->credentials(); | ||
| 128 | my $credentialsConfirmed; | ||
| 129 | my $file; | ||
| 130 | if (-e $c) { #This is a file | ||
| 131 | $file = $c; | ||
| 132 | $l->info("Loading credentials from file '$c'"); | ||
| 133 | my @rows = File::Slurp::read_file( $c => { binmode => ':encoding(UTF-8)' } ); | ||
| 134 | foreach my $row (@rows) { | ||
| 135 | if ($row =~ /^(.+):(.+)$/) { | ||
| 136 | $credentialsConfirmed = "$1:$2"; | ||
| 137 | } | ||
| 138 | last; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | $credentialsConfirmed = $c; | ||
| 143 | } | ||
| 144 | |||
| 145 | unless ($credentialsConfirmed && $credentialsConfirmed =~ /^(.+):(.+)$/) { | ||
| 146 | Pootle::Exception::Credentials->throw(error => "_loadCredentials():> Given credentials ".($file ? "from file '$file' " : "")."are malformed. Credentials must look like username:password, or point to a file with properly formatted credentials."); | ||
| 147 | } | ||
| 148 | return $credentialsConfirmed; | ||
| 149 | } | ||
| 150 | |||
| 151 | ########## ### ### | ||
| 152 | ## ACCESSORS ### ### | ||
| 153 | ########## ### ### | ||
| 154 | |||
| 155 | =head2 baseUrl | ||
| 156 | |||
| 157 | @RETURNS String, the full url of the Pootle server we are interfacing with, eg. https://translate.koha-community.org | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub baseUrl($s) { | ||
| 162 | return $s->{baseUrl}; | ||
| 163 | } | ||
| 164 | |||
| 165 | =head2 credentials | ||
| 166 | |||
| 167 | @RETURNS String, username:password | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub credentials($s) { | ||
| 172 | return $s->{credentials}; | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 ua | ||
| 176 | |||
| 177 | @RETURNS L<LWP::UserAgent> | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub ua($s) { return $s->{ua} } | ||
| 182 | |||
| 183 | 1; | ||