diff --git a/Changes b/Changes new file mode 100644 index 0000000..2206f27 --- /dev/null +++ b/Changes @@ -0,0 +1,6 @@ +Revision history for Perl extension MojoRPC::Client. + +0.01 Fri Oct 5 13:49:49 2012 + - original version; created by h2xs 1.23 with options + -AX MojoRPC::Client -b 5.10.0 + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..85c4e16 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,10 @@ +Changes +Makefile.PL +MANIFEST +README +t/MojoRPC-Client.t +lib/MojoRPC/Client.pm +lib/MojoRPC/Client/MethodCall.pm +lib/MojoRPC/Client/Object.pm +lib/MojoRPC/Client/Request.pm +lib/MojoRPC/Client/RequestPathBuilder.pm diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..02b59b0 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,12 @@ +use 5.010000; +use ExtUtils::MakeMaker; +# See lib/ExtUtils/MakeMaker.pm for details of how to influence +# the contents of the Makefile that is written. +WriteMakefile( + NAME => 'MojoRPC::Client', + VERSION_FROM => 'lib/MojoRPC/Client.pm', # finds $VERSION + PREREQ_PM => {}, # e.g., Module::Name => 1.1 + ($] >= 5.005 ? ## Add these new keywords supported since 5.005 + (ABSTRACT_FROM => 'lib/MojoRPC/Client.pm', # retrieve abstract from module + AUTHOR => 'Jonathan Taylor ') : ()), +); diff --git a/README b/README new file mode 100644 index 0000000..9144a5e --- /dev/null +++ b/README @@ -0,0 +1,40 @@ +MojoRPC-Client version 0.01 +=========================== + +The README is used to introduce the module and provide instructions on +how to install the module, any machine dependencies it may have (for +example C compilers and installed libraries) and any other information +that should be provided before the module is installed. + +A README file is required for CPAN modules since CPAN extracts the +README file from a module distribution so that people browsing the +archive can use it get an idea of the modules uses. It is usually a +good idea to provide version information here so that people can +decide whether fixes for the module are worth downloading. + +INSTALLATION + +To install this module type the following: + + perl Makefile.PL + make + make test + make install + +DEPENDENCIES + +This module requires these other modules and libraries: + + blah blah blah + +COPYRIGHT AND LICENCE + +Put the correct copyright and licence information here. + +Copyright (C) 2012 by Jonathan Taylor + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.14.2 or, +at your option, any later version of Perl 5 you may have available. + + diff --git a/lib/MojoRPC/Client.pm b/lib/MojoRPC/Client.pm new file mode 100644 index 0000000..c860b8d --- /dev/null +++ b/lib/MojoRPC/Client.pm @@ -0,0 +1,82 @@ +package MojoRPC::Client; + +use 5.010000; +use strict; +use warnings; +use Mojo::Base -base; +use MojoRPC::Client::Package; +use MojoRPC::Client::Request; + +our $VERSION = '0.01'; + +has [qw(base_url api_key)]; + +#Factory creates an Class of the specified type, which you can call methods on +sub factory { + my $self = shift; + my $class = shift; + + return MojoRPC::Client::Object->new({ _class => $class, _api_key => $self->api_key, _base_url => $self->base_url }); +} + +sub define { + my $self = shift; + my $class = shift; + + #CREATE the package named by $class + #croak if the package already exists and isn't one of ours + +} + +1; +__END__ +# Below is stub documentation for your module. You'd better edit it! + +=head1 NAME + +MojoRPC::Client - Perl extension for blah blah blah + +=head1 SYNOPSIS + + use MojoRPC::Client; + blah blah blah + +=head1 DESCRIPTION + +Stub documentation for MojoRPC::Client, created by h2xs. It looks like the +author of the extension was negligent enough to leave the stub +unedited. + +Blah blah blah. + +=head2 EXPORT + +None by default. + + + +=head1 SEE ALSO + +Mention other useful documentation such as the documentation of +related modules or operating system documentation (such as man pages +in UNIX), or any relevant external documentation such as RFCs or +standards. + +If you have a mailing list set up for your module, mention it here. + +If you have a web site set up for your module, mention it here. + +=head1 AUTHOR + +Jonathan Taylor, Ejon@localE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2012 by Jonathan Taylor + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.14.2 or, +at your option, any later version of Perl 5 you may have available. + + +=cut diff --git a/lib/MojoRPC/Client/MethodCall.pm b/lib/MojoRPC/Client/MethodCall.pm new file mode 100644 index 0000000..35232ec --- /dev/null +++ b/lib/MojoRPC/Client/MethodCall.pm @@ -0,0 +1,16 @@ +package MojoRPC::Client::MethodCall; +use Mojo::Base -base; + +has [qw( method_name parameters call_type wants )]; + +sub path { + my $self = shift; + + $method_call = $self->wants; + $method_call .= $self->call_type; + $method_call .= $self->method_name(); + + return ($method_call, @{$self->parameters}); +} + +1; \ No newline at end of file diff --git a/lib/MojoRPC/Client/Object.pm b/lib/MojoRPC/Client/Object.pm new file mode 100644 index 0000000..7f557a8 --- /dev/null +++ b/lib/MojoRPC/Client/Object.pm @@ -0,0 +1,60 @@ +package MojoRPC::Client::Object; +use Mojo::Base -base; +use MojoRPC::Client::RequestPathBuilder; +use Want; + +has [qw( _class _base_url _api_key )]; +has _chain => MojoRPC::Client::RequestPathBuilder->new(); + + +#Build up the path builder as we go along (even if its only one level) +#Detect if we are the end of the chain and do the request + +sub _finish_chain { + my $self = shift; + + my $request_object = $self->_new_request_object(); + return $request_object->send_request(); +} + +sub _new_request_object { + my $self = shift; + my $request_object = MojoRPC::Client::Request->new({ + api_key => $self->_api_key, + base_url => $self->_base_url, + request_path_builder => $self->_chain + }); + return $request_object; +} + +sub _chain { + my $self = shift; + my $args = shift; + + $self-_chain->add_to_chain($args); + + return $self; #So that we can keep chaining +} + + +sub AUTOLOAD { + my $self = shift; + ( my $method = $AUTOLOAD ) =~ s{.*::}{}; + + my $chain = $self->_chain({ method => $method, parameters => \@_, wants => wantarray ? '@' : '$', call_type => '->' }); + unless (want('CODE')) { + # if(want('LIST') { + # return $self->_finish_chain(); + # } + # else { + + # } + return $self->_finish_chain(); + } + + return $chain; +} + +sub DESTROY {} + +1; \ No newline at end of file diff --git a/lib/MojoRPC/Client/Request.pm b/lib/MojoRPC/Client/Request.pm new file mode 100644 index 0000000..1f3897b --- /dev/null +++ b/lib/MojoRPC/Client/Request.pm @@ -0,0 +1,56 @@ +package MojoRPC::Client::Request; +use Mojo::Base -base; +use HTTP::Request; +use MIME::Base64; +use JSON::XS; +use LWP; +use URI; +use Carp; +use MojoRPC::Client::Object; + +has [qw( base_url api_key request_path_builder )]; + +sub send_request { + my $self = shift; + + $request_path_builder->base_url($self->base_url); #Start to get spaghetti ish + my $http_request = HTTP::Request->new(GET => $self->request_path_builder->build ); + $http_request->header(Authorization => "Basic " . encode_base64($self->api_key())); + + my $response = $self->user_agent->request($http_request); + + unless($self->response->is_success()) { + carp "Request failed with " . $self->response->status_line; + return undef; + } + + $response = $self->parse_response($response); + + return $response; +} + +sub parse_response { + my $self = shift; + my $response = shift; + + my $json = JSON::XS->new->allow_nonref; + my $data = $json->decode($self->response->content) ; + + if($data->{class}) { + my $object = MojoRPC::Client::Object->new({ + _class => $data->{class}, _api_key => $self->api_key, _base_url => $self->base_url, _chain => $self->request_path_builder + }); + return $object; + } + return $data->{data}; +} + +sub user_agent { + my $self = shift; + return $self->{user_agent} if defined $self->{user_agent}; + $self->{user_agent} = LWP::UserAgent->new; + $self->{user_agent}->agent("MojoRPC::Client $MojoRPC::Client::VERSION"); + return $self->{user_agent}; +} + +1; \ No newline at end of file diff --git a/lib/MojoRPC/Client/RequestPathBuilder.pm b/lib/MojoRPC/Client/RequestPathBuilder.pm new file mode 100644 index 0000000..9334f84 --- /dev/null +++ b/lib/MojoRPC/Client/RequestPathBuilder.pm @@ -0,0 +1,36 @@ +package MojoRPC::Client::RequestPathBuilder; +use Mojo::Base -base; +use MojoRPC::Client::MethodCall; +use JSON::XS; + +#We use JSON here for convenience in generating the path + +has chain => []; +has [qw( base_url class_name )]; + +#Should complain if you try and add to a chain thats last item wants an array +sub add_to_chain { + my $self = shift; + my $args = shift; + + push @{$self->chain}, MojoRPC::Client::MethodCall->new( + method_name => $args->{method_name}, parameters => $args->{parameters}, + call_type => $args->{call_type}, wants => $args->{wants} + ); +} + +#Put it all together +sub build { + +} + +sub json_params { + my $self = shift; + my @path = map { $_->path() } @{$self->chain}; + + my $json = JSON::XS->new->allow_non_ref(); + + return $json->encode(\@path); +} + +1; \ No newline at end of file diff --git a/t/MojoRPC-Client.t b/t/MojoRPC-Client.t new file mode 100644 index 0000000..bb65ce7 --- /dev/null +++ b/t/MojoRPC-Client.t @@ -0,0 +1,18 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl MojoRPC-Client.t' + +######################### + +# change 'tests => 1' to 'tests => last_test_to_print'; + +use strict; +use warnings; + +use Test::More tests => 1; +BEGIN { use_ok('MojoRPC::Client') }; + +######################### + +# Insert your test code below, the Test::More module is use()ed here so read +# its man page ( perldoc Test::More ) for help writing this test script. +