forked from xsawyerx/metacpan-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.pm
298 lines (220 loc) · 8.29 KB
/
API.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use strict;
use warnings;
package MetaCPAN::API;
# ABSTRACT: A comprehensive, DWIM-featured API to MetaCPAN
use Any::Moose;
use Carp;
use JSON;
use Try::Tiny;
use HTTP::Tiny;
use URI::Escape 'uri_escape';
with qw/
MetaCPAN::API::Author
MetaCPAN::API::Distribution
MetaCPAN::API::Favorite
MetaCPAN::API::File
MetaCPAN::API::Autocomplete
MetaCPAN::API::Module
MetaCPAN::API::POD
MetaCPAN::API::Rating
MetaCPAN::API::Release
MetaCPAN::API::Source
/;
has base_url => (
is => 'ro',
isa => 'Str',
default => 'http://api.metacpan.org/v0',
);
has ua => (
is => 'ro',
isa => 'HTTP::Tiny',
lazy_build => 1,
);
has ua_args => (
is => 'ro',
isa => 'ArrayRef',
default => sub {
my $version = $MetaCPAN::API::VERSION || 'xx';
return [ agent => "MetaCPAN::API/$version" ];
},
);
sub _build_ua {
my $self = shift;
return HTTP::Tiny->new( @{ $self->ua_args } );
}
sub fetch {
my $self = shift;
my $url = shift;
my $extra = $self->_build_extra_params(@_);
my $base = $self->base_url;
my $req_url = $extra ? "$base/$url?$extra" : "$base/$url";
my $result = $self->ua->get($req_url);
return $self->_decode_result( $result, $req_url );
}
sub post {
my $self = shift;
my $url = shift;
my $query = shift;
my $base = $self->base_url;
defined $url
or croak 'First argument of URL must be provided';
ref $query and ref $query eq 'HASH'
or croak 'Second argument of query hashref must be provided';
my $query_json = to_json( $query, { canonical => 1 } );
my $result = $self->ua->request(
'POST',
"$base/$url",
{
headers => { 'Content-Type' => 'application/json' },
content => $query_json,
}
);
return $self->_decode_result( $result, $url, $query_json );
}
sub _decode_result {
my $self = shift;
my ( $result, $url, $original ) = @_;
my $decoded_result;
ref $result and ref $result eq 'HASH'
or croak 'First argument must be hashref';
defined $url
or croak 'Second argument of a URL must be provided';
if ( defined ( my $success = $result->{'success'} ) ) {
my $reason = $result->{'reason'} || '';
$reason .= ( defined $original ? " (request: $original)" : '' );
$success or croak "Failed to fetch '$url': $reason";
} else {
croak 'Missing success in return value';
}
defined ( my $content = $result->{'content'} )
or croak 'Missing content in return value';
try { $decoded_result = decode_json $content }
catch { croak "Couldn't decode '$content': $_" };
return $decoded_result;
}
sub _build_extra_params {
my $self = shift;
@_ % 2 == 0
or croak 'Incorrect number of params, must be key/value';
my %extra = @_;
# if it's deep, JSON encoding needs to be involved
if (scalar grep { ref } values %extra) {
# if this is a deep search query, forgetting
# query -> match_all in a common mistake...
$extra{query} = { match_all => {} } unless ($extra{query});
my $query_json = to_json( \%extra, { canonical => 1 } );
%extra = ( source => $query_json );
}
my $extra = join '&', map {
"$_=" . uri_escape( $extra{$_} )
} sort keys %extra;
return $extra;
}
1;
__END__
=head1 SYNOPSIS
# simple usage
my $mcpan = MetaCPAN::API->new();
my $author = $mcpan->author('XSAWYERX');
my $dist = $mcpan->release( distribution => 'MetaCPAN-API' );
# advanced usage with cache (contributed by Kent Fredric)
require CHI;
require WWW::Mechanize::Cached;
require HTTP::Tiny::Mech;
require MetaCPAN::API;
my $mcpan = MetaCPAN::API->new(
ua => HTTP::Tiny::Mech->new(
mechua => WWW::Mechanize::Cached->new(
cache => CHI->new(
driver => 'File',
root_dir => '/tmp/metacpan-cache',
),
),
),
);
=head1 DESCRIPTION
This is a hopefully-complete API-compliant interface to MetaCPAN
(L<https://metacpan.org>) with DWIM capabilities, to make your life easier.
This module has three purposes:
=over 4
=item * Provide 100% of the beta MetaCPAN API
This module will be updated regularly on every MetaCPAN API change, and intends
to provide the user with as much of the API as possible, no shortcuts. If it's
documented in the API, you should be able to do it.
Because of this design decision, this module has an official MetaCPAN namespace
with the blessing of the MetaCPAN developers.
Notice this module currently only provides the beta API, not the old
soon-to-be-deprecated API.
=item * Be lightweight, to allow flexible usage
While many modules would help make writing easier, it's important to take into
account how they affect your compile-time, run-time and overall memory
consumption.
By providing a slim interface implementation, more users are able to use this
module, such as long-running processes (like daemons), CLI or GUI applications,
cron jobs, and more.
=item * DWIM
While it's possible to access the methods defined by the API spec, there's still
a matter of what you're really trying to achieve. For example, when searching
for I<"Dave">, you want to find both I<Dave Cross> and I<Dave Rolsky> (and any
other I<Dave>), but you also want to search for a PAUSE ID of I<DAVE>, if one
exists.
This is where DWIM comes in. This module provides you with additional generic
methods which will try to do what they think you want.
Of course, this does not prevent you from manually using the API methods. You
still have full control over that, if that's what you wish.
You can (and should) read up on the generic methods, which will explain how
their DWIMish nature works, and what searches they run.
=back
=head1 ATTRIBUTES
=head2 base_url
my $mcpan = MetaCPAN::API->new(
base_url => 'http://localhost:9999',
);
This attribute is used for REST requests. You should set it to where the
MetaCPAN is accessible. By default it's already set correctly, but if you're
running a local instance of MetaCPAN, or use a local mirror, or tunnel it
through a local port, or any of those stuff, you would want to change this.
Default: I<http://api.metacpan.org/v0>.
This attribute is read-only (immutable), meaning that once it's set on
initialize (via C<new()>), you cannot change it. If you need to, create a
new instance of MetaCPAN::API. Why is it immutable? Because it's better.
=head2 ua
This attribute is used to contain the user agent used for running the REST
request to the server. It is specifically set to L<HTTP::Tiny>, so if you
want to set it manually, make sure it's of HTTP::Tiny.
HTTP::Tiny is used as part of the philosophy of keeping it tiny.
This attribute is read-only (immutable), meaning that once it's set on
initialize (via C<new()>), you cannot change it. If you need to, create a
new instance of MetaCPAN::API. Why is it immutable? Because it's better.
=head2 ua_args
my $mcpan = MetaCPAN::API->new(
ua_args => [ agent => 'MyAgent' ],
);
The arguments that will be given to the L<HTTP::Tiny> user agent.
This attribute is read-only (immutable), meaning that once it's set on
initialize (via C<new()>), you cannot change it. If you need to, create a
new instance of MetaCPAN::API. Why is it immutable? Because it's better.
The default is a user agent string: B<MetaCPAN::API/$version>.
=head1 METHODS
=head2 fetch
my $result = $mcpan->fetch('/release/distribution/Moose');
# with parameters
my $more = $mcpan->fetch(
'/release/distribution/Moose',
param => 'value',
);
This is a helper method for API implementations. It fetches a path from
MetaCPAN, decodes the JSON from the content variable and returns it.
You don't really need to use it, but you can in case you want to write your
own extension implementation to MetaCPAN::API.
It accepts an additional hash as C<GET> parameters.
=head2 post
# /release&content={"query":{"match_all":{}},"filter":{"prefix":{"archive":"Cache-Cache-1.06"}}}
my $result = $mcpan->post(
'release',
{
query => { match_all => {} },
filter => { prefix => { archive => 'Cache-Cache-1.06' } },
},
);
The POST equivalent of the C<fetch()> method. It gets the path and JSON request.