IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6584


Ignore:
Timestamp:
Mar 14, 2006, 1:24:05 PM (20 years ago)
Author:
jhoblitt
Message:

add DataStore::Product->request

Location:
trunk/DataStore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/DataStore/lib/DataStore/Product.pm

    r6538 r6584  
    11# Copyright (C) 2006  Joshua Hoblitt
    22#
    3 # $Id: Product.pm,v 1.2 2006-03-08 00:02:43 jhoblitt Exp $
     3# $Id: Product.pm,v 1.3 2006-03-14 23:24:05 jhoblitt Exp $
    44
    55package DataStore::Product;
     
    1414
    1515use Carp qw( carp );
     16use DataStore::FileSet::Parser;
    1617use DataStore::Record;
     18use DataStore::Response;
     19use LWP::UserAgent;
    1720use Params::Validate qw( validate SCALAR);
    1821
     
    108111sub request
    109112{
     113    my $self = shift;
    110114
     115    # no params
     116    validate(@_, {});
     117
     118    # make request
     119    my $ua = LWP::UserAgent->new;
     120    my $request = HTTP::Request->new(GET => $self->uri);
     121    my $response = $ua->request($request);
     122
     123    if (! $response->is_success) {
     124        carp $response->status_line;
     125        return undef;
     126    }
     127
     128    # parse document
     129    my $parser = DataStore::FileSet::Parser->new;
     130
     131    my $data = $parser->parse($response->content);
     132
     133    unless ($data) {
     134        carp "Product contains no FileSets";
     135        return undef;
     136    }
     137
     138
     139    # return DS::Response object
     140    my $dsr = DataStore::Response->new(
     141        is_success  => 1,
     142        code        => $response->code,
     143        status_line => $response->status_line,
     144        data        => $data,
     145        request     => $self,
     146    );
    111147}
    112 
    113148
    1141491;
  • trunk/DataStore/t/05_product.t

    r6538 r6584  
    33# Copyright (C) 2006  Joshua Hoblitt
    44#
    5 # $Id: 05_product.t,v 1.1 2006-03-08 00:02:43 jhoblitt Exp $
     5# $Id: 05_product.t,v 1.2 2006-03-14 23:24:05 jhoblitt Exp $
    66
    77use strict;
     
    1010use lib qw( ./lib ./t );
    1111
    12 use Test::More tests => 10;
     12use Test::More tests => 21;
    1313
    1414=head1 NAME
     
    9999# ->request()
    100100
     101use Net::HTTPServer;
     102
     103sub bar
     104{
     105    my $req = shift;             # Net::HTTPServer::Request object
     106    my $res = $req->Response();  # Net::HTTPServer::Response object
     107
     108    my $string = <<END;
     109# filesetID|time registered    |type   |telescope pointing         |etime|f|airmass|
     110otis0123456|2006-01-01T00:03:04Z|object|11:00:10.33 68:26:59.6 2000|30.0 |r|1.23   |
     111otis0123456|2006-01-01T00:03:04Z|object|11:00:10.33 68:26:59.6 2000|30.0 |r|1.23   |
     112otis0123456|2006-01-01T00:03:04Z|object|11:00:10.33 68:26:59.6 2000|30.0 |r|1.23   |
     113otis0123456|2006-01-01T00:03:04Z|object|11:00:10.33 68:26:59.6 2000|30.0 |r|1.23   |
     114END
     115
     116    $res->Print($string);
     117
     118    return $res;
     119}
     120
     121$|++;
     122
     123$SIG{CHLD} = 'IGNORE';
     124my $pid = open(my $child, "-|");
     125
     126unless ($pid) {
     127    my $server = new Net::HTTPServer( port => 'scan' );
     128
     129    # send port number to parent
     130    print $server->Start(), "\n";
     131
     132    $server->RegisterURL('/', \&bar);
     133    $server->Process();  # Run forever
     134
     135    exit -1;
     136}
     137
     138my $port = <$child>;
     139chomp $port;
     140
     141{
     142    my $dsp = DataStore::Product->new( uri => "http://localhost:$port/" );
     143
     144    isa_ok($dsp->request, 'DataStore::Response');
     145}
     146
     147{
     148    my $dsp = DataStore::Product->new( uri => "http://localhost:$port/" );
     149
     150    my $dsr = $dsp->request;
     151
     152    ok($dsr->is_success, "response->is_success() is correct");
     153    is($dsr->code, 200, "response->code() is correct");
     154    is($dsr->status_line, "200 OK", "response->status_line() is correct");
     155    is(ref $dsr->data, 'ARRAY', "response->data is arrayref is correct");
     156    isa_ok($dsr->request, 'DataStore::Record');
     157
     158    my $data = $dsr->data;
     159    is(scalar @$data, 4, "data has the correct number of rows");
     160    isa_ok(@$data[0], 'DataStore::FileSet', "data has correct type of rows");
     161    isa_ok(@$data[1], 'DataStore::FileSet', "data has correct type of rows");
     162    isa_ok(@$data[2], 'DataStore::FileSet', "data has correct type of rows");
     163    isa_ok(@$data[3], 'DataStore::FileSet', "data has correct type of rows");
     164}
     165
     166# cleanup HTTP server
     167kill 9, $pid;
Note: See TracChangeset for help on using the changeset viewer.