Skip Menu |

This queue is for tickets about the MetaCPAN-API CPAN distribution.

Report information
The Basics
Id: 69814
Status: resolved
Priority: 0/
Queue: MetaCPAN-API

People
Owner: Nobody in particular
Requestors: TIMB [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: (no value)



Subject: Doesn't support POST queries
For example, I'd like to be able to do this kind of query via the API: http://explorer.metacpan.org/? url=%2Frelease&content=%7B%22query%22%3A%7B%22match_all%22%3A%7B%7D%7D%2C%22filte r%22%3A%7B%22prefix%22%3A%7B%22archive%22%3A%22Cache-Cache-1.06%22%7D%7D%7D
On Tue Jul 26 08:19:21 2011, TIMB wrote: Show quoted text
> For example, I'd like to be able to do this kind of query via the API: > > http://explorer.metacpan.org/? >
url=%2Frelease&content=%7B%22query%22%3A%7B%22match_all%22%3A%7B%7D%7D%2C%22filte Show quoted text
> r%22%3A%7B%22prefix%22%3A%7B%22archive%22%3A%22Cache-Cache- > 1.06%22%7D%7D%7D
Hi, Tim! :) That's a very good idea and I intend to develop the module even more. This is certainly a good point to use as reference. I've opened this ticket on Github Issues (where I keep MetaCPAN::API project) and I'll keep this ticket open as well until the issue is resolved. MetaCPANExplorer certainly contains a few good examples of ways to extend MetaCPAN::API and I've starred it the other day on my RSS reader to remember it.
I have just release 0.20 which supports complex GET requests (such as those demonstrated in the MetaCPAN docs and MetaCPANExplorer. The next goal is this ticket, which will help close all outstanding API method implementations as far as I know. I'm still unsure on how to do this, but I'm thinking of splitting it to two parts: - $mcpan->post_json(...) which will allow to write any JSON string you want to search with. - MetaCPAN::API::Predefined which will include custom searches as those that appear in MetaCPAN and MetaCPANExplorer. Stuff like $mcpan->find_latest_by_author( author => ..., ... ) and the likes. I'd be happy to get your thoughts on this, as you're the only MetaCPAN::API user I know. :)
The first step would be to add a fetch()-like method to handle POST Here's my monkey-patched quick hack that "works for me": { package MetaCPAN::API; # XXX MONKEY_PATCH use JSON; sub post_query { my $self = shift; my $url = shift; my $query = shift; my $base = $self->base_url; my $query_json = encode_json($query); my $result = $self->ua->request('POST', "$base/$url", { headers => { "Content-Type" => "application/json" }, content => $query_json }); my $decoded_result; $result->{'success'} or croak "Failed to fetch '$url': " . $result->{'reason'}." (Post content: $query_json)"; 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; } } The second half of that is the same as fetch() so ought to be factored out. I call it like $results = $mcpan->post_query("/file", { ... }); A MetaCPAN::API::Predefined seems like a good idea, but is separate from this issue (though dependent on it).
I have used your code (albeit refactored it a bit) and just released MetaCPAN::API 0.30, which allows you to do complex POST queries. The example query you gave was: http://explorer.metacpan.org/?url=/release&content={"query":{"match_all":{}},"filter":{"prefix":{"archive":"Cache-Cache-1.06"}}} You can now do this as such: my $result = $mcpan->fetch( 'release', { query => { match_all => {} }, filter => { prefix => { archive => 'Cache-Cache-1.06' } }, }, ); This example is also available in the POD. Thanks! :)