root/lang/perl/WebService-Simple/trunk/lib/WebService/Simple.pm @ 8551

Revision 8551, 2.4 kB (checked in by yusukebe, 7 years ago)

addd URI escape

Line 
1package WebService::Simple;
2
3use warnings;
4use strict;
5use Carp;
6use URI::Escape;
7use LWP::UserAgent;
8use URI::Escape;
9use WebService::Simple::Response;
10
11our $VERSION = '0.01';
12
13sub new {
14    my ($class, $base_url, $param) = @_;
15    croak "paramater base_url is required" unless $base_url;
16    my $self = bless {
17                      base_url => $base_url,
18                      param => $param,
19                  }, $class;
20    $self;
21}
22
23sub get {
24    my ($self, $request_param) = @_;
25    my $url = $self->_make_url($request_param);
26    my $ua = LWP::UserAgent->new;
27    my $response = $ua->get($url);
28    croak "can't get the request" unless $response->is_success;
29    return $response;
30}
31sub _make_url{
32    my ($self, $request_param) = @_;
33    my $base_url = $self->{base_url};
34    my $url = $base_url =~ /\?$/ ? $base_url : $base_url . "?";
35    my @params;
36    map {push(@params, "$_=" . URI::Escape::uri_escape_utf8($self->{param}->{$_}))}
37        keys %{$self->{param}};
38    map {push(@params, "$_=" . URI::Escape::uri_escape_utf8($request_param->{$_}))}
39        keys %$request_param;
40    my $str = join("&",@params);
41    $url .= $str;
42    return $url;
43}
44
451;
46__END__
47
48=head1 NAME
49
50WebService::Simple - Simple interface to any Web Service APIs
51
52=head1 VERSION
53
54This document describes WebService::Simple version 0.01
55
56
57=head1 SYNOPSIS
58
59    use WebService::Simple;
60
61    my $flickr = WebService::Simple->new(
62        "http://api.flickr.com/services/rest/",
63        { api_key => "your_api_key", }
64    );
65    my $response =
66      $flickr->get( { method => "flickr.test.echo", name => "value" } );
67    my $ref = $response->parse_xml( { forcearray => [], keyattr => [] } );
68    print $ref->{name} . "\n";
69
70=head1 DESCRIPTION
71
72WebService::Simple provides you a simple interface to any Web Servcie APIs
73
74=head1 METHODS
75
76=over 4
77
78=item new(I<%args>)
79
80    my $flickr = WebService::Simple->new(
81        "http://api.flickr.com/services/rest/",
82        { api_key => "your_api_key", }
83    );
84
85Create and return a new WebService::Simple object.
86"new" Method requires an base_url of Web Service API.
87
88=item get(I<%args>)
89
90    my $response =
91      $flickr->get( { method => "flickr.test.echo", name => "value" } );
92
93Get the WebService::Simple::Response object.
94
95=back
96
97=head1 AUTHOR
98
99Yusuke Wada  C<< <yusuke@kamawada.com> >>
100
101
102=head1 LICENCE AND COPYRIGHT
103
104Copyright (c) 2008, Yusuke Wada C<< <yusuke@kamawada.com> >>. All rights reserved.
105
106This module is free software; you can redistribute it and/or
107modify it under the same terms as Perl itself. See L<perlartistic>.
108
Note: See TracBrowser for help on using the browser.