Getsy.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« Getsy/Types.pm ↑ Summary
1 0 ▶use MooseX::Declare;
Subroutine BEGIN: called CC=1 SCAR=2.6
2 use 5.008008;
3  
4 our $VERSION = '0.999007';
5  
6 =head1 NAME
7
8 WWW::Getsy - Command line interface to Etsy V2 OAuth API
9  
10 =head1 SYNOPSIS
11  
12 Command line interface to Etsy V2 OAuth API
13  
14 Register for a developer account here: http://developer.etsy.com/member/register
15 Then register your app for a V2 Sandbox key: http://developer.etsy.com/apps/register
16  
17 Then define your consumer key and secret environment variables
18  
19 export OAUTH_CONSUMER_KEY='your_key'
20 export OAUTH_CONSUMER_SECRET='your_secret'
21  
22 Finally start making some api calls
23  
24 Get a list of all methods
25  
26 getsy --sandbox --path '/'
27  
28 Get your user info
29  
30 getsy --sandbox --path '/users/__SELF__'
31  
32 You can use the --method paramter to make post, put, and delete calls (get is default)
33 and --params to pass in a JSON string of parameters
34  
35 getsy --sandbox --path '/listings/51455722' --params '{"title" : "changing the title"}' --method put
36  
37 A full list of methods available here
38  
39 http://developer.etsy.com/docs/methods
40  
41 =cut
42  
43 class WWW::Getsy with MooseX::Getopt {
44 use WWW::Getsy::OAuth;
45 use File::HomeDir qw(home);
46 use File::Spec::Functions qw(catfile);
47 use JSON::XS;
48 use MooseX::Getopt;
49 use MooseX::Types::Moose qw/Str HashRef/;
50 use WWW::Getsy::Types qw(
51 EnvConsumerKey
52 EnvConsumerSecret
53 RequestParams
54 RequestMethod
55 );
56 use Data::Dumper;
57  
58 has 'sandbox' => (
59 is => 'rw',
60 isa => 'Bool',
61 default => 0,
62 );
63  
64 has 'debug' => (
65 is => 'rw',
66 isa => 'Bool',
67 default => 0,
68 );
69  
70 has 'path' => (
71 is => 'rw',
72 isa => Str,
73 required => 1
74 );
75  
76 has 'url' => (
77 metaclass => 'NoGetopt',
78 is => 'rw',
79 isa => Str,
80 required => 1,
81 lazy => 1,
82 default => method {
83 return "http://". $self->api_domain ."/v2/private".$self->path;
84 }
85 );
86  
87 has 'api_domain' => (
88 metaclass => 'NoGetopt',
89 is => 'ro',
90 isa => Str,
91 required => 1,
92 lazy => 1,
93 default => method {
94 my $api_domain = 'openapi.etsy.com';
95 if ($self->sandbox) {
96 $api_domain = 'sandbox.openapi.etsy.com';
97 }
98 return $api_domain;
99 }
100 );
101  
102 has 'params' => (
103 is => 'rw',
104 isa => RequestParams,
105 coerce => 1,
106 required => 0,
107 default => sub { {} }
108 );
109  
110 has 'method' => (
111 is => 'rw',
112 isa => RequestMethod,
113 coerce => 1,
114 required => 1,
115 default => "get",
116 );
117  
118 has 'conf_file' => (
119 metaclass => 'NoGetopt',
120 isa => Str,
121 is => 'ro',
122 lazy => 1,
123 default => method {
124 my $configfile = ".getsy";
125 if ($self->sandbox) {
126 $configfile .= "_sandbox";
127 }
128 catfile( home(), $configfile )
129 },
130 );
131  
132 has 'consumer_key' => (
133 metaclass => 'NoGetopt',
134 isa => EnvConsumerKey,
135 is => 'ro',
136 default => sub {
137 $ENV{OAUTH_CONSUMER_KEY}
138 },
139 );
140  
141 has 'consumer_secret' => (
142 metaclass => 'NoGetopt',
143 isa => EnvConsumerSecret,
144 is => 'ro',
145 default => sub {
146 $ENV{OAUTH_CONSUMER_SECRET}
147 }
148 );
149  
150 has 'oauth_client' => (
151 metaclass => 'NoGetopt',
152 isa => 'WWW::Getsy::OAuth',
153 is => 'ro',
154 lazy_build => 1,
155 handles => [qw/
156 authorized
157 /],
158 );
159  
160 method _build_oauth_client {
161 WWW::Getsy::OAuth->new(
162 tokens => {
163 consumer_key => $self->consumer_key,
164 consumer_secret => $self->consumer_secret,
165 },
166 protocol_version => '1.0a',
167 urls => {
168 authorization_url => "http://www.etsy.com/oauth/signin",
169 request_token_url => "http://". $self->api_domain ."/v2/oauth/request_token",
170 access_token_url => "http://". $self->api_domain ."/v2/oauth/access_token",
171 },
172 callback => 'oob',
173 );
174 }
175  
176 before authorized {
177 my %tokens = $self->oauth_client->load_tokens($self->conf_file);
178  
179 if (grep {defined && length} %tokens) {
180 $self->oauth_client->{'tokens'}->{'access_token'} = $tokens{'access_token'};
181 $self->oauth_client->{'tokens'}->{'access_token_secret'} = $tokens{'access_secret'};
182 } else {
183 $self->get_access_tokens;
184 }
185 };
186  
187 method get_access_tokens {
188 print "Go to ".$self->oauth_client->get_authorization_url(callback => 'oob') ."\n";
189 print "Then enter the verifier token\n";
190 my $verifier = <STDIN>;
191 chomp $verifier;
192  
193 my ($access_token, $access_secret) = $self->oauth_client->request_access_token(verifier => $verifier);
194  
195 # Now save those values
196 $self->oauth_client->save_tokens($self->conf_file, access_token=>$access_token, access_secret=>$access_secret);
197  
198 }
199  
200 method oauth_request() {
201 if ($self->debug) {
202 print "request: ". uc($self->method) ." ". $self->url."\n";
203 print "params: ". (%{$self->params} ? Dumper($self->params) : "none") ."\n";
204 }
205 my $method = 'oauth_'. $self->method;
206 $self->$method();
207 }
208  
209 method oauth_get() {
210 return $self->oauth_client->make_restricted_request($self->url, 'GET', %{$self->params});
211 }
212  
213 method oauth_post() {
214 return $self->oauth_client->make_restricted_request($self->url, 'POST', %{$self->params});
215 }
216  
217 method oauth_put() {
218 return $self->oauth_client->make_restricted_request($self->url, 'PUT', %{$self->params});
219 }
220  
221 method oauth_delete() {
222 return $self->oauth_client->make_restricted_request($self->url, 'DELETE', %{$self->params});
223 }
224  
225 method decode(Str $json) {
226 decode_json $json;
227 }
228  
229 method encode(HashRef $json) {
230 JSON::XS->new->utf8->pretty->encode($json);
231 }
232  
233 method pretty_print(Str $json) {
234 print $self->encode($self->decode($json));
235 }
236  
237 }
238  
239  
240  
241 =head1 AUTHOR
242  
243 John Goulah, C<< <jgoulah at cpan.org> >>
244  
245 =head1 BUGS
246  
247 Please report any bugs or feature requests to C<bug-www-getsy at rt.cpan.org>, or through
248 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Getsy>. I will be notified, and then you'll
249 automatically be notified of progress on your bug as I make changes.
250  
251  
252  
253  
254 =head1 SUPPORT
255  
256 You can find documentation for this module with the perldoc command.
257  
258 perldoc WWW::Getsy
259  
260  
261 You can also look for information at:
262  
263 =over 4
264  
265 =item * RT: CPAN's request tracker
266  
267 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Getsy>
268  
269 =item * AnnoCPAN: Annotated CPAN documentation
270  
271 L<http://annocpan.org/dist/WWW-Getsy>
272  
273 =item * CPAN Ratings
274  
275 L<http://cpanratings.perl.org/d/WWW-Getsy>
276  
277 =item * Search CPAN
278  
279 L<http://search.cpan.org/dist/WWW-Getsy/>
280  
281 =back
282  
283  
284 =head1 ACKNOWLEDGEMENTS
285  
286  
287 =head1 LICENSE AND COPYRIGHT
288  
289 Copyright 2012 John Goulah.
290  
291 This program is free software; you can redistribute it and/or modify it
292 under the terms of either: the GNU General Public License as published
293 by the Free Software Foundation; or the Artistic License.
294  
295 See http://dev.perl.org/licenses/ for more information.
296  
297  
298 =cut
« Getsy/Types.pm ↑ Summary