Changeset 13005 for lang/perl/WebService-Simple/trunk
- Timestamp:
- 06/01/08 22:34:52 (6 months ago)
- Location:
- lang/perl/WebService-Simple/trunk
- Files:
-
- 4 modified
-
example/flickr_search.pl (modified) (1 diff)
-
lib/WebService/Simple.pm (modified) (7 diffs)
-
lib/WebService/Simple/Parser/XML/Simple.pm (modified) (2 diffs)
-
lib/WebService/Simple/Response.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/WebService-Simple/trunk/example/flickr_search.pl
r12988 r13005 1 1 use strict; 2 2 use warnings; 3 4 use XML::Simple; 3 5 use WebService::Simple; 6 use WebService::Simple::Parser::XML::Simple; 4 7 use Data::Dumper; 5 8 6 9 my $api_key = $ARGV[0] || "your_api_key"; 7 10 11 my $xs = XML::Simple->new( keyattr => [] ); 12 my $parser = WebService::Simple::Parser::XML::Simple->new( xs => $xs ); 8 13 my $flickr = WebService::Simple->new( 9 14 base_url => "http://api.flickr.com/services/rest/", 10 param => { api_key => $api_key, } 15 param => { api_key => $api_key, }, 16 response_parser => $parser, 11 17 ); 12 18 13 19 my $response = 14 20 $flickr->get( { method => "flickr.photos.search", text => "cat" } ); 15 print Dumper $response->parse_response ( KeyAttr => [] );21 print Dumper $response->parse_response; -
lang/perl/WebService-Simple/trunk/lib/WebService/Simple.pm
r12993 r13005 7 7 use Class::Inspector; 8 8 use Data::Dumper (); 9 use Digest::MD5 ();9 use Digest::MD5 (); 10 10 use URI::Escape; 11 11 use WebService::Simple::Response; … … 15 15 16 16 __PACKAGE__->config( 17 base_url => '', 18 response_parser => { 19 module => "XML::Simple" 20 }, 17 base_url => '', 18 response_parser => { module => "XML::Simple" }, 21 19 ); 22 20 23 sub new 24 { 25 my $class = shift; 26 my %args = @_; 27 my $base_url = delete $args{base_url} || 28 $class->config->{base_url} || 29 Carp::croak("base_url is required"); 21 sub new { 22 my $class = shift; 23 my %args = @_; 24 my $base_url = delete $args{base_url} 25 || $class->config->{base_url} 26 || Carp::croak("base_url is required"); 30 27 my $basic_params = delete $args{params} || delete $args{param} || {}; 31 my $debug = delete $args{debug} || 0; 32 33 my $response_parser = delete $args{response_parser} || 34 $class->config->{response_parser}; 35 if (! $response_parser || ! eval { $response_parser->isa('WebService::Simple::Parser') }) { 28 my $debug = delete $args{debug} || 0; 29 30 my $response_parser = delete $args{response_parser} 31 || $class->config->{response_parser}; 32 if ( !$response_parser 33 || !eval { $response_parser->isa('WebService::Simple::Parser') } ) 34 { 36 35 my $config = $response_parser || $class->config->{response_parser}; 37 if ( ! ref $config) {36 if ( !ref $config ) { 38 37 $config = { module => $config }; 39 38 } 40 39 my $module = $config->{module}; 41 if ( $module !~ s/^\+//) {40 if ( $module !~ s/^\+// ) { 42 41 $module = __PACKAGE__ . "::Parser::$module"; 43 42 } 44 if ( ! Class::Inspector->loaded($module)) {43 if ( !Class::Inspector->loaded($module) ) { 45 44 $module->require or die; 46 45 } 47 46 $response_parser = $module->new( %{ $config->{args} || {} } ); 48 } ;47 } 49 48 50 49 my $cache = delete $args{cache}; 51 if ( ! $cache || ref $cache eq 'HASH') {50 if ( !$cache || ref $cache eq 'HASH' ) { 52 51 my $config = ref $cache eq 'HASH' ? $cache : $class->config->{cache}; 53 52 if ($config) { 54 if ( ! ref $config) {53 if ( !ref $config ) { 55 54 $config = { module => $config }; 56 55 } 57 56 58 57 my $module = $config->{module}; 59 if ( ! Class::Inspector->loaded($module)) {58 if ( !Class::Inspector->loaded($module) ) { 60 59 $module->require or die; 61 60 } 62 $cache = $module->new( $config->{hashref_args} ? $config->{args} : %{ $config->{args} } ); 61 $cache = 62 $module->new( $config->{hashref_args} 63 ? $config->{args} 64 : %{ $config->{args} } ); 63 65 } 64 66 } 65 67 66 68 my $self = $class->SUPER::new(%args); 67 $self->{base_url} = URI->new($base_url);68 $self->{basic_params} = $basic_params;69 $self->{base_url} = URI->new($base_url); 70 $self->{basic_params} = $basic_params; 69 71 $self->{response_parser} = $response_parser; 70 $self->{cache} = $cache;71 $self->{debug} = $debug;72 $self->{cache} = $cache; 73 $self->{debug} = $debug; 72 74 return $self; 73 75 } 74 76 75 sub base_url { $_[0]->{base_url} }76 sub basic_params { $_[0]->{basic_params} }77 sub base_url { $_[0]->{base_url} } 78 sub basic_params { $_[0]->{basic_params} } 77 79 sub response_parser { $_[0]->{response_parser} } 78 sub cache { $_[0]->{cache} } 79 80 sub __cache_get 81 { 80 sub cache { $_[0]->{cache} } 81 82 sub __cache_get { 82 83 my $self = shift; 83 84 my $cache = $self->cache; 84 85 return unless $cache; 85 86 86 my $key = $self->__cache_key( shift);87 my $key = $self->__cache_key(shift); 87 88 return $cache->get( $key, @_ ); 88 89 } 89 90 90 sub __cache_set 91 { 91 sub __cache_set { 92 92 my $self = shift; 93 93 my $cache = $self->cache; 94 94 return unless $cache; 95 95 96 my $key = $self->__cache_key( shift);96 my $key = $self->__cache_key(shift); 97 97 return $cache->set( $key, @_ ); 98 98 } 99 99 100 sub __cache_remove 101 { 100 sub __cache_remove { 102 101 my $self = shift; 103 102 my $cache = $self->cache; 104 103 return unless $cache; 105 104 106 my $key = $self->__cache_key( shift);105 my $key = $self->__cache_key(shift); 107 106 return $cache->remove( $key, @_ ); 108 107 } 109 108 110 sub __cache_key 111 { 112 my $self = shift; 109 sub __cache_key { 110 my $self = shift; 113 111 local $Data::Dumper::Indent = 1; 114 112 local $Data::Dumper::Terse = 1; … … 121 119 my %args = @_; 122 120 123 my $uri = URI->new( $args{url});124 if (my $extra_path = $args{extra_path}){121 my $uri = URI->new( $args{url} ); 122 if ( my $extra_path = $args{extra_path} ) { 125 123 $extra_path =~ s!^/!!; 126 $uri->path( $uri->path . $extra_path );124 $uri->path( $uri->path . $extra_path ); 127 125 } 128 126 129 127 my $params = $args{params}; 130 128 if ($params) { 131 foreach my $key ( keys %$params) {132 if ( utf8::is_utf8($params->{$key})) {129 foreach my $key ( keys %$params ) { 130 if ( utf8::is_utf8( $params->{$key} ) ) { 133 131 $params->{$key} = utf8::encode( $params->{$key} ); 134 132 } 135 133 } 136 $uri->query_form( %$params);134 $uri->query_form(%$params); 137 135 } 138 136 … … 142 140 sub get { 143 141 my $self = shift; 144 my ($url, %extra); 145 146 if (ref $_[0] eq 'HASH') { 147 $url = ""; 148 %extra = %{shift @_}; 149 } else { 142 my ( $url, %extra ); 143 144 if ( ref $_[0] eq 'HASH' ) { 145 $url = ""; 146 %extra = %{ shift @_ }; 147 } 148 else { 150 149 $url = shift @_; 151 if ( ref $_[0] eq 'HASH') {152 %extra = %{ shift @_ } 150 if ( ref $_[0] eq 'HASH' ) { 151 %extra = %{ shift @_ }; 153 152 } 154 153 } … … 157 156 url => $self->base_url, 158 157 extra_path => $url, 159 params => { %{ $self->basic_params}, %extra }160 ); 161 print "Request URL is $uri\n" if $self->{debug};158 params => { %{ $self->basic_params }, %extra } 159 ); 160 print "Request URL is $uri\n" if $self->{debug}; 162 161 163 162 my @headers = @_; 164 163 165 164 my $response; 166 $response = $self->__cache_get( [$uri, @headers]);165 $response = $self->__cache_get( [ $uri, @headers ] ); 167 166 if ($response) { 168 167 return $response; 169 168 } 170 169 171 $response = $self->SUPER::get( $uri, @headers);172 if ( ! $response->is_success) {170 $response = $self->SUPER::get( $uri, @headers ); 171 if ( !$response->is_success ) { 173 172 Carp::croak("request to $uri failed"); 174 173 } … … 178 177 parser => $self->response_parser 179 178 ); 180 $self->__cache_set( [$uri, @headers], $response);179 $self->__cache_set( [ $uri, @headers ], $response ); 181 180 return $response; 182 181 } 183 182 184 sub post 185 { 186 my ($self, $url, @params) = @_; 183 sub post { 184 my ( $self, $url, @params ) = @_; 187 185 188 186 # XXX - do not include params 189 187 my $uri = $self->request_url( 190 url => $self->base_url,188 url => $self->base_url, 191 189 extra_path => $url 192 190 ); … … 195 193 # of push 196 194 unshift @params, %{ $self->basic_params }; 197 my $response = $self->SUPER::post( $uri, @params );198 199 if ( ! $response->is_success) {200 Carp::croak( "request to $url failed: " . $response->status_line);195 my $response = $self->SUPER::post( $uri, @params ); 196 197 if ( !$response->is_success ) { 198 Carp::croak( "request to $url failed: " . $response->status_line ); 201 199 } 202 200 $response = WebService::Simple::Response->new_from_response( -
lang/perl/WebService-Simple/trunk/lib/WebService/Simple/Parser/XML/Simple.pm
r12988 r13005 7 7 use XML::Simple; 8 8 9 sub new { 10 my $class = shift; 11 my %args = @_; 12 my $xs = delete $args{xs} || XML::Simple->new; 13 my $self = $class->SUPER::new(%args); 14 $self->{xs} = $xs; 15 return $self; 16 } 17 9 18 sub parse_response { 10 my $self = shift; 11 my $response = shift; 12 my %opt = @_; 13 return XMLin( $response->content, %opt ); 19 my $self = shift; 20 $self->{xs}->XMLin( $_[0]->content ); 14 21 } 15 22 … … 24 31 =head1 METHODS 25 32 33 =head2 new 34 26 35 =head2 parse_response 27 36 -
lang/perl/WebService-Simple/trunk/lib/WebService/Simple/Response.pm
r12988 r13005 20 20 sub parse_response { 21 21 my $self = shift; 22 my %opt = @_; 23 return $self->{__parser}->parse_response( $self, %opt ); 22 return $self->{__parser}->parse_response( $self ); 24 23 } 25 24
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)