Astro-Catalog

 view release on metacpan or  search on metacpan

lib/Astro/Catalog/Transport/REST.pm  view on Meta::CPAN


=cut

sub _default_useragent_id {
    my $self = shift;
    my $HOST = hostname();
    my $DOMAIN = hostdomain();
    my $package = ref($self);
    my $pack_version;
    {
        # Need a symbolic reference
        no strict 'refs';
        $pack_version = ${ $package."::VERSION" };
    }
    $pack_version = 'UNKNOWN' unless defined $pack_version;
    return "Astro::Catalog::REST/$pack_version ($HOST.$DOMAIN)";
}


=item B<_make_query>

Private function used to make an query. Should not be called directly,
since it does not parse the results. Instead use the querydb()
method.

=cut

sub _make_query {
    my $self = shift;

    # clean out the buffer
    $self->{BUFFER} = "";

    # Build the query URL
    my $URL = $self->_build_query();

    # Run the actual HTTP query
    # and get the retrieved buffer
    $self->{BUFFER} = $self->_fetch_url( $URL );

    return;
}

=item B<_fetch_url>

Simple wrapper around LWP to retrieve content from a remote
URL and return it as a single string.

    $result = $q->_fetch_url($URL);

=cut

sub _fetch_url {
    my $self = shift;
    my $URL = shift;

    # grab the user agent
    my $ua = $self->useragent;

    # build request
    my $request = new HTTP::Request('GET', $URL);

    # grab page from web
    my $reply = $ua->request($request);

    # Look at the result to see if it worked
    if (${$reply}{"_rc"} eq 200) {
        # stuff the page contents into the buffer
        return ${$reply}{"_content"};
    }
    else {
        croak("Error ${$reply}{_rc}: Failed to establish network connection using url $URL");
    }
}

=item B<_build_query>

Build the URL to be sent to the remote service. The default method
concatenates the C<query_url> along with all the defined query options
combined using key=value pairs separated by &.

    $url = $q->_build_query();

If the URL can not be built simply by concatenation (eg it requires
token replacement), then a subclassed method will be required.

=cut

sub _build_query {
    my $self = shift;

    # grab the base URL
    my $URL = $self->query_url;
    my $options = "";

    # loop round all the options keys and build the query
    my %allow = $self->_get_allowed_options;

    # Translate options
    my %translated = $self->_translate_options();

    foreach my $key ( keys %translated) {
        $options .= "&$key=" . $translated{$key}
            if defined $translated{$key};
    }

    # Remove the leading ampersand from the options list because
    # it can cause some forms to fail.
    $options =~ s/^&//;

    # build final query URL
    $URL = $URL . $options;

    return $URL;
}

1;

__END__

=back



( run in 2.260 seconds using v1.01-cache-2.11-cpan-99c4e6809bf )