Changeset 8608 for lang/perl/WebService-Simple/trunk/lib
- Timestamp:
- 04/01/08 21:02:22 (9 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/WebService-Simple/trunk/lib/WebService/Simple.pm
r8554 r8608 8 8 use URI::Escape; 9 9 use WebService::Simple::Response; 10 use Data::Dumper; 10 11 11 our $VERSION = '0.0 1';12 our $VERSION = '0.02'; 12 13 13 14 sub new { 14 my ($class, $base_url, $param) = @_;15 croak "paramater base_url is required" unless $ base_url;15 my ($class, %opt) = @_; 16 croak "paramater base_url is required" unless $opt{base_url}; 16 17 my $self = bless { 17 base_url => $base_url,18 param => $param,18 ua => LWP::UserAgent->new, 19 %opt, 19 20 }, $class; 20 21 $self; … … 24 25 my ($self, $request_param) = @_; 25 26 my $url = $self->_make_url($request_param); 26 my $ua = LWP::UserAgent->new; 27 my $response = $ua->get($url); 27 my $response = $self->_fetch_url($url); 28 return $response; 29 } 30 31 sub _fetch_url{ 32 my ($self,$url) = @_; 33 my $response; 34 if(exists $self->{cache}){ 35 $response = $self->{cache}->thaw($url); 36 if(defined $response){ 37 return $response; 38 } 39 } 40 $response = $self->{ua}->get($url); 28 41 croak "can't get the request" unless $response->is_success; 42 if(exists $self->{cache}) { 43 $self->{cache}->freeze($url, $response); 44 } 29 45 return $response; 30 46 } … … 60 76 61 77 my $flickr = WebService::Simple->new( 62 "http://api.flickr.com/services/rest/",63 { api_key => "your_api_key", }78 base_url => "http://api.flickr.com/services/rest/", 79 param => { api_key => "your_api_key", } 64 80 ); 65 81 my $response = … … 79 95 80 96 my $flickr = WebService::Simple->new( 81 "http://api.flickr.com/services/rest/",82 { api_key => "your_api_key", }97 base_url => "http://api.flickr.com/services/rest/", 98 param => { api_key => "your_api_key", } 83 99 ); 84 100 … … 95 111 =back 96 112 113 =head1 CACHING 114 115 Cache the response of Web Service APIs by using Cache object. 116 Here's an example. 117 118 my $cache = Cache::File->new( 119 cache_root => '/tmp/mycache', 120 default_expires => '30 min', 121 ); 122 123 my $flickr = WebService::Simple->new( 124 base_url => "http://api.flickr.com/services/rest/", 125 cache => $cache, 126 param => { api_key => $api_key, } 127 ); 128 129 97 130 =head1 AUTHOR 98 131
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)