Top Banner
Mike F - mrf Friday, 25 July 14
55

Automated RESTful DB's

Aug 27, 2014

Download

Software

Michael Francis

Automated RESTful Database access using Perl.

Note this has largely been replaced by the newly release WebAPI::DBIC
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Mike F - mrf Friday, 25 July 14
  • Automated RESTful DB Mike F - mrf Friday, 25 July 14
  • Mike F - mrf Friday, 25 July 14
  • Friday, 25 July 14
  • REST Friday, 25 July 14
  • REST Client-Server Friday, 25 July 14
  • REST Client-Server Stateless Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • Friday, 25 July 14
  • PSGI Friday, 25 July 14
  • PSGI Layer Communication Specication Friday, 25 July 14
  • PSGI Layer Communication Specication Implemented as Plack Friday, 25 July 14
  • PSGI Layer Communication Specication Implemented as Plack Plack::Middleware::* Friday, 25 July 14
  • PSGI Layer Communication Specication Implemented as Plack Plack::Middleware::* Layers separate functions Friday, 25 July 14
  • use Plack::Builder; use MyApp; builder { enable qw/Debug Session Auth::Basic/; MyApp->as_psgi($args); } Friday, 25 July 14
  • use Plack::Builder; use Plack::App::Rest; builder { enable qw/Debug Session Auth::Basic/; Plack::App::Rest->init( persistence_mapper => DBIC, persistence_arg => [ db_connection_args => $dsn ] ); } Friday, 25 July 14
  • sub init { my $class = shift; my $self = $class->new(@_); return $self->init_rest_resource; } sub init_rest_resource { my ($self) = @_; my $resource_initialiser = Plack::Util::load_class( $self->persistence_mapper, 'Plack::App::REST' ); $resource_initialiser->new( @{$self->persistence_args}, )->to_psgi_app; } Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • Friday, 25 July 14
  • Uniform Interface Friday, 25 July 14
  • Uniform Interface Identication Friday, 25 July 14
  • Uniform Interface Identication Manipulation Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Hypermedia driven Friday, 25 July 14
  • Friday, 25 July 14
  • Web::Simple Friday, 25 July 14
  • Web::Simple Small and Lightweight Friday, 25 July 14
  • Web::Simple Small and Lightweight Supports PSGI Friday, 25 July 14
  • Web::Simple Small and Lightweight Supports PSGI Cares about Identication Friday, 25 July 14
  • sub dispatch_request { my ($self) = @_; map { my $resource = $_; ( "GET + /".$resource."/ + ?*" => sub { my ($self, $params) = @_; Web::Machine->new( resource => 'Plack::App::REST::DBIC::GET::Resource', resource_args => [ db_schema => $self->db_schema, db_table => $resource, db_params => $params, ], ); }, ); } $self->db_schema->sources; } Friday, 25 July 14
  • sub dispatch_request { my ($self) = @_; map { my $resource = $_; ( "GET + /".$resource."/ + ?*" => sub { my ($self, $params) = @_; Web::Machine->new( resource => 'Plack::App::REST::DBIC::GET::Resource', resource_args => [ db_schema => $self->db_schema, db_table => $resource, db_params => $params, ], ); }, ); } $self->db_schema->sources; } Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Hypermedia driven Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Hypermedia driven Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Hypermedia driven Friday, 25 July 14
  • Web::Machine Based on Erlang and Pythons WebMachines Automatic Handling of Status Understands HTTP ow Friday, 25 July 14
  • Friday, 25 July 14
  • sub content_types_provided { [ {'application/json' => 'to_json'}, ]; } sub to_json { my $self = shift; return 404 unless $response; # A status code was returned return $response if ref $response eq 'SCALAR'; return JSON::to_json($response), } Friday, 25 July 14
  • sub get_data { my ($self) = @_; my $params = $self->db_params; my $page = delete $params->{page} // 1; my $rows = delete $params->{page_size} // 10; my $resultset = $self->db_schema->resultset($self->db_table); for my $param (keys %$params){ return 400 unless $resultset->result_source->has_column($param); } my $results = $resultset->search( $params, {page => $page, rows => $rows} ); return $self->serialise_results( $results, $params, $results->pager, $rows ); } Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Hypermedia driven Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Hypermedia driven Friday, 25 July 14
  • use Moo::Role; use URI; sub serialise_results { my ($self, $results, $params, $pager, $rows) = @_; my @results; while (my $result = $results->next){ push @results, $self->serialise_row($result); } return unless @results; my $href = URI->new( $self->request->base.$results->result_source->source_name."/" ); $href->query_form(%$params) if keys %$params; $href->query_form( page => $pager->current_page, page_size => $rows, $href->query_form ); return { total_results => $pager->total_entries, page => $pager->current_page, page_size => $pager->entries_on_this_page, href => $href->as_string, data => @results, } } Friday, 25 July 14
  • sub serialise_row { my ($self, $result) = @_; my $href = URI->new( $self->request->base.$result->result_source->source_name."/" ); $href->query_form( map { $_ => $result->$_ } $result->primary_columns ); return { type => $result->result_source->source_name, href => $href->as_string, data => {$result->get_columns}, }; } Friday, 25 July 14
  • Uniform Interface Identication Manipulation Informative Hypermedia driven Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • REST Client-Server Stateless Cacheable Layered Uniform Interface Friday, 25 July 14
  • Problems No routing in Web::Machine What is a Resource? An Row or a Resultset POST to non existent resource Business logic Friday, 25 July 14
  • TODO Routing in Web::Machine Resource on the y creation Allow business override/callbacks Methods for Hypermedia control between related resources. Friday, 25 July 14
  • Friday, 25 July 14
  • Questions? Friday, 25 July 14