aboutsummaryrefslogtreecommitdiffstats
path: root/perllib
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2019-04-05 15:42:17 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2019-04-05 15:42:17 +0100
commit3092ba7a0030846c115c47115c8b7161216ec2ad (patch)
tree50dc96b44ad54b7502b6abbf35cd1ec63c6f36af /perllib
parent9195de2a66ce7ff898a76040a1704a8185b5d5d5 (diff)
parent2ab5e5e15bd84168028341b4e7ec01422a6b00d8 (diff)
Merge branch 'open311-better-get-error'
Diffstat (limited to 'perllib')
-rw-r--r--perllib/Open311.pm103
1 files changed, 41 insertions, 62 deletions
diff --git a/perllib/Open311.pm b/perllib/Open311.pm
index 749e7ccad..ec3390ee7 100644
--- a/perllib/Open311.pm
+++ b/perllib/Open311.pm
@@ -8,7 +8,7 @@ use XML::Simple;
use LWP::Simple;
use LWP::UserAgent;
use DateTime::Format::W3CDTF;
-use HTTP::Request::Common qw(POST);
+use HTTP::Request::Common qw(GET POST);
use FixMyStreet::Cobrand;
use FixMyStreet::DB;
use Utils;
@@ -437,80 +437,49 @@ sub _params_to_string {
return $string;
}
-sub _get {
- my $self = shift;
- my $path = shift;
+sub _request {
+ my $self = shift;
+ my $method = shift;
+ my $path = shift;
my $params = shift || {};
my $uri = URI->new( $self->endpoint );
+ $uri->path( $uri->path . $path );
- $params->{ jurisdiction_id } = $self->jurisdiction
+ $params->{jurisdiction_id} = $self->jurisdiction
if $self->jurisdiction;
- $uri->path( $uri->path . $path );
- my $base_uri = $uri->clone;
- $uri->query_form( $params );
+ $params->{api_key} = ($self->api_key || '')
+ if $method eq 'POST' && $self->api_key;
+
+ my $debug_request = $method . ' ' . $uri->as_string . "\n\n";
+
+ my $req = do {
+ if ($method eq 'GET') {
+ $uri->query_form( $params );
+ GET $uri->as_string;
+ } elsif ($method eq 'POST') {
+ POST $uri->as_string, $params;
+ }
+ };
- my $debug_request = "GET " . $base_uri->as_string . "\n\n";
$debug_request .= $self->_params_to_string($params, $debug_request);
$self->debug_details( $self->debug_details . $debug_request );
- my $content;
- if ( $self->test_mode ) {
+ if ( $self->test_mode && $req->method eq 'GET') {
$self->success(1);
- $content = $self->test_get_returns->{ $path };
$self->test_uri_used( $uri->as_string );
- } else {
- my $ua = LWP::UserAgent->new;
-
- my $req = HTTP::Request->new(
- GET => $uri->as_string
- );
-
- my $res = $ua->request( $req );
+ return $self->test_get_returns->{ $path };
+ }
- if ( $res->is_success ) {
- $content = $res->decoded_content;
- $self->success(1);
+ my $res = do {
+ if ( $self->test_mode ) {
+ $self->test_req_used( $req );
+ $self->test_get_returns->{ $path };
} else {
- $self->success(0);
- $self->error( sprintf(
- "request failed: %s\n%s\n",
- $res->status_line,
- $uri->as_string
- ) );
+ my $ua = LWP::UserAgent->new;
+ $ua->request( $req );
}
- }
-
- return $content;
-}
-
-sub _post {
- my $self = shift;
- my $path = shift;
- my $params = shift;
-
- my $uri = URI->new( $self->endpoint );
- $uri->path( $uri->path . $path );
-
- $params->{jurisdiction_id} = $self->jurisdiction
- if $self->jurisdiction;
- $params->{api_key} = ($self->api_key || '')
- if $self->api_key;
- my $req = POST $uri->as_string, $params;
-
- my $debug_request = $req->method . ' ' . $uri->as_string . "\n\n";
- $debug_request .= $self->_params_to_string($params, $debug_request);
- $self->debug_details( $self->debug_details . $debug_request );
-
- my $ua = LWP::UserAgent->new();
- my $res;
-
- if ( $self->test_mode ) {
- $res = $self->test_get_returns->{ $path };
- $self->test_req_used( $req );
- } else {
- $res = $ua->request( $req );
- }
+ };
if ( $res->is_success ) {
$self->success(1);
@@ -523,10 +492,20 @@ sub _post {
$self->_process_error( $res->decoded_content ),
$self->debug_details
) );
- return 0;
+ return;
}
}
+sub _get {
+ my $self = shift;
+ return $self->_request(GET => @_);
+}
+
+sub _post {
+ my $self = shift;
+ return $self->_request(POST => @_);
+}
+
sub _process_error {
my $self = shift;
my $error = shift;