| 1 | package LWP::UserAgent::Caching::Simple; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | LWP::UserAgent::Caching::Simple - The first 'hard thing' made easy --- simple | ||||||||||
| 6 | |||||||||||
| 7 | =head1 VERSION | ||||||||||
| 8 | |||||||||||
| 9 | Version 0.06 | ||||||||||
| 10 | |||||||||||
| 11 | =cut | ||||||||||
| 12 | |||||||||||
| 13 | 1 | our $VERSION = '0.06'; | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use parent 'LWP::UserAgent::Caching'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 1 | ▶ | use CHI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | use parent 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | our @EXPORT_OK = qw(get_from_json); | |||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | use LWP::UserAgent::Caching::Simple; | ||||||||||
| 29 | |||||||||||
| 30 | my $ua = LWP::UserAgent::Caching::Simple->new; | ||||||||||
| 31 | |||||||||||
| 32 | my $resp = $ua->get( 'http://example.com/cached?' ); | ||||||||||
| 33 | |||||||||||
| 34 | and maybe even something quick: | ||||||||||
| 35 | |||||||||||
| 36 | # use a built-in default User-Agent for quick one timers | ||||||||||
| 37 | |||||||||||
| 38 | use LWP::UserAgent::Caching::Simple qw(get_from_json); | ||||||||||
| 39 | |||||||||||
| 40 | my $hashref = get_from_json ( | ||||||||||
| 41 | 'http://example.com/cached?', | ||||||||||
| 42 | 'Cache-Control' => 'max-stale', # without delta-seconds, unlimited | ||||||||||
| 43 | 'Cache-Control' => 'no-transform', # something not implemented | ||||||||||
| 44 | ); | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||
| 48 | |||||||||||
| 49 | This is a simplified version of L<LWP::UserAgent::Caching> with sensible | ||||||||||
| 50 | defaults and less options. For more control and more options, please use that | ||||||||||
| 51 | module. | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | 2 | ▶ | sub _chi_cache { | ||||||||
|
Subroutine
_chi_cache: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 2 | return CHI->new( | |||||||||
| 57 | driver => 'File', | ||||||||||
| 58 | root_dir => '/tmp/LWP_UserAgent_Caching', | ||||||||||
| 59 | file_extension => '.cache', | ||||||||||
| 60 | ) | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 64 | 2 | my ( $class) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 2 | my $self = $class->SUPER::new( | |||||||||
| 67 | http_caching => { | ||||||||||
| 68 | cache => _chi_cache(), | ||||||||||
| 69 | } | ||||||||||
| 70 | ); | ||||||||||
| 71 | |||||||||||
| 72 | 2 | return $self | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | { | ||||||||||
| 76 | 1 | my $ua; | |||||||||
| 77 | 0 | ▶ | sub _default_useragent { | ||||||||
|
Subroutine
_default_useragent: not called CC=2 SCAR=17.9
| |||||||||||
| 78 | 0 | ▶ | $ua = __PACKAGE__->new() unless $ua; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | return $ua | |||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 1 | ▶ | sub get_from_json { | ||||||||
|
Subroutine
get_from_json: not called CC=2 SCAR=15.7
Pod
documented
| |||||||||||
| 84 | 0 | my $resp = _default_useragent()->get(@_, Accept => 'application/json'); | |||||||||
| 85 | 0 | ▶ | return decode_json($resp->decoded_content()) if $resp->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | warn "HTTP Status message ${\$resp->code} [${\$resp->message}] GET $_[0]\n"; | |||||||||
| 87 | return | ||||||||||
| 88 | |||||||||||
| 89 | 0 | } | |||||||||
| 90 | |||||||||||
| 91 | =head1 METHODS | ||||||||||
| 92 | |||||||||||
| 93 | Since this is a subclass of L<LWP::UserAgent::Caching> it has it's methods, like | ||||||||||
| 94 | the following object methods: | ||||||||||
| 95 | |||||||||||
| 96 | =over | ||||||||||
| 97 | |||||||||||
| 98 | =item request | ||||||||||
| 99 | |||||||||||
| 100 | =item get | ||||||||||
| 101 | |||||||||||
| 102 | =item post | ||||||||||
| 103 | |||||||||||
| 104 | =item put | ||||||||||
| 105 | |||||||||||
| 106 | =item delete | ||||||||||
| 107 | |||||||||||
| 108 | =back | ||||||||||
| 109 | |||||||||||
| 110 | =head1 EXPORT_OK | ||||||||||
| 111 | |||||||||||
| 112 | =head2 get_from_json | ||||||||||
| 113 | |||||||||||
| 114 | This will simply make a GET request to a server, with the C<Accept> Header set | ||||||||||
| 115 | to C<application/json>. On succes, it will turn the returned json (as requested) | ||||||||||
| 116 | into a perl data structure. Otherwise it will be C<undef> and print a warning. | ||||||||||
| 117 | |||||||||||
| 118 | =head1 CAVEATS | ||||||||||
| 119 | |||||||||||
| 120 | This is a super simplified way of making a straightforward request. It can | ||||||||||
| 121 | handle more complex requests as well, using | ||||||||||
| 122 | |||||||||||
| 123 | my $resp = $ua->request($http_rqst); | ||||||||||
| 124 | |||||||||||
| 125 | which will give a full C<HTTP::Response> object back. The UserAgent is a full | ||||||||||
| 126 | subclass of the standard L<LWP::UserAgent>, and one can still change the setting | ||||||||||
| 127 | of that, like e.g. the C<< $ua->agent('SecretAgent/007') >>. | ||||||||||
| 128 | |||||||||||
| 129 | =head1 AUTHOR | ||||||||||
| 130 | |||||||||||
| 131 | Theo van Hoesel, C<< <Th.J.v.Hoesel at THEMA-MEDIA.nl> >> | ||||||||||
| 132 | |||||||||||
| 133 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 134 | |||||||||||
| 135 | Copyright 2016 .. 2018 Theo van Hoesel. | ||||||||||
| 136 | |||||||||||
| 137 | =cut | ||||||||||
| 138 | |||||||||||
| 139 | 1 | 1; | |||||||||