| 1 | package WebService::MyGengo::RequestFactory; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | BEGIN { extends 'WebService::MyGengo::Base' }; | ||
| 7 | |||
| 8 | use WebService::MyGengo::Exception; | ||
| 9 | |||
| 10 | use URI; | ||
| 11 | use Digest::HMAC; | ||
| 12 | use Digest::SHA1; | ||
| 13 | use JSON qw(encode_json); | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | WebService::MyGengo::RequestFactory - A factory for creating myGengo API requests | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Returns various L<WebService::MyGengo::Request> objects to be sent to the API. | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | # Note: Requests are usually created automatically by WebService::MyGengo::Client | ||
| 26 | my $req_factory = new WebService::MyGengo::RequestFactory({ | ||
| 27 | public_key => $pubkey | ||
| 28 | , private_key => $privkey | ||
| 29 | , root_uri => $api_uri | ||
| 30 | }); | ||
| 31 | |||
| 32 | my $req = $req_factory->new_request( $method, $endpoint, \%params ); | ||
| 33 | |||
| 34 | # Alternate constructor syntax | ||
| 35 | my $req_factory = new WebService::MyGengo::RequestFactory( | ||
| 36 | $pubkey | ||
| 37 | , $privkey | ||
| 38 | , $api_uri | ||
| 39 | ); | ||
| 40 | |||
| 41 | =head1 ATTRIBUTES | ||
| 42 | |||
| 43 | All attributes are read-only. If, for some reason, you need to generate | ||
| 44 | requests for a different keypair or root_uri, just make a new RequestFactory. | ||
| 45 | |||
| 46 | =head2 public_key (Str) | ||
| 47 | |||
| 48 | Your public API key. | ||
| 49 | |||
| 50 | =head2 private_key (Str) | ||
| 51 | |||
| 52 | Your private API key | ||
| 53 | |||
| 54 | =cut | ||
| 55 | has [qw/public_key private_key/] => ( | ||
| 56 | is => 'ro' | ||
| 57 | , isa => 'Str' | ||
| 58 | , required => 1 | ||
| 59 | ); | ||
| 60 | |||
| 61 | =head2 root_uri (URI) | ||
| 62 | |||
| 63 | The URI to be used as the base for all API endpoints. | ||
| 64 | |||
| 65 | eg, 'http://api.sandbox.mygengo.com/v1.1' | ||
| 66 | |||
| 67 | =cut | ||
| 68 | has root_uri => ( | ||
| 69 | is => 'ro' | ||
| 70 | , isa => 'WebService::MyGengo::URI' | ||
| 71 | , required => 1 | ||
| 72 | ); | ||
| 73 | |||
| 74 | =head1 METHODS | ||
| 75 | =cut | ||
| 76 | |||
| 77 | #=head2 BUILDARGS | ||
| 78 | # | ||
| 79 | #Allow arguments as a list or hashref. | ||
| 80 | # | ||
| 81 | #=cut | ||
| 82 | around BUILDARGS => sub { | ||
| 83 | my ( $orig, $class, $args ) = ( shift, shift, @_ ); | ||
| 84 | |||
| 85 | ref($args) eq 'HASH' and return $class->$orig(@_); | ||
| 86 | |||
| 87 | return { | ||
| 88 | public_key => shift | ||
| 89 | , private_key => shift | ||
| 90 | , root_uri => shift->clone | ||
| 91 | }; | ||
| 92 | }; | ||
| 93 | |||
| 94 | =head2 new_request( $request_method, $endpoint, \%params ) | ||
| 95 | |||
| 96 | Returns an L<HTTP::Request> object for the given API endpoint. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | sub new_request { | ||
| 100 | my ( $self, $method, $endpoint, $params ) = ( shift, @_ ); | ||
| 101 | |||
| 102 | !( $method && $endpoint ) and WebService::MyGengo::Exception->throw({ | ||
| 103 | message => "Both an HTTP request method and API endpoint are required" | ||
| 104 | . " to create a request." | ||
| 105 | }); | ||
| 106 | |||
| 107 | my $builder_method = "_build_".$method; | ||
| 108 | my $builder = $self->can( $builder_method ) | ||
| 109 | or WebService::MyGengo::Exception->throw({ message => | ||
| 110 | "Cannot find builder for '" | ||
| 111 | . $method . "' request." | ||
| 112 | }); | ||
| 113 | |||
| 114 | my $req = $self->$builder( $method, $endpoint, $params ); | ||
| 115 | |||
| 116 | return $req; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _build_POST { | ||
| 120 | return shift->_build_request_with_form_params( @_ ); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _build_PUT { | ||
| 124 | return shift->_build_request_with_form_params( @_ ); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _build_GET { | ||
| 128 | return shift->_build_request_with_query_params( @_ ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _build_DELETE { | ||
| 132 | return shift->_build_request_with_query_params( @_ ); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _build_request_with_form_params { | ||
| 136 | my ( $self, $method, $endpoint, $params ) = ( shift, @_ ); | ||
| 137 | |||
| 138 | my ( $uri, $req_params ) = $self->_get_uri_and_req_params( @_ ); | ||
| 139 | |||
| 140 | $req_params->{data} = encode_json($params); | ||
| 141 | |||
| 142 | my $request = HTTP::Request->new( $method => $uri ); | ||
| 143 | $request->push_header( 'Accept' => 'application/json; charset=utf-8' ); | ||
| 144 | $request->push_header('Content-Type' =>'application/x-www-form-urlencoded'); | ||
| 145 | |||
| 146 | # See L<HTTP::Request::Common>'s POST method. We're just using URI to | ||
| 147 | # format the request body for us in this case. | ||
| 148 | $uri->query_form( $req_params ); | ||
| 149 | $request->content( $uri->query ); | ||
| 150 | |||
| 151 | return $request; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _build_request_with_query_params { | ||
| 155 | my ( $self, $method, $endpoint, $params ) = ( shift, @_ ); | ||
| 156 | |||
| 157 | my ( $uri, $req_params ) = $self->_get_uri_and_req_params( @_ ); | ||
| 158 | |||
| 159 | # Non-POST/PUT requests need query parameters | ||
| 160 | ref($params) eq 'HASH' and @$req_params{ keys %$params } = values %$params; | ||
| 161 | $uri->query_form( $req_params ); | ||
| 162 | |||
| 163 | my $request = HTTP::Request->new( $method => $uri ); | ||
| 164 | $request->push_header( 'Accept' => 'application/json; charset=utf-8' ); | ||
| 165 | |||
| 166 | return $request; | ||
| 167 | } | ||
| 168 | |||
| 169 | #=head2 _get_uri_and_req_params( $method, $endpoint ) | ||
| 170 | # | ||
| 171 | #Returns an array of a URI object and a hashref of request parameters | ||
| 172 | #common to all API requests. | ||
| 173 | # | ||
| 174 | #=cut | ||
| 175 | sub _get_uri_and_req_params { | ||
| 176 | my ( $self, $method, $endpoint ) = ( shift, @_ ); | ||
| 177 | |||
| 178 | my $pubkey = $self->public_key; | ||
| 179 | my $privkey = $self->private_key; | ||
| 180 | my $uri = $self->root_uri->clone; | ||
| 181 | |||
| 182 | $uri->path( $uri->path . $endpoint ); | ||
| 183 | |||
| 184 | my $time = time(); | ||
| 185 | my $hmac = Digest::HMAC->new($privkey, "Digest::SHA1"); | ||
| 186 | $hmac->add($time); | ||
| 187 | |||
| 188 | my $req_params = { | ||
| 189 | 'api_sig' => $hmac->hexdigest | ||
| 190 | , 'api_key' => $pubkey | ||
| 191 | , 'ts' => $time | ||
| 192 | }; | ||
| 193 | |||
| 194 | return ( $uri, $req_params ); | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | __PACKAGE__->meta->make_immutable(); | ||
| 199 | 1; | ||
| 200 | |||
| 201 | =head1 AUTHOR | ||
| 202 | |||
| 203 | Nathaniel Heinrichs | ||
| 204 | |||
| 205 | =head1 LICENSE | ||
| 206 | |||
| 207 | Copyright (c) 2011, Nathaniel Heinrichs <nheinric-at-cpan.org>. | ||
| 208 | All rights reserved. | ||
| 209 | |||
| 210 | This library is free software. You can redistribute it and/or modify | ||
| 211 | it under the same terms as Perl itself. | ||
| 212 | |||
| 213 | =cut |