Slide 1

Slide 1 text

Using  Roles  to     Build  Web  Service  Clients   Mark  Allen   [email protected]   @bytemeorg  

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

h@ps://flic.kr/p/4ZJ9ms  

Slide 7

Slide 7 text

h@ps://flic.kr/p/9g1aZc  

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

h@ps://flic.kr/p/8J3cKz  

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

You're Welcome!

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

h@ps://flic.kr/p/38vsE3  

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

h@ps://flic.kr/p/6uRpjb  

Slide 18

Slide 18 text

h@ps://flic.kr/p/3EBJg9  

Slide 19

Slide 19 text

h@ps://flic.kr/p/2zWmyv  

Slide 20

Slide 20 text

use strict; package WebService::Foobar::Request; use Moo::Role; use warnings NONFATAL => 'all'; use HTTP::Tiny; use Carp qw(confess); has 'ua' => ( is => 'ro', lazy => 1, default => sub { HTTP::Tiny->new( agent => 'WebService-Foobar ', default_headers => { 'Content-Type' => 'application/json' }, ) }, ); has 'base_url' => ( is => 'ro', lazy => 1, default => sub { 'http://api.foobar.io/v1/' }, );

Slide 21

Slide 21 text

# package WebService::Foobar::Request; # ... sub _request { my ($self, $op, $data) = @_; # ideally $data is already serialized my $url = $self->_build_url($op) or confess "Couldn't build url for $op\n"; my $result = $self->ua->request('POST', $url, {content=>$data}); if ( $result->{success} ) return $result; } else { confess "Request to $url failed. Got (" . $result->{status} . ") " . $result->{content} . "\n"; } }

Slide 22

Slide 22 text

use strict; package WebService::Foobar::JSON; use Moo::Role; use warnings NONFATAL => 'all'; use JSON; use Carp qw(confess); has 'serializer' => ( is => 'ro', lazy => 1, default => sub { JSON->new() }, ); sub encode { my ($self, $ref) = @_; $self->serializer->encode($ref); } sub decode { my ($self, $data) = @_; $self->serializer->decode($data); }

Slide 23

Slide 23 text

use strict; package WebService::Foobar::Data; use Moo; use warnings NONFATAL => 'all'; use Storable qw(dcopy); with 'WebService::Foobar::JSON'; has 'bar' => ( 'is' => 'rw' ); has 'baz' => ( 'qux' => 'ro' ); sub qux { my $self = shift; print $self->bar . " and " . $self->baz; } sub _serialize { my $self = shift; my $copy = dcopy($self); $self->encode($copy); }

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Public  domain  

Slide 26

Slide 26 text

h@ps://flic.kr/p/7ABQ56  

Slide 27

Slide 27 text

h@p://commons.wikimedia.org/wiki/File%3ATed_Gundersons_FBI_BADGE_inside.jpg  

Slide 28

Slide 28 text

h@p://commons.wikimedia.org/wiki/File:Synopsis_hist%C3%B3rica_chronologica_de_Espa%C3%B1a_-­‐_1775-­‐1791.jpg  

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

h@ps://flic.kr/p/58vQCQ  

Slide 31

Slide 31 text

THANK  YOU!   Slides:  h@ps://speakerdeck.com/mrallen1/   Email:  mrallen1  at  yahoo  dot  com   Twi-er:  @bytemeorg   Github:  h@ps://github.com/mrallen1