diff --git a/Koha/ERM/EUsage/UsageDataProvider.pm b/Koha/ERM/EUsage/UsageDataProvider.pm index 00d3e8f7e6..1c7ae1443b 100644 --- a/Koha/ERM/EUsage/UsageDataProvider.pm +++ b/Koha/ERM/EUsage/UsageDataProvider.pm @@ -266,7 +266,7 @@ Tests the connection of the harvester to the SUSHI service and returns any alert sub test_connection { my ($self) = @_; - my $url = $self->service_url; + my $url = _validate_url($self->service_url, 'status'); $url .= 'status'; $url .= '?customer_id=' . $self->customer_id; $url .= '&requestor_id=' . $self->requestor_id if $self->requestor_id; @@ -383,17 +383,59 @@ sub _build_url_query { $self->erm_usage_data_provider_id; } - # FIXME: service_url needs to end in 'reports/' - # below concat will result in a badly formed URL otherwise - # Either validate this on UI form, here, or both - my $url = $self->service_url; + my $url = _validate_url($self->service_url, 'harvest'); - $url .= $self->{report_type}; + $url .= lc $self->{report_type}; $url .= '?customer_id=' . $self->customer_id; $url .= '&requestor_id=' . $self->requestor_id if $self->requestor_id; $url .= '&api_key=' . $self->api_key if $self->api_key; - $url .= '&begin_date=' . $self->{begin_date} if $self->{begin_date}; - $url .= '&end_date=' . $self->{end_date} if $self->{end_date}; + $url .= '&begin_date=' . substr $self->{begin_date}, 0, 7 if $self->{begin_date}; + $url .= '&end_date=' . substr $self->{end_date}, 0, 7 if $self->{end_date}; + + return $url; +} + + +=head3 _validate_url + +Checks whether the url ends in a trailing "/" and adds one if not + +my $url = _validate_url($url, 'harvest') + +$caller is either the harvest_sushi function ("harvest") or the test_connection function ("status") + +=cut + +sub _validate_url { + my ( $url, $caller ) = @_; + + if($caller eq 'harvest') { + # Not all urls will end in "/" - add one so they are standardised + $url = _check_trailing_character($url); + # All SUSHI report requests should be to the "/reports" endpoint + # Not all providers in the counter registry include this in their data so we need to check and add it + my $reports_param = substr $url, -8; + $url .= 'reports/' if $reports_param ne 'reports/'; + } else { + $url = _check_trailing_character($url); + } + + return $url; +} + +=head3 _check_trailing_character + +Checks whether a url string ends in a "/" before we concatenate further params to the end of the url + +=cut + +sub _check_trailing_character { + my ( $url ) = @_; + + my $trailing_char = substr $url, -1; + if ( $trailing_char ne '/' ) { + $url .= '/'; + } return $url; }