From 783674e223a221d4960bf19edd4da57fbe4bfa09 Mon Sep 17 00:00:00 2001 From: Petter Reinholdtsen Date: Sat, 23 Jul 2011 08:46:25 +0200 Subject: Start on Catalyst based Open311 API based on old CGI based implementation. Comment out non-working code for now. --- perllib/FixMyStreet/App/Controller/Open311.pm | 632 ++++++++++++++++++++++++++ 1 file changed, 632 insertions(+) create mode 100644 perllib/FixMyStreet/App/Controller/Open311.pm (limited to 'perllib/FixMyStreet/App/Controller') diff --git a/perllib/FixMyStreet/App/Controller/Open311.pm b/perllib/FixMyStreet/App/Controller/Open311.pm new file mode 100644 index 000000000..9d64138d9 --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Open311.pm @@ -0,0 +1,632 @@ +package FixMyStreet::App::Controller::Open311; + +use utf8; +use Moose; +use namespace::autoclean; + +use JSON; +use XML::Simple; +use URI::Escape; +use mySociety::DBHandle qw(select_all); +use mySociety::Web qw(ent); + +BEGIN { extends 'Catalyst::Controller'; } + +=head1 NAME + +FixMyStreet::App::Controller::Open311 - Catalyst Controller + +=head1 DESCRIPTION + +Open311 server API + +Open311 server API for Open311 clients + +http://open311.org/ +http://wiki.open311.org/GeoReport_v2 +http://fixmystreet.org.nz/api +http://seeclickfix.com/open311/ + +Issues with Open311 + * no way to specify which languages are understood by the + recipients. some lang=nb,nn setting should be available. + * not obvious how to handle generic requests (ie without lat/lon + values). + * should service IDs be numeric or not? Spec do not say, and all + examples I find use numbers. + * missing way to search for reports near a location using lat/lon + * report attributes lack title field. + * missing way to provide updates information for a request + * should support GeoRSS output as well as json and home made XML + +=head1 METHODS + +=cut + +=head2 index + +Displays some summary information for the requests. + +=cut + +sub index : Path : Args(0) { + my ( $self, $c ) = @_; + return show_documentation($c); +} + +=head2 discovery + +http://search.cpan.org/~bobtfish/Catalyst-Manual-5.8007/lib/Catalyst/Manual/Intro.pod#Action_types + +=cut + +sub discovery_v2 : Regex('^open311(.cgi)?/v2/discovery.(xml|json|html)$') : Args(0) { + my ( $self, $c ) = @_; + my $format = $c->req->captures->[1]; + return get_discovery($c, 'xml'); +} + +sub services_v2 : Regex('^open311(.cgi)?/v2/services.(xml|json|html)$') : Args(0) { + my ( $self, $c ) = @_; + my $format = $c->req->captures->[1]; + return get_services($c, $format); +} + +sub requests_v2 : Regex('^open311(.cgi)?/v2/requests.(xml|json|html|rss)$') : Args(0) { + my ( $self, $c ) = @_; + my $format = $c->req->captures->[1]; + return get_requests($c, $format); +} + +sub request_v2 : Regex('^open311(.cgi)?/v2/requests/(\d+).(xml|json|html)$') : Args(0) { + my ( $self, $c ) = @_; + my $id = $c->req->captures->[1]; + my $format = $c->req->captures->[2]; + return get_request($c, $id, $format); +} + +sub error : Private { + my ($q, $error) = @_; + show_documentation($q, "ERROR: $error"); +} + +sub show_documentation : Private { + my ($c, $message) = @_; + my $jurisdiction_id = 'fiksgatami.no'; + my $response; + + $c->res->content_type('text/html; charset=utf-8'); + + $response .= CGI::h1(_('Open311 API for the mySociety FixMyStreet server')); + $response .= CGI::p(sprintf(_('Note: %s'), $message)) + if $message; + $response .= CGI::p(_('At the moment only searching for and looking at reports work.')); + $response .= CGI::p(_('This API implementation is work in progress and not yet stabilized. It will change without warnings in the future.')); + + $response .= CGI::li(CGI::a({rel => 'nofollow', + href => "http://www.open311.org/"}, + _('Open311 initiative web page'))); + $response .= CGI::li(CGI::a({rel => 'nofollow', + href => 'http://wiki.open311.org/GeoReport_v2'}, + _('Open311 specification'))); + + $response .= CGI::p(sprintf(_('At most %d requests are returned in each query. The returned requests are ordered by requested_datetime, so to get all requests, do several searches with rolling start_date and end_date.'), + mySociety::Config::get('RSS_LIMIT'))); + + my $baseurl = $c->cobrand->base_url(); + + $response .= CGI::p(_('The following Open311 v2 attributes are returned for each request: service_request_id, description, lat, long, media_url, status, requested_datetime, updated_datetime, service_code and service_name.')); + + $response .= CGI::p(_('In addition, the following attributes that are not part of the Open311 v2 specification are returned: agency_sent_datetime, title (also returned as part of description), interface_used, comment_count, requestor_name (only present if requestor allowed the name to be shown on this site).')); + + $response .= CGI::p(_('The Open311 v2 attribute agency_responsible is used to list the administrations that received the problem report, which is not quite the way the attribute is defined in the Open311 v2 specification.')); + + my $mapiturl = mySociety::Config::get('MAPIT_URL'); + $response .= CGI::p(sprintf(_('With request searches, it is also possible to search for agency_responsible to limit the requests to those sent to a single administration. The search term is the administration ID provided by MaPit.'), $mapiturl)); + + $response .= CGI::p(_('Examples:')); + + $response .= "