WWW-Crab-Client

 view release on metacpan or  search on metacpan

lib/WWW/Crab/Client.pm  view on Meta::CPAN

# _write_json()
#
# Sends the given object to the Crab server as a JSON message.
#
#   $self->_write_json($self->_get_url($ACTION), $HASHREF, $READ);
#
# Dies on failure, and returns 1 on success, unless $READ is given a
# true value, when it attempts to decode the server response as JSON
# and return the decoded object.  If there is no response, it returns
# a reference to an empty hash.

sub _write_json {
    my $self = shift;
    my $url = shift;
    my $obj = shift;
    my $read = shift;

    my $ua = new LWP::UserAgent(timeout => $self->{'timeout'});
    my $req = new HTTP::Request(PUT => $url);
    $req->content(encode_json($obj));
    my $res = $ua->request($req);
    die $res->status_line() unless $res->is_success();
    return 1 unless $read;

    my $content = $res->decoded_content();
    return {} unless $content;

    return decode_json($content);
}

# _get_url()
#
# Returns the URL to be used for a given Crab aaction.
#
#     my $url = $self->_get_url($ACTION);
#
# Where the action is typically 'start' or 'finish'.

sub _get_url {
    my $self = shift;
    my $action = shift;

    my @path = ($self->{'hostname'}, $self->{'username'});
    push @path, $self->{'id'} if defined $self->{'id'};

    return 'http://' . $self->{'server'} . ':' . $self->{'port'} . '/' .
           join('/', 'api', '0', $action, @path);
}

# _get_username()
#
# Detects the username of the current user.
#
# This provides the default value for the username parameter
# of the WWW::Crab::Client constructor.

sub _get_username {
    my $username = undef;

    eval {
        $username = scalar getpwuid($<);
    };

    return $username if defined $username;

    eval {
        require Win32;
        $username = Win32::LoginName();
    };

    return $username if defined $username;

    return 'user';
}

1;

__END__

=back

=head1 AUTHOR

Graham Bell <g.bell@eaobservatory.org>

=head1 COPYRIGHT

Copyright (C) 2012-2013 Science and Technology Facilities Council.
Copyright (C) 2016 East Asian Observatory.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.



( run in 1.036 second using v1.01-cache-2.11-cpan-ff9377addf4 )