| 1 | package HTTP::API::Client; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 78 | $HTTP::API::Client::VERSION = '1.23'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 78 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | HTTP::API::Client - lightweight HTTP/REST client with per-request signing | ||||||||||||||||||||||||||||||||||||||||||
| 8 | hooks, retry-with-backoff, and JSON/form encoding | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | Talking to an authenticated JSON or form-urlencoded REST API with plain | ||||||||||||||||||||||||||||||||||||||||||
| 13 | L<LWP::UserAgent> usually means the same boilerplate on every call: compute | ||||||||||||||||||||||||||||||||||||||||||
| 14 | a signature or auth header from the request's own data, remember not to | ||||||||||||||||||||||||||||||||||||||||||
| 15 | leak the signing secret into the body, retry on a flaky response, and | ||||||||||||||||||||||||||||||||||||||||||
| 16 | serialize Perl values (which have no native boolean and no native | ||||||||||||||||||||||||||||||||||||||||||
| 17 | comma-list) unambiguously. C<HTTP::API::Client> is that boilerplate, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | written once - a thin L<LWP::UserAgent> wrapper with: | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =item * An event/callback system (see L</"new_request(%options)">) that computes | ||||||||||||||||||||||||||||||||||||||||||
| 23 | header or data values from the rest of the request at build time - the | ||||||||||||||||||||||||||||||||||||||||||
| 24 | mechanism for a signed-request header, not a special case bolted on top | ||||||||||||||||||||||||||||||||||||||||||
| 25 | of it. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =item * Retry-with-backoff on failed responses, configurable per status | ||||||||||||||||||||||||||||||||||||||||||
| 28 | code (see L</"ENVIRONMENT VARIABLES">). | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =item * JSON and form-urlencoded body encoding from the same C<%data> | ||||||||||||||||||||||||||||||||||||||||||
| 31 | hash, including L<HTTP::API::DataTypeMarker>'s C<xTRUE>/C<xCSV>-family | ||||||||||||||||||||||||||||||||||||||||||
| 32 | markers for the values Perl scalars can't represent unambiguously on | ||||||||||||||||||||||||||||||||||||||||||
| 33 | their own. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | If none of that boilerplate applies to what you're calling - no signing, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | no retry policy, plain data - reaching for L<LWP::UserAgent> or | ||||||||||||||||||||||||||||||||||||||||||
| 39 | L<HTTP::Tiny> directly is simpler. This module earns its weight | ||||||||||||||||||||||||||||||||||||||||||
| 40 | specifically for authenticated API clients that would otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 41 | reimplement the same header-signing/retry logic by hand. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =head1 USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | use HTTP::API::Client; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | The constructor takes no required arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | my $ua = HTTP::API::Client->new( | ||||||||||||||||||||||||||||||||||||||||||
| 50 | base_url => "https://api.example.com", | ||||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =head2 A signed request | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | This is the case the module exists for: compute an auth header from the | ||||||||||||||||||||||||||||||||||||||||||
| 56 | request's own data, once, at construction - every call this client makes | ||||||||||||||||||||||||||||||||||||||||||
| 57 | carries it automatically, and the secret used to compute it never appears | ||||||||||||||||||||||||||||||||||||||||||
| 58 | in the request itself. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | my $ua = HTTP::API::Client->new( | ||||||||||||||||||||||||||||||||||||||||||
| 61 | base_url => "https://api.example.com", | ||||||||||||||||||||||||||||||||||||||||||
| 62 | pre_defined_data => { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | api_key => "AKIA123", | ||||||||||||||||||||||||||||||||||||||||||
| 64 | api_secret => sub { "s3cr3t" }, # or read from config/env | ||||||||||||||||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | pre_defined_headers => { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | APIKEY => sub { my (undef, %o) = @_; $o{data}{api_key} }, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | Signature => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | my (undef, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 70 | "$o{data}{api_key}:$o{data}{api_secret}"; # a real HMAC in practice | ||||||||||||||||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | pre_defined_events => { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | not_include => { api_secret => 1 }, # keep the secret out of the body/query | ||||||||||||||||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | $ua->get("/search", { q => "widgets" }); | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # -> GET https://api.example.com/search?api_key=AKIA123&q=widgets | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # APIKEY: AKIA123 | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # Signature: AKIA123:s3cr3t | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | See L</"new_request(%options)"> for the full list of build-time hooks this uses. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =head2 Shorthand methods | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | C<get>/C<post>/C<put>/C<head>/C<delete> are shorthand over C<send()>: | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | $ua->get($url); # same as $ua->send( GET, $url ) | ||||||||||||||||||||||||||||||||||||||||||
| 90 | $ua->post($url, \%data, \%headers); # same as $ua->send( POST, $url, \%data, \%headers ) | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =head2 JSON in, JSON out | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | my $ua = HTTP::API::Client->new(base_url => "http://example.com"); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | $ua->get("/search", { q => "something" }); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | my $decoded = $ua->json_response; # the response body, JSON-decoded | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =head2 Form-urlencoded | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | my $ua = HTTP::API::Client->new(content_type => "application/x-www-form-urlencoded"); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | $ua->post("http://example.com", { q => "something" }); | ||||||||||||||||||||||||||||||||||||||||||
| 102 | my $response = $ua->last_response; # an HTTP::Response object | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | Body encoding only knows how to serialize into JSON or a query string | ||||||||||||||||||||||||||||||||||||||||||
| 105 | (see C<convert_data()> below) - any other content type must be built and | ||||||||||||||||||||||||||||||||||||||||||
| 106 | passed in already-encoded. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | All constructor arguments below are also read-write accessors | ||||||||||||||||||||||||||||||||||||||||||
| 111 | (C<< $ua->base_url("http://new-host") >>), except C<engine> which is | ||||||||||||||||||||||||||||||||||||||||||
| 112 | read-only after construction. Several fall back to an environment | ||||||||||||||||||||||||||||||||||||||||||
| 113 | variable (see L</"ENVIRONMENT VARIABLES">) when not passed explicitly. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =item base_url | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | Prefixed onto every C<$path> passed to C<send()> (and the shorthand | ||||||||||||||||||||||||||||||||||||||||||
| 120 | methods). Not set by default - a bare path is used as-is. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =item username / password | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | HTTP Basic auth credentials. If either is set, Basic auth is used and | ||||||||||||||||||||||||||||||||||||||||||
| 125 | C<auth_token> is ignored. Default from C<HTTP_USERNAME>/C<HTTP_PASSWORD>. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =item auth_token | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | Sent verbatim as the C<Authorization> header when C<username>/C<password> | ||||||||||||||||||||||||||||||||||||||||||
| 130 | are both unset. Default from C<HTTP_AUTH_TOKEN>. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =item content_type | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | Forces the request content type. Left unset, GET defaults to | ||||||||||||||||||||||||||||||||||||||||||
| 135 | C<application/x-www-form-urlencoded> and every other method defaults to | ||||||||||||||||||||||||||||||||||||||||||
| 136 | C<application/json; charset=$charset>. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | =item charset | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | Used to build the default JSON content-type header and to decide whether | ||||||||||||||||||||||||||||||||||||||||||
| 141 | UTF-8 byte-encoding is applied to the request body. Default C<utf8>, from | ||||||||||||||||||||||||||||||||||||||||||
| 142 | C<HTTP_CHARSET>. Must be a real L<JSON::XS> charset method name (C<utf8>, | ||||||||||||||||||||||||||||||||||||||||||
| 143 | C<latin1>, C<ascii>, ...) - an invalid value dies immediately and clearly, | ||||||||||||||||||||||||||||||||||||||||||
| 144 | naming the bad value, the next time C<kvp2json()> is called, whether or | ||||||||||||||||||||||||||||||||||||||||||
| 145 | not C<json> has already been built. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =item timeout | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Request timeout in seconds, passed to the underlying L<LWP::UserAgent>. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | Default C<60>, from C<HTTP_TIMEOUT>. Re-applied to C<ua> at the start of | ||||||||||||||||||||||||||||||||||||||||||
| 151 | every C<send()> call, so changing it between calls takes effect on the | ||||||||||||||||||||||||||||||||||||||||||
| 152 | next one, not just at construction. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =item ssl_verify | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | Passed through as C<verify_hostname> to L<LWP::UserAgent>'s C<ssl_opts>. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | Default off (C<0>), from C<SSL_VERIFY>. Re-applied to C<ua> at the start | ||||||||||||||||||||||||||||||||||||||||||
| 158 | of every C<send()> call, so changing it between calls takes effect on | ||||||||||||||||||||||||||||||||||||||||||
| 159 | the next one, not just at construction. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =item pre_defined_data / pre_defined_headers / pre_defined_events | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | Hashrefs merged underneath the C<%data>/C<%headers>/C<%events> passed to | ||||||||||||||||||||||||||||||||||||||||||
| 164 | each individual call - set once at construction time for values every | ||||||||||||||||||||||||||||||||||||||||||
| 165 | request should carry (an API key, a fixed header, a standing callback), | ||||||||||||||||||||||||||||||||||||||||||
| 166 | then override per-call as needed. | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | =item engine | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | Read-only. Defaults to C<"LWP::UserAgent">, the only engine C<send()> | ||||||||||||||||||||||||||||||||||||||||||
| 171 | actually dispatches requests through - C<_build_ua> will call a | ||||||||||||||||||||||||||||||||||||||||||
| 172 | same-named method on a subclass to build an alternate UA object, but | ||||||||||||||||||||||||||||||||||||||||||
| 173 | C<send()> itself does not yet know how to use anything other than | ||||||||||||||||||||||||||||||||||||||||||
| 174 | L<LWP::UserAgent> with it (see C<t/12_unsupported_engine.t>: setting a | ||||||||||||||||||||||||||||||||||||||||||
| 175 | different engine fails with a clear error rather than silently doing | ||||||||||||||||||||||||||||||||||||||||||
| 176 | nothing). | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =item last_response | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | Read-write. The most recent L<HTTP::Response>, set by C<send()> and read by | ||||||||||||||||||||||||||||||||||||||||||
| 181 | L</json_response>/L</kvp_response>. C<undef> until the first request. | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | =item ua | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | Read-write. The underlying user-agent object C<send()> dispatches through | ||||||||||||||||||||||||||||||||||||||||||
| 186 | - an L<LWP::UserAgent> by default, built lazily by C<_build_ua> on first | ||||||||||||||||||||||||||||||||||||||||||
| 187 | use, then reused for every subsequent call. Set this directly to inject | ||||||||||||||||||||||||||||||||||||||||||
| 188 | a stand-in (a fake, a mock, a pre-configured instance) instead of the | ||||||||||||||||||||||||||||||||||||||||||
| 189 | real one; this is how the test suite avoids making real network calls. | ||||||||||||||||||||||||||||||||||||||||||
| 190 | C<browser_id>/C<timeout>/C<ssl_verify> are re-applied to it (via | ||||||||||||||||||||||||||||||||||||||||||
| 191 | C<< $ua->agent >>/C<< $ua->timeout >>/C<< $ua->ssl_opts >>, each only if | ||||||||||||||||||||||||||||||||||||||||||
| 192 | C<ua> supports that method) at the start of every C<send()> call, not | ||||||||||||||||||||||||||||||||||||||||||
| 193 | just when it's first built. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =item browser_id | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | Read-write. The C<User-Agent> header value passed to the underlying | ||||||||||||||||||||||||||||||||||||||||||
| 198 | C<ua>. Defaults to C<"HTTP API Client v$VERSION"> - C<$VERSION> is only | ||||||||||||||||||||||||||||||||||||||||||
| 199 | set when running the installed CPAN release (Dist::Zilla's | ||||||||||||||||||||||||||||||||||||||||||
| 200 | C<[PkgVersion]> plugin injects it at build time), so a development | ||||||||||||||||||||||||||||||||||||||||||
| 201 | checkout reports C<"HTTP API Client vdev"> instead. Re-applied to C<ua> | ||||||||||||||||||||||||||||||||||||||||||
| 202 | at the start of every C<send()> call, so changing it between calls takes | ||||||||||||||||||||||||||||||||||||||||||
| 203 | effect on the next one, not just at construction. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =item retry_config | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | Read-write hashref C<< { fail_response => $n, fail_status => $csv, delay | ||||||||||||||||||||||||||||||||||||||||||
| 208 | => $seconds } >>. The programmatic equivalent of | ||||||||||||||||||||||||||||||||||||||||||
| 209 | C<RETRY_FAIL_RESPONSE>/C<RETRY_FAIL_STATUS>/C<RETRY_DELAY> - set this | ||||||||||||||||||||||||||||||||||||||||||
| 210 | directly to configure retry behavior at construction without touching | ||||||||||||||||||||||||||||||||||||||||||
| 211 | process environment variables. A partial hashref is fine - any key you | ||||||||||||||||||||||||||||||||||||||||||
| 212 | omit falls back to that same env var's default (C<0>/C<''>/C<5>), not to | ||||||||||||||||||||||||||||||||||||||||||
| 213 | the environment variable itself. Re-applied at the start of every | ||||||||||||||||||||||||||||||||||||||||||
| 214 | C<send()> call, so changing it between calls takes effect on the next | ||||||||||||||||||||||||||||||||||||||||||
| 215 | one, not just at construction. See L</"ENVIRONMENT VARIABLES"> for what | ||||||||||||||||||||||||||||||||||||||||||
| 216 | each key does. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =item json | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | Read-write. The L<JSON::XS> instance C<kvp2json()> encodes through and | ||||||||||||||||||||||||||||||||||||||||||
| 221 | C<json_response()> decodes through, built lazily with | ||||||||||||||||||||||||||||||||||||||||||
| 222 | C<->canonical->allow_nonref>. Set this directly to inject a | ||||||||||||||||||||||||||||||||||||||||||
| 223 | differently-configured instance (e.g. one with C<->allow_blessed> | ||||||||||||||||||||||||||||||||||||||||||
| 224 | enabled) instead of the default. C<charset> is re-applied to it (via its | ||||||||||||||||||||||||||||||||||||||||||
| 225 | C<utf8>/C<latin1>/etc method) at the start of every C<kvp2json()>/ | ||||||||||||||||||||||||||||||||||||||||||
| 226 | C<json_response()> call, not just when C<json> is first built, so a | ||||||||||||||||||||||||||||||||||||||||||
| 227 | C<charset> change takes effect on the next encode or decode even if | ||||||||||||||||||||||||||||||||||||||||||
| 228 | C<json> was already in use - this also applies to an injected custom | ||||||||||||||||||||||||||||||||||||||||||
| 229 | instance. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | =head1 ENVIRONMENT VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | These environment variables expose the controls without changing the existing code. | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | HTTP VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | HTTP_USERNAME - basic auth username | ||||||||||||||||||||||||||||||||||||||||||
| 240 | HTTP_PASSWORD - basic auth password | ||||||||||||||||||||||||||||||||||||||||||
| 241 | HTTP_AUTH_TOKEN - basic auth token string | ||||||||||||||||||||||||||||||||||||||||||
| 242 | HTTP_CHARSET - content type charset. default utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 243 | HTTP_TIMEOUT - timeout the request in seconds. default 60 seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 244 | SSL_VERIFY - verify ssl url. default is off | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | DEBUG VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | DEBUG_IN_OUT - print out request and response in string to STDERR | ||||||||||||||||||||||||||||||||||||||||||
| 249 | DEBUG_SEND_OUT - print out request in string to STDERR | ||||||||||||||||||||||||||||||||||||||||||
| 250 | DEBUG_RESPONSE - print out response in string to STDERR | ||||||||||||||||||||||||||||||||||||||||||
| 251 | DEBUG_RESPONSE_HEADER_ONLY - print out response header only without the body | ||||||||||||||||||||||||||||||||||||||||||
| 252 | DEBUG_RESPONSE_IF_FAIL - narrows DEBUG_IN_OUT/DEBUG_RESPONSE to only print | ||||||||||||||||||||||||||||||||||||||||||
| 253 | on a failed response. Does nothing by itself - | ||||||||||||||||||||||||||||||||||||||||||
| 254 | DEBUG_IN_OUT or DEBUG_RESPONSE must also be set. | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | RETRY VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | RETRY_FAIL_RESPONSE - number of time to retry if response comes back is failed. default 0 retry. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | A negative value is clamped to 0 (no retry) rather than silently | ||||||||||||||||||||||||||||||||||||||||||
| 260 | making zero attempts. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | RETRY_FAIL_STATUS - only retry if specified status code. e.g. 500,404 | ||||||||||||||||||||||||||||||||||||||||||
| 262 | An empty entry (a doubled comma, stray whitespace) is | ||||||||||||||||||||||||||||||||||||||||||
| 263 | skipped rather than becoming a bogus always-false entry. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | RETRY_DELAY - retry with wait time of 5 seconds in between, by default. A negative | ||||||||||||||||||||||||||||||||||||||||||
| 265 | value is clamped to 0 rather than reaching sleep() as-is. | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | =head2 get / post / put / head / delete ($path, \%data, \%headers, \%events) | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | Shorthand for C<send($METHOD, $path, \%data, \%headers, \%events)>. C<$path> | ||||||||||||||||||||||||||||||||||||||||||
| 272 | is appended to the C<base_url> attribute if one was given. Returns whatever | ||||||||||||||||||||||||||||||||||||||||||
| 273 | C<send()> returns - normally an L<HTTP::Response>. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | =head2 send($method, $path, \%data, \%headers, \%events) | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | Builds and sends one HTTP request, retrying per L</"ENVIRONMENT VARIABLES"> | ||||||||||||||||||||||||||||||||||||||||||
| 278 | if configured. C<%data> and C<%headers> are merged over C<pre_defined_data> | ||||||||||||||||||||||||||||||||||||||||||
| 279 | and C<pre_defined_headers>. C<%events> are the per-call callbacks documented | ||||||||||||||||||||||||||||||||||||||||||
| 280 | under C<new_request()> below. Sets and returns the C<last_response> | ||||||||||||||||||||||||||||||||||||||||||
| 281 | attribute. | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | None of C<\%data>/C<\%headers>/C<\%events> are mutated - C<send()> copies | ||||||||||||||||||||||||||||||||||||||||||
| 284 | each before doing anything with it, so a hashref you pass in (and may | ||||||||||||||||||||||||||||||||||||||||||
| 285 | reuse across other calls) comes back exactly as you passed it, with no | ||||||||||||||||||||||||||||||||||||||||||
| 286 | merged-in C<pre_defined_*> keys or event-hook keys added by C<send()>'s | ||||||||||||||||||||||||||||||||||||||||||
| 287 | own machinery. | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | Passing C<< $events->{test_request_object} = 1 >> makes it return the built | ||||||||||||||||||||||||||||||||||||||||||
| 290 | L<HTTP::Request> instead of sending it - the way this module's own test | ||||||||||||||||||||||||||||||||||||||||||
| 291 | suite inspects what would have gone out. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =head2 json_response | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | Decode the C<last_response> body as JSON and return the resulting hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | Never dies - a missing response or invalid JSON comes back as | ||||||||||||||||||||||||||||||||||||||||||
| 297 | C<< { status => "error", error => $message } >> instead. C<charset> is | ||||||||||||||||||||||||||||||||||||||||||
| 298 | re-applied to C<json> at the start of every call, the same as | ||||||||||||||||||||||||||||||||||||||||||
| 299 | C<kvp2json()> does on the encode side, so a C<charset> change takes | ||||||||||||||||||||||||||||||||||||||||||
| 300 | effect on the next decode too. | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | =head2 kvp_response | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | Parse the C<last_response> body as a C<key=value&key=value> query string | ||||||||||||||||||||||||||||||||||||||||||
| 305 | and return it as a hashref. Returns C<{}> if no request has been made yet, | ||||||||||||||||||||||||||||||||||||||||||
| 306 | or the response body is empty. A key that repeats (the shape | ||||||||||||||||||||||||||||||||||||||||||
| 307 | C<kvp2str_each> produces when encoding an array-valued field) decodes to | ||||||||||||||||||||||||||||||||||||||||||
| 308 | an arrayref of every value seen, in order; a key seen once still decodes | ||||||||||||||||||||||||||||||||||||||||||
| 309 | to a plain scalar. Decodes both percent-encoding and the | ||||||||||||||||||||||||||||||||||||||||||
| 310 | C<application/x-www-form-urlencoded> convention of a literal C<+> meaning | ||||||||||||||||||||||||||||||||||||||||||
| 311 | a space (a genuinely percent-encoded literal C<+>, i.e. C<%2B>, still | ||||||||||||||||||||||||||||||||||||||||||
| 312 | decodes to C<+>) - this matters for a response body from any API, not | ||||||||||||||||||||||||||||||||||||||||||
| 313 | just one built by this module's own C<kvp2str_each>, which never emits a | ||||||||||||||||||||||||||||||||||||||||||
| 314 | raw C<+> itself. An empty C<&>-separated segment (a leading, trailing, | ||||||||||||||||||||||||||||||||||||||||||
| 315 | or doubled C<&>) is skipped rather than decoded into a bogus | ||||||||||||||||||||||||||||||||||||||||||
| 316 | C<< '' => undef >> entry. | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | =head2 new_request(%options) | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | Builds the L<HTTP::Request> for one call. This is where the C<%events> | ||||||||||||||||||||||||||||||||||||||||||
| 321 | callbacks passed to C<send()> are read: C<before_headers>, C<headers_keys> / | ||||||||||||||||||||||||||||||||||||||||||
| 322 | C<add_headers_keys> (which header keys to consider, in what order), | ||||||||||||||||||||||||||||||||||||||||||
| 323 | C<before_header>/C<after_header> keyed by header name, and | ||||||||||||||||||||||||||||||||||||||||||
| 324 | C<after_header_keys> - the hooks used to compute things like a signature | ||||||||||||||||||||||||||||||||||||||||||
| 325 | header from other data at build time. See F<t/04_callbacks.t> for a worked | ||||||||||||||||||||||||||||||||||||||||||
| 326 | example (API key + signature). | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | C<%options> also accepts C<skip_headers>, a hashref of header names to | ||||||||||||||||||||||||||||||||||||||||||
| 329 | exclude from the request no matter what C<%headers> or the events above | ||||||||||||||||||||||||||||||||||||||||||
| 330 | say. Only reachable by calling C<new_request()> directly with it - C<send()> | ||||||||||||||||||||||||||||||||||||||||||
| 331 | never sets it, and setting it inside an C<%events> callback has no effect | ||||||||||||||||||||||||||||||||||||||||||
| 332 | (a callback receives a snapshot copy of C<%options>, not the one | ||||||||||||||||||||||||||||||||||||||||||
| 333 | C<new_request()> itself is iterating). See F<t/23_skip_headers.t>. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =head2 get_content_type(%options) | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | Resolves the effective content type for one call: the C<content_type> | ||||||||||||||||||||||||||||||||||||||||||
| 338 | attribute if set explicitly; otherwise C<application/x-www-form-urlencoded> | ||||||||||||||||||||||||||||||||||||||||||
| 339 | for a GET, or C<application/json; charset=$charset> for anything else. | ||||||||||||||||||||||||||||||||||||||||||
| 340 | Called internally by C<convert_data()>, C<prepare_request()>, and | ||||||||||||||||||||||||||||||||||||||||||
| 341 | C<new_request()> - there is normally no need to call this directly. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =head2 convert_data(%options) | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | Turn C<%options>'s C<data> hashref into the request body, according to | ||||||||||||||||||||||||||||||||||||||||||
| 346 | content type: JSON via C<kvp2json()> if the content type contains C<json>, | ||||||||||||||||||||||||||||||||||||||||||
| 347 | a query string via C<kvp2str()> if it is exactly | ||||||||||||||||||||||||||||||||||||||||||
| 348 | C<application/x-www-form-urlencoded>. Any other content type returns an | ||||||||||||||||||||||||||||||||||||||||||
| 349 | empty string for empty data, or dies naming the content type - there is no | ||||||||||||||||||||||||||||||||||||||||||
| 350 | generic way to serialize arbitrary data into an arbitrary content type. | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | =head2 prepare_request(%options) | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | Builds the bare L<HTTP::Request> (method, URL, content-type header) and | ||||||||||||||||||||||||||||||||||||||||||
| 355 | applies authentication: C<username>/C<password> take priority and are sent | ||||||||||||||||||||||||||||||||||||||||||
| 356 | as HTTP Basic auth via C<basic_authenticator()>; C<auth_token> is used only | ||||||||||||||||||||||||||||||||||||||||||
| 357 | if neither is set, and is sent as a raw C<Authorization> header value | ||||||||||||||||||||||||||||||||||||||||||
| 358 | verbatim (no C<Bearer> prefix is added for you - include it in the token | ||||||||||||||||||||||||||||||||||||||||||
| 359 | itself if the API expects one). C<auth_token> only supplies a I<default> | ||||||||||||||||||||||||||||||||||||||||||
| 360 | C<Authorization> header - an explicit one already present in C<%headers> | ||||||||||||||||||||||||||||||||||||||||||
| 361 | for this call (any casing: C<Authorization>, C<authorization>, ...) is | ||||||||||||||||||||||||||||||||||||||||||
| 362 | left alone rather than overwritten. Called by C<new_request()> - there is | ||||||||||||||||||||||||||||||||||||||||||
| 363 | normally no need to call this directly. | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =head2 basic_authenticator($request, $username, $password) | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | Sets HTTP Basic auth on C<$request> via | ||||||||||||||||||||||||||||||||||||||||||
| 368 | C<< $request->headers->authorization_basic >>. Override this in a subclass | ||||||||||||||||||||||||||||||||||||||||||
| 369 | to change how basic auth is applied without touching the rest of request | ||||||||||||||||||||||||||||||||||||||||||
| 370 | building. | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | =head2 kvp2json(%options) / kvp2str(%options) | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | The two body encoders C<convert_data> dispatches to. C<kvp2json> walks | ||||||||||||||||||||||||||||||||||||||||||
| 375 | C<%options>'s C<data> hashref and JSON::XS-encodes it, resolving any | ||||||||||||||||||||||||||||||||||||||||||
| 376 | C<CODE> values by calling them and unwrapping L<HTTP::API::DataTypeMarker>'s | ||||||||||||||||||||||||||||||||||||||||||
| 377 | C<xBOOLEAN>-family markers into their JSON form; an C<xCSV>-marked value | ||||||||||||||||||||||||||||||||||||||||||
| 378 | has no special JSON handling and just JSON-encodes as an ordinary array | ||||||||||||||||||||||||||||||||||||||||||
| 379 | (there's no C<key=value&key=value> repetition problem in JSON for it to | ||||||||||||||||||||||||||||||||||||||||||
| 380 | solve). C<kvp2str> produces a C<key=value&key=value> query string instead, | ||||||||||||||||||||||||||||||||||||||||||
| 381 | with C<xCSV>-marked values joined by commas instead of repeated per key. | ||||||||||||||||||||||||||||||||||||||||||
| 382 | An empty arrayref value is omitted from C<kvp2str>'s output entirely (an | ||||||||||||||||||||||||||||||||||||||||||
| 383 | empty array has no C<key=value> representation), the same as a key | ||||||||||||||||||||||||||||||||||||||||||
| 384 | mapped to C<undef>/missing already is - including an empty array nested | ||||||||||||||||||||||||||||||||||||||||||
| 385 | inside an C<xCSV(...)> list, which contributes nothing to the joined | ||||||||||||||||||||||||||||||||||||||||||
| 386 | string rather than leaving a blank comma segment behind. An C<xCSV()> | ||||||||||||||||||||||||||||||||||||||||||
| 387 | with no elements at all is different: it still emits C<key=> (present, | ||||||||||||||||||||||||||||||||||||||||||
| 388 | empty value) rather than being omitted - there is no join-corruption | ||||||||||||||||||||||||||||||||||||||||||
| 389 | risk to avoid the way there is for a nested empty array, and C<key=> is | ||||||||||||||||||||||||||||||||||||||||||
| 390 | valid, parseable form data distinct from the key being absent. | ||||||||||||||||||||||||||||||||||||||||||
| 391 | Both respect an C<< $events->{keys} >> callback to control which keys are | ||||||||||||||||||||||||||||||||||||||||||
| 392 | included and in what order, and both accept a C<skip_key> hashref in | ||||||||||||||||||||||||||||||||||||||||||
| 393 | C<%options> (same reachability caveat as C<new_request()>'s | ||||||||||||||||||||||||||||||||||||||||||
| 394 | C<skip_headers> above - only useful from a direct call, e.g. a C<data> | ||||||||||||||||||||||||||||||||||||||||||
| 395 | callback recursively re-invoking C<kvp2str()>/C<kvp2json()> on itself to | ||||||||||||||||||||||||||||||||||||||||||
| 396 | build its own value without infinite-looping on its own key; see | ||||||||||||||||||||||||||||||||||||||||||
| 397 | F<t/04_callbacks.t>) to exclude a key from the encoded body. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | C<kvp2str> alone also fires C<< $events->{before_sorting_keys} >> and | ||||||||||||||||||||||||||||||||||||||||||
| 400 | C<< $events->{after_sorting_keys} >>, each called with C<< keys => \@keys | ||||||||||||||||||||||||||||||||||||||||||
| 401 | >> - an arrayref of the field names about to be (respectively, just | ||||||||||||||||||||||||||||||||||||||||||
| 402 | were) sorted. Both are genuinely live: a callback that mutates | ||||||||||||||||||||||||||||||||||||||||||
| 403 | C<@$keys> in place (push/splice/grep out an element) changes what | ||||||||||||||||||||||||||||||||||||||||||
| 404 | C<kvp2str> actually encodes, as long as C<< $events->{keys} >> isn't | ||||||||||||||||||||||||||||||||||||||||||
| 405 | also set (which replaces C<@keys> outright, discarding | ||||||||||||||||||||||||||||||||||||||||||
| 406 | C<before_sorting_keys>' mutation). See F<t/21_before_events.t> and | ||||||||||||||||||||||||||||||||||||||||||
| 407 | F<t/33_before_sorting_keys_mutation.t>. C<kvp2json> has no equivalent | ||||||||||||||||||||||||||||||||||||||||||
| 408 | hook - its C<@keys> is either C<< $events->{keys} >>'s return value or | ||||||||||||||||||||||||||||||||||||||||||
| 409 | an unsorted C<keys %$data>, with no separate sorting step to hook into. | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | C<kvp2json> encodes a numeric-looking string value as a genuine JSON | ||||||||||||||||||||||||||||||||||||||||||
| 412 | number rather than a quoted string - but only when doing so loses | ||||||||||||||||||||||||||||||||||||||||||
| 413 | nothing (stringifying the number back reproduces the original value | ||||||||||||||||||||||||||||||||||||||||||
| 414 | exactly). A value like C<"5"> becomes C<5>, but C<"00501"> (a US zip | ||||||||||||||||||||||||||||||||||||||||||
| 415 | code) or C<"5.0"> stays a string, since numifying either would silently | ||||||||||||||||||||||||||||||||||||||||||
| 416 | discard meaningful formatting. A value like C<"NaN">, C<"Inf">, or | ||||||||||||||||||||||||||||||||||||||||||
| 417 | C<"-Inf"> also stays a string even though it round-trips losslessly as | ||||||||||||||||||||||||||||||||||||||||||
| 418 | a Perl number - JSON has no valid representation for a non-finite | ||||||||||||||||||||||||||||||||||||||||||
| 419 | number, and numifying it would make C<kvp2json> emit the bareword | ||||||||||||||||||||||||||||||||||||||||||
| 420 | C<nan>/C<inf>/C<-inf>, which is not valid JSON and no parser (including | ||||||||||||||||||||||||||||||||||||||||||
| 421 | this module's own JSON::XS) can decode. C<kvp2str> never numifies at | ||||||||||||||||||||||||||||||||||||||||||
| 422 | all - a query string has no separate number/string type, so there was | ||||||||||||||||||||||||||||||||||||||||||
| 423 | never a reason to risk altering the original representation. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | =head2 kvp2json_each(%options) / kvp2str_each(%options) | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | The per-value helpers C<kvp2json()>/C<kvp2str()> recurse into for each key | ||||||||||||||||||||||||||||||||||||||||||
| 428 | via C<%options>'s C<value> (and C<key> - the field's own key, available | ||||||||||||||||||||||||||||||||||||||||||
| 429 | to a C<CODE> value's callback via C<%options>; C<kvp2json_each> passes it | ||||||||||||||||||||||||||||||||||||||||||
| 430 | through at every level, including a nested hash value's own key, not | ||||||||||||||||||||||||||||||||||||||||||
| 431 | just the top-level field's). Resolves a C<CODE> value by calling it, | ||||||||||||||||||||||||||||||||||||||||||
| 432 | unwraps L<HTTP::API::DataTypeMarker> markers (C<xCSV>/C<xBOOLEAN> and | ||||||||||||||||||||||||||||||||||||||||||
| 433 | friends), and recurses into plain array values. There is normally no | ||||||||||||||||||||||||||||||||||||||||||
| 434 | need to call these directly - they exist as public methods so a | ||||||||||||||||||||||||||||||||||||||||||
| 435 | C<data>/C<value> callback can recursively re-invoke them on itself (see | ||||||||||||||||||||||||||||||||||||||||||
| 436 | C<kvp2json()>/C<kvp2str()> above). | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | These two are not symmetric on invalid input, in two ways. First, | ||||||||||||||||||||||||||||||||||||||||||
| 439 | C<kvp2json_each> dies if a raw-bytes (non-UTF8-flagged) value is not | ||||||||||||||||||||||||||||||||||||||||||
| 440 | valid UTF-8, rather than silently corrupting it into C<U+FFFD> | ||||||||||||||||||||||||||||||||||||||||||
| 441 | replacement characters - JSON has no way to represent arbitrary bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 442 | C<kvp2str_each> has no such restriction; a query string can carry any | ||||||||||||||||||||||||||||||||||||||||||
| 443 | byte sequence unescaped-safe via percent-encoding, so the same input | ||||||||||||||||||||||||||||||||||||||||||
| 444 | that dies in the JSON path passes through the form-urlencoded path | ||||||||||||||||||||||||||||||||||||||||||
| 445 | unchanged. Second, a plain hash value recurses in C<kvp2json_each> (JSON | ||||||||||||||||||||||||||||||||||||||||||
| 446 | has a native object type for it) but dies in C<kvp2str_each> - a query | ||||||||||||||||||||||||||||||||||||||||||
| 447 | string has no standard convention for representing a nested hash. | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | Both die - with the same message, naming the offending ref type and | ||||||||||||||||||||||||||||||||||||||||||
| 450 | pointing at C<xBOOLEAN()> - on any other reference type neither of them | ||||||||||||||||||||||||||||||||||||||||||
| 451 | otherwise recognizes, most commonly a bare (not C<xBOOLEAN>-wrapped) | ||||||||||||||||||||||||||||||||||||||||||
| 452 | scalar ref: a value like C<< \$flag >> passed directly, typically from | ||||||||||||||||||||||||||||||||||||||||||
| 453 | forgetting to call C<xBOOLEAN(\$flag)> around it. Before this die was | ||||||||||||||||||||||||||||||||||||||||||
| 454 | added, C<kvp2str_each> silently stringified such a value as | ||||||||||||||||||||||||||||||||||||||||||
| 455 | C<SCALAR(0x...)>, and C<kvp2json_each> reached the same outcome only by | ||||||||||||||||||||||||||||||||||||||||||
| 456 | accident of JSON::XS itself refusing to encode an arbitrary scalar ref | ||||||||||||||||||||||||||||||||||||||||||
| 457 | - with a message that never mentioned this module's own vocabulary. | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | C<kvp2str_each>'s C<%options> also accepts C<no_key>, set internally when | ||||||||||||||||||||||||||||||||||||||||||
| 460 | recursing into an ARRAY or C<xCSV> element: when true, the returned | ||||||||||||||||||||||||||||||||||||||||||
| 461 | fragment omits the leading C<key=> prefix (a plain scalar, C<xCSV>, and | ||||||||||||||||||||||||||||||||||||||||||
| 462 | C<xBOOLEAN>-marked value all honor this identically) so a CSV/array | ||||||||||||||||||||||||||||||||||||||||||
| 463 | element joins as a bare value rather than a spurious embedded | ||||||||||||||||||||||||||||||||||||||||||
| 464 | C<key=value> pair. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | Both encoders unwrap an C<xBOOLEAN(\$flag)> live scalar ref to whatever | ||||||||||||||||||||||||||||||||||||||||||
| 467 | C<$flag> currently holds, not just the C<0>/C<1> case C<xTRUE>/C<xFALSE> | ||||||||||||||||||||||||||||||||||||||||||
| 468 | use - a live ref that currently holds exactly (C<eq>, not merely | ||||||||||||||||||||||||||||||||||||||||||
| 469 | numerically C<==>) the string C<0> or C<1> still becomes a native JSON | ||||||||||||||||||||||||||||||||||||||||||
| 470 | boolean in C<kvp2json_each> (matching C<xTRUE>/C<xFALSE>; JSON::XS's own | ||||||||||||||||||||||||||||||||||||||||||
| 471 | convention only accepts that exact canonical form, not e.g. C<"01"> or | ||||||||||||||||||||||||||||||||||||||||||
| 472 | C<"1.0">), but any other live value - including one that is numerically | ||||||||||||||||||||||||||||||||||||||||||
| 473 | equal to C<0>/C<1> without being formatted exactly that way - encodes as | ||||||||||||||||||||||||||||||||||||||||||
| 474 | its own actual contents in both encoders alike, the same way a plain | ||||||||||||||||||||||||||||||||||||||||||
| 475 | (non-live) C<xBOOLEAN> value already did - including | ||||||||||||||||||||||||||||||||||||||||||
| 476 | C<kvp2json_each>'s invalid-UTF-8 die documented above, which a live | ||||||||||||||||||||||||||||||||||||||||||
| 477 | non-canonical value goes through exactly like the plain-scalar case | ||||||||||||||||||||||||||||||||||||||||||
| 478 | does. A live value that currently holds C<undef> is treated as an | ||||||||||||||||||||||||||||||||||||||||||
| 479 | empty string in both encoders, not C<undef> itself - it never reaches | ||||||||||||||||||||||||||||||||||||||||||
| 480 | the invalid-UTF-8 check (an empty string is trivially valid UTF-8) and | ||||||||||||||||||||||||||||||||||||||||||
| 481 | encodes as C<""> in JSON, matching how C<kvp2str_each> already handled | ||||||||||||||||||||||||||||||||||||||||||
| 482 | this case. | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | A plain (non-live) C<xBOOLEAN> value - anything other than the | ||||||||||||||||||||||||||||||||||||||||||
| 485 | C<\1>/C<\0> shape C<xTRUE>/C<xFALSE> use, including a value passed to | ||||||||||||||||||||||||||||||||||||||||||
| 486 | C<xBOOLEAN()> directly with no leading backslash - goes through the | ||||||||||||||||||||||||||||||||||||||||||
| 487 | exact same invalid-UTF-8 die and lossless-numify treatment in | ||||||||||||||||||||||||||||||||||||||||||
| 488 | C<kvp2json_each> as the plain scalar branch: C<xBOOLEAN("5")> encodes as | ||||||||||||||||||||||||||||||||||||||||||
| 489 | the JSON number C<5>, and invalid UTF-8 bytes die with the same message | ||||||||||||||||||||||||||||||||||||||||||
| 490 | rather than silently corrupting into JSON, just like a plain scalar | ||||||||||||||||||||||||||||||||||||||||||
| 491 | value with the identical content would. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | C<xBOOLEAN()> only accepts a plain scalar or a scalar ref - wrapping | ||||||||||||||||||||||||||||||||||||||||||
| 494 | anything else (an arrayref, a hashref) dies with a clear message naming | ||||||||||||||||||||||||||||||||||||||||||
| 495 | the offending ref type in both encoders, rather than silently | ||||||||||||||||||||||||||||||||||||||||||
| 496 | stringifying it (e.g. into C<"HASH(0x...)">) the way an unmarked | ||||||||||||||||||||||||||||||||||||||||||
| 497 | reference of the wrong shape does elsewhere in this module (see | ||||||||||||||||||||||||||||||||||||||||||
| 498 | C<kvp2str_each>'s nested-hash die, above). | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | This software is Copyright (c) 2026 by Michael Vu. | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | The MIT (X11) License | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | The MIT License | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | Permission is hereby granted, free of charge, to any person | ||||||||||||||||||||||||||||||||||||||||||
| 511 | obtaining a copy of this software and associated | ||||||||||||||||||||||||||||||||||||||||||
| 512 | documentation files (the "Software"), to deal in the Software | ||||||||||||||||||||||||||||||||||||||||||
| 513 | without restriction, including without limitation the rights to | ||||||||||||||||||||||||||||||||||||||||||
| 514 | use, copy, modify, merge, publish, distribute, sublicense, | ||||||||||||||||||||||||||||||||||||||||||
| 515 | and/or sell copies of the Software, and to permit persons to | ||||||||||||||||||||||||||||||||||||||||||
| 516 | whom the Software is furnished to do so, subject to the | ||||||||||||||||||||||||||||||||||||||||||
| 517 | following conditions: | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | The above copyright notice and this permission notice shall | ||||||||||||||||||||||||||||||||||||||||||
| 520 | be included in all copies or substantial portions of the | ||||||||||||||||||||||||||||||||||||||||||
| 521 | Software. | ||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT | ||||||||||||||||||||||||||||||||||||||||||
| 524 | WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||||||||||||||||||||||||||||||||||||||||||
| 525 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||||||||||||||||||||||||||||||||||||||||
| 526 | MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||||||||||||||||
| 527 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||||||||||||||||||||||||||||||||||||||||||
| 528 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||||||||||||||||||||||||||||||||||||||||||
| 529 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||||||||||||||||||||||||||||||||
| 530 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||||||||||||||||||||||||||||||||||||||||||
| 531 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||||||||||||||||||||||||||||||||||||||
| 532 | CONNECTION WITH THE SOFTWARE OR THE USE OR | ||||||||||||||||||||||||||||||||||||||||||
| 533 | OTHER DEALINGS IN THE SOFTWARE. | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | 78 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | 78 | ▶ | use HTTP::Headers; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | 78 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 78 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 541 | 78 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | 78 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 543 | 78 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 544 | 78 | ▶ | use URI::Escape qw( uri_escape uri_unescape ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | 78 | ▶ | use Scalar::Util qw( looks_like_number ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | 78 | ▶ | use POSIX qw( isnan isinf ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | 78 | ▶ | use HTTP::API::DataTypeMarker; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | 78 | extends 'Exporter'; | |||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | 78 | our @EXPORT = qw( xCSV xBOOLEAN | |||||||||||||||||||||||||||||||||||||||||
| 552 | xTRUE xFALSE | ||||||||||||||||||||||||||||||||||||||||||
| 553 | xTrue xFalse | ||||||||||||||||||||||||||||||||||||||||||
| 554 | xtrue xfalse | ||||||||||||||||||||||||||||||||||||||||||
| 555 | xt__e xf___e | ||||||||||||||||||||||||||||||||||||||||||
| 556 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | 78 | has username => ( | |||||||||||||||||||||||||||||||||||||||||
| 559 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 560 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 561 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 562 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | 65 | ▶ | sub _build_username { _defor($ENV{HTTP_USERNAME}, '') } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_username: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | 78 | has password => ( | |||||||||||||||||||||||||||||||||||||||||
| 567 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 568 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 569 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 570 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | 65 | ▶ | sub _build_password { _defor($ENV{HTTP_PASSWORD}, '') } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_password: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||
| 574 | 78 | has auth_token => ( | |||||||||||||||||||||||||||||||||||||||||
| 575 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 576 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 577 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 578 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | 65 | ▶ | sub _build_auth_token { _defor($ENV{HTTP_AUTH_TOKEN}, '') } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_auth_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | 78 | has base_url => ( | |||||||||||||||||||||||||||||||||||||||||
| 583 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 584 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 585 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 586 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||
| 588 | ▶ | sub _build_base_url {} | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_base_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | 78 | has last_response => ( | |||||||||||||||||||||||||||||||||||||||||
| 591 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 592 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 593 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 594 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | ▶ | sub _build_last_response {} | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_last_response: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | 78 | has charset => ( | |||||||||||||||||||||||||||||||||||||||||
| 599 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 600 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 601 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 602 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | 47 | ▶ | sub _build_charset { _defor($ENV{HTTP_CHARSET}, "utf8") } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_charset: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | 78 | has browser_id => ( | |||||||||||||||||||||||||||||||||||||||||
| 607 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 608 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 609 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 610 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | sub _build_browser_id { | ||||||||||||||||||||||||||||||||||||||||||
| 613 | 74 | ▶ | my $ver = _defor($HTTP::API::Client::VERSION, 'dev'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_browser_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 614 | 74 | return "HTTP API Client v$ver"; | |||||||||||||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | 78 | has content_type => ( | |||||||||||||||||||||||||||||||||||||||||
| 618 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 619 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 620 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 621 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | ▶ | sub _build_content_type {} | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_content_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | sub get_content_type { | ||||||||||||||||||||||||||||||||||||||||||
| 626 | 272 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_content_type: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 627 | 272 | my $content_type = $self->content_type; | |||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||
| 629 | 272 | ▶ | if ($content_type) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 630 | 87 | return $content_type; | |||||||||||||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | 185 | my $method = ${$o{method}}; | |||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | 185 | ▶ | if ($method eq 'GET') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 636 | 153 | return 'application/x-www-form-urlencoded'; | |||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | 32 | my $charset = $self->charset; | |||||||||||||||||||||||||||||||||||||||||
| 640 | 32 | return "application/json; charset=$charset"; | |||||||||||||||||||||||||||||||||||||||||
| 641 | } | ||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||
| 643 | 78 | has engine => ( | |||||||||||||||||||||||||||||||||||||||||
| 644 | is => "ro", | ||||||||||||||||||||||||||||||||||||||||||
| 645 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 646 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 647 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||
| 649 | 73 | ▶ | sub _build_engine {"LWP::UserAgent"} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_engine: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||
| 651 | 78 | has ua => ( | |||||||||||||||||||||||||||||||||||||||||
| 652 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 653 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 654 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 655 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||
| 657 | sub _build_ua { | ||||||||||||||||||||||||||||||||||||||||||
| 658 | 60 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_ua: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 659 | 60 | my $ssl_verify = $self->ssl_verify; | |||||||||||||||||||||||||||||||||||||||||
| 660 | 60 | my $engine = $self->engine; | |||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||
| 662 | 60 | my $ua; | |||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | 60 | ▶ | if ( $engine eq "LWP::UserAgent" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 665 | 59 | $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => $ssl_verify } ); | |||||||||||||||||||||||||||||||||||||||||
| 666 | 59 | $ua->agent( $self->browser_id ); | |||||||||||||||||||||||||||||||||||||||||
| 667 | 59 | $ua->timeout( $self->timeout ); | |||||||||||||||||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||||||||||||||||
| 669 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 670 | 1 | $ua = $self->$engine($ssl_verify); | |||||||||||||||||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||
| 673 | 60 | return $ua; | |||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||
| 676 | 78 | has ssl_verify => ( | |||||||||||||||||||||||||||||||||||||||||
| 677 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 678 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 679 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 680 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||
| 682 | sub _build_ssl_verify { | ||||||||||||||||||||||||||||||||||||||||||
| 683 | 60 | ▶ | return _defor( $ENV{SSL_VERIFY}, 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_ssl_verify: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||
| 686 | # send() does NOT read this attribute - since HAC-068 it calls | ||||||||||||||||||||||||||||||||||||||||||
| 687 | # $self->_build_retry directly on every call, so retry_config changes | ||||||||||||||||||||||||||||||||||||||||||
| 688 | # take effect live. This accessor is kept only for external callers who | ||||||||||||||||||||||||||||||||||||||||||
| 689 | # already read $api->retry directly; it's a one-time snapshot at | ||||||||||||||||||||||||||||||||||||||||||
| 690 | # whatever moment it's first accessed, not a live view of send()'s | ||||||||||||||||||||||||||||||||||||||||||
| 691 | # actual behavior. | ||||||||||||||||||||||||||||||||||||||||||
| 692 | 78 | has retry => ( | |||||||||||||||||||||||||||||||||||||||||
| 693 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 694 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 695 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 696 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||||||
| 698 | sub _build_retry { | ||||||||||||||||||||||||||||||||||||||||||
| 699 | 89 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_retry: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 700 | 89 | my %retry = %{ _defor($self->retry_config, {}) }; | |||||||||||||||||||||||||||||||||||||||||
| 701 | 89 | my $count = _defor( $retry{fail_response}, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 702 | 89 | ▶ | $count = 0 if looks_like_number($count) && $count < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&looks_like_number($count) and $count < 0
MC/DC50%1/2
&looks_like_number($count) and $count < 0
&looks_like_number($count)
$count < 0
Truth table66%2/3
A and B
A= &looks_like_number($count)B= $count < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 703 | 13 | my %status = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||
| 704 | 15 | grep { length } | |||||||||||||||||||||||||||||||||||||||||
| 705 | 15 | map { s/^\s+|\s+$//g; $_ } | |||||||||||||||||||||||||||||||||||||||||
| 706 | 89 | split /,/, _defor($retry{fail_status}, ''); | |||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||
| 708 | 89 | my $delay = _defor( $retry{delay}, 5 ); | |||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||
| 710 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 711 | 89 | count => $count, | |||||||||||||||||||||||||||||||||||||||||
| 712 | status => \%status, | ||||||||||||||||||||||||||||||||||||||||||
| 713 | delay => $delay, | ||||||||||||||||||||||||||||||||||||||||||
| 714 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 715 | } | ||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||
| 717 | 78 | has retry_config => ( | |||||||||||||||||||||||||||||||||||||||||
| 718 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 719 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 720 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 721 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||
| 723 | sub _build_retry_config { | ||||||||||||||||||||||||||||||||||||||||||
| 724 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 725 | fail_response => _defor( $ENV{RETRY_FAIL_RESPONSE}, 0 ), | ||||||||||||||||||||||||||||||||||||||||||
| 726 | fail_status => _defor($ENV{RETRY_FAIL_STATUS}, ''), | ||||||||||||||||||||||||||||||||||||||||||
| 727 | 73 | ▶ | delay => _defor( $ENV{RETRY_DELAY}, 5 ), | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_retry_config: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 728 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 729 | } | ||||||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||||||
| 731 | 78 | has timeout => ( | |||||||||||||||||||||||||||||||||||||||||
| 732 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 733 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 734 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 735 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||
| 737 | 73 | ▶ | sub _build_timeout { _defor($ENV{HTTP_TIMEOUT}, 60) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_timeout: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||
| 739 | 78 | has json => ( | |||||||||||||||||||||||||||||||||||||||||
| 740 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 741 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 742 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 743 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | sub _build_json { | ||||||||||||||||||||||||||||||||||||||||||
| 746 | 30 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 747 | 30 | return _apply_charset( JSON::XS->new->canonical->allow_nonref, $self->charset ); | |||||||||||||||||||||||||||||||||||||||||
| 748 | } | ||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||
| 750 | sub _apply_charset { | ||||||||||||||||||||||||||||||||||||||||||
| 751 | 89 | ▶ | my ($json, $charset) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_charset: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 752 | 89 | ▶ | eval { $json->$charset; 1 } or die "Unsupported charset '$charset': $@"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 753 | 87 | return $json; | |||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||
| 756 | 78 | has debug_flags => ( | |||||||||||||||||||||||||||||||||||||||||
| 757 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 758 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 759 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 760 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||
| 762 | sub _build_debug_flags { | ||||||||||||||||||||||||||||||||||||||||||
| 763 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 764 | in_out => $ENV{DEBUG_IN_OUT}, | ||||||||||||||||||||||||||||||||||||||||||
| 765 | send_out => $ENV{DEBUG_SEND_OUT}, | ||||||||||||||||||||||||||||||||||||||||||
| 766 | response => $ENV{DEBUG_RESPONSE}, | ||||||||||||||||||||||||||||||||||||||||||
| 767 | response_header_only => $ENV{DEBUG_RESPONSE_HEADER_ONLY}, | ||||||||||||||||||||||||||||||||||||||||||
| 768 | response_if_fail => $ENV{DEBUG_RESPONSE_IF_FAIL}, | ||||||||||||||||||||||||||||||||||||||||||
| 769 | 74 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_debug_flags: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 770 | } | ||||||||||||||||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||||||||||||||||
| 772 | 78 | has pre_defined_data => ( | |||||||||||||||||||||||||||||||||||||||||
| 773 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 774 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 775 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 776 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||||||||||
| 778 | 70 | ▶ | sub _build_pre_defined_data {{}} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_pre_defined_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||
| 780 | 78 | has pre_defined_headers => ( | |||||||||||||||||||||||||||||||||||||||||
| 781 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 782 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 783 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 784 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||
| 786 | 73 | ▶ | sub _build_pre_defined_headers {{}} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_pre_defined_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||
| 788 | 78 | has pre_defined_events => ( | |||||||||||||||||||||||||||||||||||||||||
| 789 | is => "rw", | ||||||||||||||||||||||||||||||||||||||||||
| 790 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 791 | builder => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 792 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | 73 | ▶ | sub _build_pre_defined_events {{}} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_pre_defined_events: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||
| 796 | sub get { | ||||||||||||||||||||||||||||||||||||||||||
| 797 | 61 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 798 | 61 | return $self->send( GET => @args ); | |||||||||||||||||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||||||||||||
| 801 | sub post { | ||||||||||||||||||||||||||||||||||||||||||
| 802 | 21 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 803 | 21 | return $self->send( POST => @args ); | |||||||||||||||||||||||||||||||||||||||||
| 804 | } | ||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||
| 806 | sub put { | ||||||||||||||||||||||||||||||||||||||||||
| 807 | 1 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 808 | 1 | return $self->send( PUT => @args ); | |||||||||||||||||||||||||||||||||||||||||
| 809 | } | ||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | sub head { | ||||||||||||||||||||||||||||||||||||||||||
| 812 | 1 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
head: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 813 | 1 | return $self->send( HEAD => @args ); | |||||||||||||||||||||||||||||||||||||||||
| 814 | } | ||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||
| 816 | sub delete { | ||||||||||||||||||||||||||||||||||||||||||
| 817 | 1 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 818 | 1 | return $self->send( DELETE => @args ); | |||||||||||||||||||||||||||||||||||||||||
| 819 | } | ||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||
| 821 | sub _execute_callbacks { | ||||||||||||||||||||||||||||||||||||||||||
| 822 | 168 | ▶ | my ($self, $type, %options) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_execute_callbacks: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||
| 824 | 168 | my $sth = $options{$type}; | |||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||
| 826 | 168 | for my $key (keys %$sth) { | |||||||||||||||||||||||||||||||||||||||||
| 827 | 143 | my $callback = $sth->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 828 | 143 | ▶ | next if !defined $callback; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 829 | 141 | ▶ | next if !UNIVERSAL::isa($callback, 'CODE'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 830 | 17 | $sth->{$key} = $self->$callback(key => $key, %options); | |||||||||||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||
| 834 | sub send { | ||||||||||||||||||||||||||||||||||||||||||
| 835 | 84 | ▶ | my ($self, $method, $path, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send: called CC=26 SCAR=32.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 836 | $data, $headers, $events) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||
| 838 | 84 | $method = uc $method; | |||||||||||||||||||||||||||||||||||||||||
| 839 | 84 | $path = _defor( $path, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 840 | 84 | $data = { %{ _defor( $data, {} ) } }; | |||||||||||||||||||||||||||||||||||||||||
| 841 | 84 | $headers = { %{ _defor( $headers, {} ) } }; | |||||||||||||||||||||||||||||||||||||||||
| 842 | 84 | $events = { %{ _defor( $events, {} ) } }; | |||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||
| 844 | 84 | my $base_url = $self->base_url; | |||||||||||||||||||||||||||||||||||||||||
| 845 | 84 | ▶ | my $url = $base_url ? $base_url . $path : $path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 846 | 84 | my $ua = $self->ua; | |||||||||||||||||||||||||||||||||||||||||
| 847 | 84 | ▶ | $ua->agent( $self->browser_id ) if $ua->can('agent'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 848 | 84 | ▶ | $ua->timeout( $self->timeout ) if $ua->can('timeout'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 849 | 84 | ▶ | $ua->ssl_opts( verify_hostname => $self->ssl_verify ) if $ua->can('ssl_opts'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 850 | 84 | my $retry_settings = $self->_build_retry; | |||||||||||||||||||||||||||||||||||||||||
| 851 | 84 | my $retry_count = _defor( $retry_settings->{count}, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 852 | 84 | my $retry_delay = _defor( $retry_settings->{delay}, 5 ); | |||||||||||||||||||||||||||||||||||||||||
| 853 | 84 | ▶ | $retry_delay = 0 if looks_like_number($retry_delay) && $retry_delay < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&looks_like_number($retry_delay) and $retry_delay < 0
MC/DC50%1/2
&looks_like_number($retry_delay) and $retry_delay < 0
&looks_like_number($retry_delay)
$retry_delay < 0
Truth table66%2/3
A and B
A= &looks_like_number($retry_delay)B= $retry_delay < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 854 | 84 | my %retry_status = %{ _defor($retry_settings->{status}, {}) }; | |||||||||||||||||||||||||||||||||||||||||
| 855 | 84 | my %debug = %{ _defor($self->debug_flags, {}) }; | |||||||||||||||||||||||||||||||||||||||||
| 856 | 84 | my $eng = $self->engine; | |||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||
| 858 | 84 | ▶ | if ( my $pd = $self->pre_defined_data ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 859 | 84 | %$data = ( %$pd, %$data ); | |||||||||||||||||||||||||||||||||||||||||
| 860 | } | ||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||
| 862 | 84 | ▶ | if ( my $ph = $self->pre_defined_headers ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 863 | 84 | %$headers = ( %$ph, %$headers ); | |||||||||||||||||||||||||||||||||||||||||
| 864 | } | ||||||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||||||
| 866 | 84 | ▶ | if ( my $pe = $self->pre_defined_events ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 867 | 84 | %$events = ( %$pe, %$events ); | |||||||||||||||||||||||||||||||||||||||||
| 868 | } | ||||||||||||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||||||||||||
| 870 | 84 | my %options = ( | |||||||||||||||||||||||||||||||||||||||||
| 871 | method => \$method, | ||||||||||||||||||||||||||||||||||||||||||
| 872 | url => \$url, | ||||||||||||||||||||||||||||||||||||||||||
| 873 | path => \$path, | ||||||||||||||||||||||||||||||||||||||||||
| 874 | data => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 875 | headers => $headers, | ||||||||||||||||||||||||||||||||||||||||||
| 876 | events => $events, | ||||||||||||||||||||||||||||||||||||||||||
| 877 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||||||
| 879 | 84 | $self->_execute_callbacks(data => %options); | |||||||||||||||||||||||||||||||||||||||||
| 880 | 84 | $self->_execute_callbacks(headers => %options); | |||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||
| 882 | 84 | my $response; | |||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | RETRY: | ||||||||||||||||||||||||||||||||||||||||||
| 885 | 84 | foreach my $retry ( 0 .. $retry_count ) { | |||||||||||||||||||||||||||||||||||||||||
| 886 | 92 | my $started_time = time; | |||||||||||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||||||||||||
| 888 | 92 | ▶ | if ( $eng eq 'LWP::UserAgent' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 889 | 91 | my $req = $self->new_request( %options ); | |||||||||||||||||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||||||||||||||||
| 891 | 87 | ▶ | if ($events->{test_request_object}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 892 | 64 | return $req; | |||||||||||||||||||||||||||||||||||||||||
| 893 | } | ||||||||||||||||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||||||||||||||||
| 895 | 23 | $response = $ua->request($req); | |||||||||||||||||||||||||||||||||||||||||
| 896 | } | ||||||||||||||||||||||||||||||||||||||||||
| 897 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 898 | 1 | die "Unsupported engine: $eng - only LWP::UserAgent is currently wired up in send()"; | |||||||||||||||||||||||||||||||||||||||||
| 899 | } | ||||||||||||||||||||||||||||||||||||||||||
| 900 | |||||||||||||||||||||||||||||||||||||||||||
| 901 | 23 | ▶ | my $suppress_on_success = $debug{response_if_fail} && $response->is_success; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$debug{'response_if_fail'} && $response->is_success
MC/DC100%2/2
$debug{'response_if_fail'} && $response->is_success
$debug{'response_if_fail'}
$response->is_success
Truth table100%3/3
A && B
A= $debug{'response_if_fail'}B= $response->is_success
| |||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||
| 903 | 23 | ▶ | if ( $debug{send_out} || ( $debug{in_out} && !$suppress_on_success ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$debug{"send_out"} or $debug{"in_out"} and not $suppress_on_success
$debug{"in_out"} and not $suppress_on_success
MC/DC66%2/3
$debug{"send_out"} or $debug{"in_out"} and not $suppress_on_success
$debug{"send_out"}
$debug{"in_out"}
not $suppress_on_success
Truth table75%3/4
A or B and not(C)
A= $debug{"send_out"}B= $debug{"in_out"}C= not $suppress_on_success
| |||||||||||||||||||||||||||||||||||||||||||
| 904 | 6 | print STDERR "-- REQUEST --\n"; | |||||||||||||||||||||||||||||||||||||||||
| 905 | 6 | ▶ | if ( $retry_count && $retry ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$retry_count and $retry
MC/DC100%2/2
$retry_count and $retry
$retry_count
$retry
Truth table100%3/3
A and B
A= $retry_countB= $retry
| |||||||||||||||||||||||||||||||||||||||||||
| 906 | 2 | print STDERR "-- RETRY $retry of $retry_count\n"; | |||||||||||||||||||||||||||||||||||||||||
| 907 | } | ||||||||||||||||||||||||||||||||||||||||||
| 908 | 6 | print STDERR $response->request->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 909 | 6 | print STDERR "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 910 | } | ||||||||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||||||||
| 912 | 23 | my $debug_response = _defor($debug{in_out}, $debug{response}); | |||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||
| 914 | 23 | ▶ | $debug_response = 0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 915 | if $suppress_on_success; | ||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | 23 | ▶ | if ($debug_response) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 918 | 3 | my $used_time = time - $started_time; | |||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||
| 920 | 3 | print STDERR "-- RESPONSE $used_time sec(s) --\n"; | |||||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||||||
| 922 | print STDERR $debug{response_header_only} | ||||||||||||||||||||||||||||||||||||||||||
| 923 | 3 | ▶ | ? $response->headers->as_string | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 924 | : $response->as_string; | ||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||
| 926 | 3 | print STDERR ( "-" x 80 ) . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 927 | } | ||||||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||||||
| 929 | last RETRY ## request is success, not further for retry | ||||||||||||||||||||||||||||||||||||||||||
| 930 | 23 | ▶ | if $response->is_success; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||||||||||||||||
| 932 | last RETRY ## no attempts left, don't sleep for a retry that won't happen | ||||||||||||||||||||||||||||||||||||||||||
| 933 | 19 | ▶ | if $retry >= $retry_count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||||||||||||||||
| 935 | 9 | ▶ | if ( !%retry_status ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 936 | 4 | sleep $retry_delay; | |||||||||||||||||||||||||||||||||||||||||
| 937 | ## no retry pattern at all then just retry | ||||||||||||||||||||||||||||||||||||||||||
| 938 | 4 | next RETRY; | |||||||||||||||||||||||||||||||||||||||||
| 939 | } | ||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||
| 941 | 5 | ▶ | $retry_status{ $response->code } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 942 | or | ||||||||||||||||||||||||||||||||||||||||||
| 943 | last RETRY; ## status code not in RETRY_FAIL_STATUS, just stop retry | ||||||||||||||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||||||||||||
| 945 | 4 | sleep $retry_delay; | |||||||||||||||||||||||||||||||||||||||||
| 946 | 4 | next RETRY; | |||||||||||||||||||||||||||||||||||||||||
| 947 | } | ||||||||||||||||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||||||||||||||||
| 949 | 15 | return $self->last_response($response); | |||||||||||||||||||||||||||||||||||||||||
| 950 | } | ||||||||||||||||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||||||||||||||||
| 952 | sub json_response { | ||||||||||||||||||||||||||||||||||||||||||
| 953 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
json_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||||||||||||||
| 955 | my $response = try { | ||||||||||||||||||||||||||||||||||||||||||
| 956 | 5 | ▶ | my $content = _defor($self->last_response->decoded_content, '{}'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 957 | 4 | my $json = _apply_charset( $self->json, $self->charset ); | |||||||||||||||||||||||||||||||||||||||||
| 958 | 4 | $json->decode($content); | |||||||||||||||||||||||||||||||||||||||||
| 959 | } | ||||||||||||||||||||||||||||||||||||||||||
| 960 | catch { | ||||||||||||||||||||||||||||||||||||||||||
| 961 | 2 | ▶ | my $error = $_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 962 | 2 | { status => "error", error => $error }; | |||||||||||||||||||||||||||||||||||||||||
| 963 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||||||||||||||||
| 965 | 5 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 966 | } | ||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||
| 968 | sub kvp_response { | ||||||||||||||||||||||||||||||||||||||||||
| 969 | 12 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kvp_response: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||||||||||||||||
| 971 | 12 | ▶ | my $response = $self->last_response | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 972 | or return {}; | ||||||||||||||||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||||||||||||||||
| 974 | 11 | ▶ | my $content = $response->decoded_content | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 975 | or return {}; | ||||||||||||||||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||||||||||||||||
| 977 | 11 | my %data; | |||||||||||||||||||||||||||||||||||||||||
| 978 | 11 | for my $pair ( split /&/, $content ) { | |||||||||||||||||||||||||||||||||||||||||
| 979 | 22 | ▶ | next if $pair eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 980 | 20 | my ( $k, $v ) = map { ( my $s = $_ ) =~ tr/+/ /; uri_unescape($s) } split /=/, $pair, 2; | |||||||||||||||||||||||||||||||||||||||||
| 981 | 20 | ▶ | if ( exists $data{$k} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 982 | 5 | ▶ | $data{$k} = [ $data{$k} ] unless ref $data{$k} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 983 | 5 | push @{ $data{$k} }, $v; | |||||||||||||||||||||||||||||||||||||||||
| 984 | } | ||||||||||||||||||||||||||||||||||||||||||
| 985 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 986 | 15 | $data{$k} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 987 | } | ||||||||||||||||||||||||||||||||||||||||||
| 988 | } | ||||||||||||||||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||||||||||||||||
| 990 | 11 | return \%data; | |||||||||||||||||||||||||||||||||||||||||
| 991 | } | ||||||||||||||||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||||||||||||||||
| 993 | sub new_request { | ||||||||||||||||||||||||||||||||||||||||||
| 994 | 92 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_request: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||||||||||||||||
| 996 | 92 | my ($method, $url) = map { $$_ } @o{qw(method url)}; | |||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||
| 998 | 92 | my ($data, $headers, $events) = @o{qw(data headers events)}; | |||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||
| 1000 | 92 | my $content_type = $self->get_content_type(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||||||||||||||||
| 1002 | 92 | my $content = $self->convert_data(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||||||||||||
| 1004 | 89 | ▶ | if ($content) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1005 | 41 | ▶ | if ($self->charset eq 'utf8') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1006 | 40 | $content = _tune_utf8($content); | |||||||||||||||||||||||||||||||||||||||||
| 1007 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1008 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1009 | |||||||||||||||||||||||||||||||||||||||||||
| 1010 | 89 | my $request; | |||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||
| 1012 | 89 | ▶ | if ($method eq 'GET') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1013 | 67 | ▶ | if ($content_type ne 'application/x-www-form-urlencoded') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1014 | 1 | die "Unable to create a get request with content_type: $content_type"; | |||||||||||||||||||||||||||||||||||||||||
| 1015 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1016 | elsif ($content) { | ||||||||||||||||||||||||||||||||||||||||||
| 1017 | 20 | ▶ | if ($url =~ m/\?/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1018 | 1 | $request = $self->prepare_request(%o, url => \"$url&$content"); | |||||||||||||||||||||||||||||||||||||||||
| 1019 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1020 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1021 | 19 | $request = $self->prepare_request(%o, url => \"$url?$content"); | |||||||||||||||||||||||||||||||||||||||||
| 1022 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1023 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1025 | 46 | $request = $self->prepare_request(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1026 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1027 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1028 | elsif ($content) { | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | 20 | $request = $self->prepare_request(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1030 | 20 | $request->content($content); | |||||||||||||||||||||||||||||||||||||||||
| 1031 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1033 | 2 | $request = $self->prepare_request(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1034 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1035 | |||||||||||||||||||||||||||||||||||||||||||
| 1036 | 88 | %o = (%o, | |||||||||||||||||||||||||||||||||||||||||
| 1037 | request => $request, | ||||||||||||||||||||||||||||||||||||||||||
| 1038 | content => \$content, | ||||||||||||||||||||||||||||||||||||||||||
| 1039 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1040 | |||||||||||||||||||||||||||||||||||||||||||
| 1041 | 88 | ▶ | if (my $do = $events->{before_headers}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1042 | 1 | $self->$do(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1043 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||||||||||||||||
| 1045 | 88 | my @keys; | |||||||||||||||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||||||||||||||||
| 1047 | 88 | ▶ | if (my $keys = $events->{headers_keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1048 | 1 | @keys = $self->$keys(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1049 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1050 | elsif (my $add = $events->{add_headers_keys}) { | ||||||||||||||||||||||||||||||||||||||||||
| 1051 | 2 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 1052 | 2 | @keys = sort grep { !$seen{$_}++ } $self->$add(%o), keys %$headers; | |||||||||||||||||||||||||||||||||||||||||
| 1053 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1054 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1055 | 85 | @keys = sort keys %$headers; | |||||||||||||||||||||||||||||||||||||||||
| 1056 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||||||||||||||||
| 1058 | 88 | foreach my $key ( @keys ) { | |||||||||||||||||||||||||||||||||||||||||
| 1059 | 22 | ▶ | if (my $do = $events->{before_header}{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1060 | 2 | $headers->{$key} = $self->$do(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1061 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1062 | |||||||||||||||||||||||||||||||||||||||||||
| 1063 | 22 | ▶ | next if $o{skip_headers}{$key} || !exists $headers->{$key} || !defined $headers->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$o{"skip_headers"}{$key} or not exists $headers->{$key} or not defined $headers->{$key}
$o{"skip_headers"}{$key} or not exists $headers->{$key}
MC/DC66%2/3
$o{"skip_headers"}{$key} or not exists $headers->{$key} or not defined $headers->{$key}
$o{"skip_headers"}{$key}
not exists $headers->{$key}
not defined $headers->{$key}
Truth table75%3/4
A or not(B) or not(C)
A= $o{"skip_headers"}{$key}B= not exists $headers->{$key}C= not defined $headers->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||||||||||||||||
| 1065 | 20 | $request->header( $key => _encode_if_utf8_flagged( $headers->{$key} ) ); | |||||||||||||||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||||||||||||||||
| 1067 | 20 | ▶ | if (my $do = $events->{after_header}{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1068 | 2 | $self->$do(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1069 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1070 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1071 | |||||||||||||||||||||||||||||||||||||||||||
| 1072 | 88 | ▶ | if (my $do = $events->{after_header_keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1073 | 1 | $self->$do(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1074 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||
| 1076 | 88 | return $request; | |||||||||||||||||||||||||||||||||||||||||
| 1077 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||||||||||||
| 1079 | sub prepare_request { | ||||||||||||||||||||||||||||||||||||||||||
| 1080 | 88 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_request: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||||||||||||||||
| 1082 | 88 | my ($method, $url) = map { $$_ } @o{qw(method url)}; | |||||||||||||||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||||||||||||||||
| 1084 | 88 | my ($headers) = @o{qw(headers)}; | |||||||||||||||||||||||||||||||||||||||||
| 1085 | |||||||||||||||||||||||||||||||||||||||||||
| 1086 | 88 | my $request = HTTP::Request->new( $method => $url ); | |||||||||||||||||||||||||||||||||||||||||
| 1087 | |||||||||||||||||||||||||||||||||||||||||||
| 1088 | 88 | $request->content_type($self->get_content_type(%o)); | |||||||||||||||||||||||||||||||||||||||||
| 1089 | |||||||||||||||||||||||||||||||||||||||||||
| 1090 | 88 | my ($u, $p, $at) = map { _defor($self->$_, '') } | |||||||||||||||||||||||||||||||||||||||||
| 1091 | qw(username password auth_token); | ||||||||||||||||||||||||||||||||||||||||||
| 1092 | |||||||||||||||||||||||||||||||||||||||||||
| 1093 | 88 | ▶ | if ($u || $p) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$u or $p
MC/DC50%1/2
$u or $p
$u
$p
Truth table66%2/3
A or B
A= $uB= $p
| |||||||||||||||||||||||||||||||||||||||||||
| 1094 | 5 | $self->basic_authenticator( $request, | |||||||||||||||||||||||||||||||||||||||||
| 1095 | _encode_if_utf8_flagged($u), _encode_if_utf8_flagged($p) ); | ||||||||||||||||||||||||||||||||||||||||||
| 1096 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1097 | elsif ($at) { | ||||||||||||||||||||||||||||||||||||||||||
| 1098 | $headers->{authorization} = $at | ||||||||||||||||||||||||||||||||||||||||||
| 1099 | 4 | ▶ | unless grep { lc($_) eq 'authorization' } keys %$headers; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||||||||||||
| 1102 | 88 | return $request; | |||||||||||||||||||||||||||||||||||||||||
| 1103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||||||||||
| 1105 | sub _tune_utf8 { | ||||||||||||||||||||||||||||||||||||||||||
| 1106 | 42 | ▶ | my ($content) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tune_utf8: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1107 | |||||||||||||||||||||||||||||||||||||||||||
| 1108 | 42 | my $req = HTTP::Request->new( POST => "http://find-encoding.com" ); | |||||||||||||||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||||||||||||||||
| 1110 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 1111 | 42 | ▶ | $req->content($content); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1113 | catch { | ||||||||||||||||||||||||||||||||||||||||||
| 1114 | 1 | ▶ | my $error = $_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1115 | 1 | ▶ | if ( $error =~ /content must be bytes/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1116 | 1 | eval { $content = Encode::encode( utf8 => $content ); }; | |||||||||||||||||||||||||||||||||||||||||
| 1117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | 42 | }; | |||||||||||||||||||||||||||||||||||||||||
| 1119 | 42 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 1120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1121 | |||||||||||||||||||||||||||||||||||||||||||
| 1122 | sub _should_skip_key { | ||||||||||||||||||||||||||||||||||||||||||
| 1123 | 152 | ▶ | my (%o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_should_skip_key: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1124 | 152 | my ($key, $data) = @o{qw(key data)}; | |||||||||||||||||||||||||||||||||||||||||
| 1125 | 152 | ▶ | return $o{skip_key}{$key} || !exists $data->{$key} || !defined $data->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$o{'skip_key'}{$key} || !exists $data->{$key} || !defined($data->{$key})
$o{"skip_key"}{$key} or not exists $data->{$key}
MC/DC66%2/3
$o{'skip_key'}{$key} || !exists $data->{$key} || !defined($data->{$key})
$o{"skip_key"}{$key}
not exists $data->{$key}
!defined($data->{$key})
Truth table75%3/4
A or not(B) || not(C)
A= $o{"skip_key"}{$key}B= not exists $data->{$key}C= !defined($data->{$key})
| |||||||||||||||||||||||||||||||||||||||||||
| 1126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1127 | |||||||||||||||||||||||||||||||||||||||||||
| 1128 | sub _encode_if_utf8_flagged { | ||||||||||||||||||||||||||||||||||||||||||
| 1129 | 313 | ▶ | my ($v) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_if_utf8_flagged: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1130 | 313 | ▶ | return utf8::is_utf8($v) ? Encode::encode( utf8 => $v ) : $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||||||||||
| 1133 | sub _uri_escape_bytes_or_chars { | ||||||||||||||||||||||||||||||||||||||||||
| 1134 | 283 | ▶ | my ($v) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uri_escape_bytes_or_chars: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1135 | 283 | return uri_escape( _encode_if_utf8_flagged($v) ); | |||||||||||||||||||||||||||||||||||||||||
| 1136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1137 | |||||||||||||||||||||||||||||||||||||||||||
| 1138 | sub _json_validate_utf8 { | ||||||||||||||||||||||||||||||||||||||||||
| 1139 | 67 | ▶ | my ($v) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_validate_utf8: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1140 | 67 | ▶ | return $v if utf8::is_utf8($v); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1141 | 63 | my $decoded = eval { Encode::decode( utf8 => $v, Encode::FB_CROAK ) }; | |||||||||||||||||||||||||||||||||||||||||
| 1142 | 63 | ▶ | die sprintf( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1143 | "kvp2json_each: value is not valid UTF-8, refusing to silently corrupt it into JSON (raw bytes: %v02x)\n", | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | $v | ||||||||||||||||||||||||||||||||||||||||||
| 1145 | ) if !defined $decoded; | ||||||||||||||||||||||||||||||||||||||||||
| 1146 | 59 | return $decoded; | |||||||||||||||||||||||||||||||||||||||||
| 1147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1148 | |||||||||||||||||||||||||||||||||||||||||||
| 1149 | sub _numify_if_lossless { | ||||||||||||||||||||||||||||||||||||||||||
| 1150 | 63 | ▶ | my ($v) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_numify_if_lossless: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1151 | 63 | ▶ | return $v if !looks_like_number($v); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1152 | # NaN/Infinity have no valid JSON representation - JSON::XS's utf8-mode | ||||||||||||||||||||||||||||||||||||||||||
| 1153 | # encoder emits them as the bareword nan/inf/-inf, which is not valid | ||||||||||||||||||||||||||||||||||||||||||
| 1154 | # JSON. Leave the original string untouched rather than numifying into | ||||||||||||||||||||||||||||||||||||||||||
| 1155 | # a token no JSON parser (including JSON::XS itself) can decode. | ||||||||||||||||||||||||||||||||||||||||||
| 1156 | 46 | ▶ | return $v if isnan($v + 0) || isinf($v + 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
isnan($v + 0) or isinf($v + 0)
MC/DC100%2/2
isnan($v + 0) or isinf($v + 0)
isnan($v + 0)
isinf($v + 0)
Truth table100%3/3
A or B
A= isnan($v + 0)B= isinf($v + 0)
| |||||||||||||||||||||||||||||||||||||||||||
| 1157 | # Compare against a throwaway stringified copy, not $v+0 itself - comparing | ||||||||||||||||||||||||||||||||||||||||||
| 1158 | # a number with eq caches a string form on that same scalar (SvPOK), and | ||||||||||||||||||||||||||||||||||||||||||
| 1159 | # JSON::XS then encodes it as a JSON string instead of a JSON number. | ||||||||||||||||||||||||||||||||||||||||||
| 1160 | 41 | my $stringified = ($v + 0) . ''; | |||||||||||||||||||||||||||||||||||||||||
| 1161 | 41 | ▶ | return $v eq $stringified ? $v + 0 : $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||||||||||||
| 1164 | sub convert_data { | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | 92 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
convert_data: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||||||||||||||||
| 1167 | 92 | my ($data, $events) = @o{qw(data events)}; | |||||||||||||||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||||||||||||||||
| 1169 | 92 | my $content_type = $self->get_content_type(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||||||||||||||||
| 1171 | 92 | ▶ | if ($content_type =~ m/json/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1172 | 17 | return $self->kvp2json(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | elsif ($content_type eq 'application/x-www-form-urlencoded') { | ||||||||||||||||||||||||||||||||||||||||||
| 1175 | 73 | return $self->kvp2str(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1177 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1178 | 2 | ▶ | return '' if !%$data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1179 | 1 | die "Unable to convert data for content_type: $content_type"; | |||||||||||||||||||||||||||||||||||||||||
| 1180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1182 | |||||||||||||||||||||||||||||||||||||||||||
| 1183 | sub kvp2json { | ||||||||||||||||||||||||||||||||||||||||||
| 1184 | 55 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kvp2json: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||||||||||||
| 1186 | 55 | my ($data, $events) = @o{qw(data events)}; | |||||||||||||||||||||||||||||||||||||||||
| 1187 | |||||||||||||||||||||||||||||||||||||||||||
| 1188 | 55 | my $json = _apply_charset( $self->json, $self->charset ); | |||||||||||||||||||||||||||||||||||||||||
| 1189 | |||||||||||||||||||||||||||||||||||||||||||
| 1190 | 54 | my @keys; | |||||||||||||||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||||||||||||||||
| 1192 | 54 | ▶ | if (my $do = $events->{keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1193 | 1 | @keys = $self->$do(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1195 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | 53 | @keys = keys %$data; | |||||||||||||||||||||||||||||||||||||||||
| 1197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||||||||||||||||
| 1199 | 54 | my %data = (); | |||||||||||||||||||||||||||||||||||||||||
| 1200 | |||||||||||||||||||||||||||||||||||||||||||
| 1201 | 54 | foreach my $key(@keys) { | |||||||||||||||||||||||||||||||||||||||||
| 1202 | 66 | ▶ | if ($events->{not_include}{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1203 | next | ||||||||||||||||||||||||||||||||||||||||||
| 1204 | 1 | } | |||||||||||||||||||||||||||||||||||||||||
| 1205 | 65 | ▶ | next if _should_skip_key(%o, key => $key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1206 | 64 | $data{$key} = $self->kvp2json_each(%o, key => $key, value => $data->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 1207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||||||||||||||||
| 1209 | 48 | return $json->encode(\%data); | |||||||||||||||||||||||||||||||||||||||||
| 1210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||||||||||
| 1212 | sub kvp2json_each { | ||||||||||||||||||||||||||||||||||||||||||
| 1213 | 97 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kvp2json_each: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1214 | |||||||||||||||||||||||||||||||||||||||||||
| 1215 | 97 | my ($v) = map { _defor($_, '') } @o{qw( value )}; | |||||||||||||||||||||||||||||||||||||||||
| 1216 | |||||||||||||||||||||||||||||||||||||||||||
| 1217 | 97 | ▶ | if (UNIVERSAL::isa($v, 'CODE')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1218 | 8 | $v = $self->$v(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1220 | |||||||||||||||||||||||||||||||||||||||||||
| 1221 | 97 | ▶ | if (!ref $v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1222 | 56 | return _numify_if_lossless( _json_validate_utf8($v) ); | |||||||||||||||||||||||||||||||||||||||||
| 1223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1224 | elsif (ref $v eq 'BOOL') { | ||||||||||||||||||||||||||||||||||||||||||
| 1225 | 26 | my $inner = $v->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 1226 | 26 | ▶ | if ( ref $inner eq 'SCALAR' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1227 | 21 | my $live_value = _defor( $$inner, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 1228 | 21 | ▶ | if ( !( $live_value eq '0' || $live_value eq '1' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$live_value eq 0 or $live_value eq 1
MC/DC100%2/2
$live_value eq 0 or $live_value eq 1
$live_value eq 0
$live_value eq 1
Truth table100%3/3
A or B
A= $live_value eq 0B= $live_value eq 1
| |||||||||||||||||||||||||||||||||||||||||||
| 1229 | 7 | return _numify_if_lossless( _json_validate_utf8($live_value) ); | |||||||||||||||||||||||||||||||||||||||||
| 1230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1231 | 14 | return $inner; | |||||||||||||||||||||||||||||||||||||||||
| 1232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1233 | 5 | ▶ | die "xBOOLEAN() only accepts a plain scalar or a scalar ref, not a " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1234 | . ref($inner) . " ref\n" if ref $inner; | ||||||||||||||||||||||||||||||||||||||||||
| 1235 | 4 | return _numify_if_lossless( _json_validate_utf8( _defor( $inner, '' ) ) ); | |||||||||||||||||||||||||||||||||||||||||
| 1236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1237 | elsif (UNIVERSAL::isa($v, 'ARRAY')) { | ||||||||||||||||||||||||||||||||||||||||||
| 1238 | 11 | my @parts; | |||||||||||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||||||||||||
| 1240 | 11 | foreach my $val(@$v) { | |||||||||||||||||||||||||||||||||||||||||
| 1241 | 26 | push @parts, $self->kvp2json_each(%o, value => $val); | |||||||||||||||||||||||||||||||||||||||||
| 1242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||||||||||||
| 1244 | 11 | return \@parts; | |||||||||||||||||||||||||||||||||||||||||
| 1245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1246 | elsif (UNIVERSAL::isa($v, 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||||
| 1247 | 3 | my %parts; | |||||||||||||||||||||||||||||||||||||||||
| 1248 | |||||||||||||||||||||||||||||||||||||||||||
| 1249 | 3 | foreach my $key(keys %$v) { | |||||||||||||||||||||||||||||||||||||||||
| 1250 | 7 | $parts{$key} = $self->kvp2json_each(%o, key => $key, value => $v->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 1251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1252 | |||||||||||||||||||||||||||||||||||||||||||
| 1253 | 3 | return \%parts; | |||||||||||||||||||||||||||||||||||||||||
| 1254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||||||||||
| 1256 | die "Unable to convert a " . ref($v) . " reference into JSON for key '" | ||||||||||||||||||||||||||||||||||||||||||
| 1257 | 1 | . _defor( $o{key}, '' ) . "' - wrap it in xBOOLEAN() if you meant a " | |||||||||||||||||||||||||||||||||||||||||
| 1258 | . "live boolean/tracked value\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 1259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||||||||||||||||
| 1261 | sub kvp2str { | ||||||||||||||||||||||||||||||||||||||||||
| 1262 | 94 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kvp2str: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||||||||||||||||
| 1264 | 94 | my ($data, $events) = @o{qw(data events)}; | |||||||||||||||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||||||||||||||||
| 1266 | 94 | my @keys = keys %$data; | |||||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||||||
| 1268 | 94 | ▶ | if (my $do = $events->{before_sorting_keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1269 | 3 | $self->$do(%o, keys => \@keys); | |||||||||||||||||||||||||||||||||||||||||
| 1270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1271 | |||||||||||||||||||||||||||||||||||||||||||
| 1272 | 94 | ▶ | if (my $do = $events->{keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1273 | 2 | @keys = $self->$do(%o); | |||||||||||||||||||||||||||||||||||||||||
| 1274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1275 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1276 | 92 | @keys = sort @keys; | |||||||||||||||||||||||||||||||||||||||||
| 1277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||||||||||||||||
| 1279 | 94 | ▶ | if (my $do = $events->{after_sorting_keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1280 | 1 | $self->$do(%o, keys => \@keys); | |||||||||||||||||||||||||||||||||||||||||
| 1281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1282 | |||||||||||||||||||||||||||||||||||||||||||
| 1283 | 94 | my @parts; | |||||||||||||||||||||||||||||||||||||||||
| 1284 | |||||||||||||||||||||||||||||||||||||||||||
| 1285 | 94 | foreach my $key(@keys) { | |||||||||||||||||||||||||||||||||||||||||
| 1286 | 88 | ▶ | next if $events->{not_include}{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1287 | 87 | ▶ | next if _should_skip_key(%o, key => $key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1288 | 86 | my $part = $self->kvp2str_each(%o, key => $key, value => $data->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 1289 | 82 | ▶ | push @parts, $part if length $part; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||||||||||||||||
| 1292 | 90 | return join '&', @parts; | |||||||||||||||||||||||||||||||||||||||||
| 1293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1294 | |||||||||||||||||||||||||||||||||||||||||||
| 1295 | sub kvp2str_each { | ||||||||||||||||||||||||||||||||||||||||||
| 1296 | 159 | ▶ | my ($self, %o) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kvp2str_each: called CC=22 SCAR=30.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1297 | |||||||||||||||||||||||||||||||||||||||||||
| 1298 | 159 | my ($raw_key, $v) = map { _defor($_, '') } @o{qw( key value )}; | |||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||
| 1300 | 159 | my $k = _uri_escape_bytes_or_chars($raw_key); | |||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||
| 1302 | 159 | ▶ | if (UNIVERSAL::isa($v, 'CODE')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1303 | 8 | $v = $self->$v(%o, key => $k); | |||||||||||||||||||||||||||||||||||||||||
| 1304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||||||||||||||||
| 1306 | 159 | ▶ | if (!ref $v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1307 | 98 | $v = _uri_escape_bytes_or_chars($v); | |||||||||||||||||||||||||||||||||||||||||
| 1308 | |||||||||||||||||||||||||||||||||||||||||||
| 1309 | 98 | ▶ | if ($o{no_key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1310 | 39 | return $v; | |||||||||||||||||||||||||||||||||||||||||
| 1311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1312 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1313 | 59 | return "$k=$v"; | |||||||||||||||||||||||||||||||||||||||||
| 1314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1316 | elsif (ref $v eq 'BOOL') { | ||||||||||||||||||||||||||||||||||||||||||
| 1317 | 27 | my $inner = $v->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 1318 | 27 | ▶ | die "xBOOLEAN() only accepts a plain scalar or a scalar ref, not a " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $inner and ref $inner ne "SCALAR"
MC/DC100%2/2
ref $inner and ref $inner ne "SCALAR"
ref $inner
ref $inner ne "SCALAR"
Truth table100%3/3
A and B
A= ref $innerB= ref $inner ne "SCALAR"
| |||||||||||||||||||||||||||||||||||||||||||
| 1319 | . ref($inner) . " ref\n" if ref $inner && ref $inner ne 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||
| 1320 | 26 | ▶ | my $bool_value = ref $inner eq 'SCALAR' ? $$inner : $inner; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1321 | 26 | my $escaped = _uri_escape_bytes_or_chars( _defor($bool_value, '') ); | |||||||||||||||||||||||||||||||||||||||||
| 1322 | 26 | ▶ | return $o{no_key} ? $escaped : "$k=$escaped"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1324 | elsif (ref $v eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | 15 | my @parts; | |||||||||||||||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||||||||||||||||
| 1327 | 15 | foreach my $val(@$v) { | |||||||||||||||||||||||||||||||||||||||||
| 1328 | 28 | my $part = $self->kvp2str_each(%o, key => $raw_key, value => $val, no_key => 0); | |||||||||||||||||||||||||||||||||||||||||
| 1329 | 28 | ▶ | push @parts, $part if length $part; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1331 | |||||||||||||||||||||||||||||||||||||||||||
| 1332 | 15 | ▶ | return '' if !@parts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||||||||||||||||
| 1334 | 12 | ▶ | return ($o{no_key} ? '&' : '') . join '&', @parts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1336 | elsif (ref $v eq 'CSV') { | ||||||||||||||||||||||||||||||||||||||||||
| 1337 | 16 | my @csv; | |||||||||||||||||||||||||||||||||||||||||
| 1338 | my @parts; | ||||||||||||||||||||||||||||||||||||||||||
| 1339 | |||||||||||||||||||||||||||||||||||||||||||
| 1340 | 16 | foreach my $val(@$v) { | |||||||||||||||||||||||||||||||||||||||||
| 1341 | 46 | ▶ | next if ref $val eq 'ARRAY' && !@$val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $val eq "ARRAY" and not @$val
MC/DC100%2/2
ref $val eq "ARRAY" and not @$val
ref $val eq "ARRAY"
not @$val
Truth table100%3/3
A and not(B)
A= ref $val eq "ARRAY"B= not @$val
| |||||||||||||||||||||||||||||||||||||||||||
| 1342 | |||||||||||||||||||||||||||||||||||||||||||
| 1343 | 45 | my $part = $self->kvp2str_each(%o, key => $raw_key, value => $val, no_key => 1); | |||||||||||||||||||||||||||||||||||||||||
| 1344 | |||||||||||||||||||||||||||||||||||||||||||
| 1345 | 45 | ▶ | if ($part =~ m/&/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1346 | 3 | push @parts, $part; | |||||||||||||||||||||||||||||||||||||||||
| 1347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1348 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1349 | 42 | push @csv, $part; | |||||||||||||||||||||||||||||||||||||||||
| 1350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1352 | |||||||||||||||||||||||||||||||||||||||||||
| 1353 | 16 | ▶ | my $csv = ($o{no_key} ? '' : "$k=") . join( ',', @csv); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1354 | |||||||||||||||||||||||||||||||||||||||||||
| 1355 | 16 | ▶ | if (@parts) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1356 | 3 | return join '&', $csv, map { s/^&//r } @parts; | |||||||||||||||||||||||||||||||||||||||||
| 1357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1358 | |||||||||||||||||||||||||||||||||||||||||||
| 1359 | 13 | return $csv; | |||||||||||||||||||||||||||||||||||||||||
| 1360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1361 | elsif (UNIVERSAL::isa($v, 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||||
| 1362 | 2 | die "Unable to convert nested hash value for key '$k' into a form-urlencoded query string"; | |||||||||||||||||||||||||||||||||||||||||
| 1363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | |||||||||||||||||||||||||||||||||||||||||||
| 1365 | 1 | die "Unable to convert a " . ref($v) . " reference into a form-urlencoded " | |||||||||||||||||||||||||||||||||||||||||
| 1366 | . "query string value for key '$k' - wrap it in xBOOLEAN() if you meant " | ||||||||||||||||||||||||||||||||||||||||||
| 1367 | . "a live boolean/tracked value\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 1368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||||||||||||||||
| 1370 | sub basic_authenticator { | ||||||||||||||||||||||||||||||||||||||||||
| 1371 | 5 | ▶ | my ($self, $req, $u, $p) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
basic_authenticator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1372 | 5 | return $req->headers->authorization_basic($u, $p); | |||||||||||||||||||||||||||||||||||||||||
| 1373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||||||||||||||||
| 1375 | sub _defor { | ||||||||||||||||||||||||||||||||||||||||||
| 1376 | 2454 | ▶ | my ($default, $or) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_defor: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1377 | 2454 | ▶ | return (defined($default) && length($default)) ? $default : $or; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $default and length $default
MC/DC100%2/2
defined $default and length $default
defined $default
length $default
Truth table100%3/3
A and B
A= defined $defaultB= length $default
| |||||||||||||||||||||||||||||||||||||||||||
| 1378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1379 | |||||||||||||||||||||||||||||||||||||||||||
| 1380 | 78 | ▶ | no Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1381 | |||||||||||||||||||||||||||||||||||||||||||
| 1382 | 78 | 1; | |||||||||||||||||||||||||||||||||||||||||