File: Client.pm

package info (click to toggle)
libtest-lwp-useragent-perl 0.036-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: perl: 530; makefile: 10
file content (37 lines) | stat: -rw-r--r-- 834 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package MyApp::Client;
use strict;
use warnings;

use Moose;
use LWP::UserAgent;
use JSON::MaybeXS;

has useragent => (
    is => 'ro', isa => 'LWP::UserAgent',
    lazy => 1,
    default => sub { LWP::UserAgent->new },
);

sub get_indexes
{
    my ($self, %args) = @_;

    my $user = $args{user};

    # call our server to get the data
    my $url = "http://myserver.com/user/$user";
    my $response = $self->useragent->get($url);

    if ($response->code ne '200'
        or $response->headers->content_type ne 'application/json')
    {
        warn "network timeout when fetching $url" if $response->decoded_content =~ /time(?:d )?out/;
        return;
    }

    # parse JSON data from response
    my $data = decode_json($response->decoded_content);
    return @{$data->{post_ids} // []};
}
__PACKAGE__->meta->make_immutable;