From 9589d0e85c678d0dc44751fe234c574c3be45ba0 Mon Sep 17 00:00:00 2001 From: Arne Johannessen Date: Tue, 12 Jan 2021 00:49:21 +0100 Subject: [PATCH] Add support for Bolt to version pre-check --- Build.PL | 4 +-- lib/REST/Neo4p.pm | 43 +++++++++------------------- lib/REST/Neo4p/Agent/Neo4j/Driver.pm | 4 +-- t/002_agent.t | 2 +- 4 files changed, 19 insertions(+), 34 deletions(-) diff --git a/Build.PL b/Build.PL index 22fc3ae..d98a972 100644 --- a/Build.PL +++ b/Build.PL @@ -78,7 +78,7 @@ my $build = $class->new 'experimental' => 0, 'MIME::Base64' => 0, perl => 5.010001, - 'Neo4j::Driver' => '0.16', + 'Neo4j::Driver' => '0.19', }, recommends => { 'Mojo::UserAgent' => 0, @@ -105,7 +105,7 @@ my $build = $class->new 'Test::Pod' => 1.0, 'Mojo::UserAgent' => 0, 'HTTP::Thin' => 0, - 'Neo4j::Driver' => '0.16', + 'Neo4j::Driver' => '0.19', }, meta_merge => { resources => { diff --git a/lib/REST/Neo4p.pm b/lib/REST/Neo4p.pm index 7f836ad..f97dd90 100644 --- a/lib/REST/Neo4p.pm +++ b/lib/REST/Neo4p.pm @@ -7,6 +7,7 @@ use URI; use URI::Escape; use HTTP::Tiny; use JSON::ize; +use Neo4j::Driver 0.1801; use REST::Neo4p::Agent; use REST::Neo4p::Node; use REST::Neo4p::Index; @@ -16,7 +17,7 @@ use strict; use warnings; BEGIN { - $REST::Neo4p::VERSION = '0.4001'; + $REST::Neo4p::VERSION = '0.4002'; } our $CREATE_AUTO_ACCESSORS = 0; @@ -137,51 +138,35 @@ sub connect { REST::Neo4p::LocalException->throw("Server address not set\n") unless $server_address; my ($major, $minor, $patch, $milestone); eval { - ($major, $minor, $patch, $milestone) = get_neo4j_version($server_address); + ($major) = get_neo4j_version( $server_address, $user // $u, $pass // $p ); }; if (my $e = Exception::Class->caught) { REST::Neo4p::CommException->throw("On version pre-check: $e"); } if ($major >= 4) { unless ($AGENT_MODULE eq 'Neo4j::Driver') { - unless (eval "require Neo4j::Driver; 1") { - REST::Neo4j::Exception->throw("Neo4j version 4 or higher requires Neo4j::Driver as agent module, but Neo4j::Driver is not installed in your system"); - } warn "Neo4j version 4 or higher requires Neo4j::Driver as agent module; using this instead of $AGENT_MODULE"; $AGENT_MODULE = 'Neo4j::Driver'; } } + elsif ($uri->scheme eq 'bolt') { + unless ($AGENT_MODULE eq 'Neo4j::Driver') { + warn "Bolt requires Neo4j::Driver as agent module; using this instead of $AGENT_MODULE"; + $AGENT_MODULE = 'Neo4j::Driver'; + } + } $neo4p->agent->credentials($server_address,'Neo4j',$user,$pass) if defined $user; my $connected = $neo4p->agent->connect($server_address); return $HANDLES[$HANDLE]->{_connected} = $connected; } sub get_neo4j_version { - my ($url) = @_; - my $version; - my $resp = HTTP::Tiny->new( default_headers => { 'Content-Type' => 'application/json'}) - ->get($url); - if ($resp->{success}) { - my $content = J($resp->{content}); - $version = $content->{neo4j_version}; - unless (defined $version) { - $resp = HTTP::Tiny->new( default_headers => { 'Content-Type' => 'application/json'}) - ->get("$url/db/data/"); - if ($resp->{success}) { - $content = J($resp->{content}); - $version = $content->{neo4j_version}; - } - else { - die "$resp->{status} $resp->{reason}"; - } - } - die "Neo4j version not found (is $url a Neo4j endpoint?)" unless defined $version; - } - else { - die "$resp->{status} $resp->{reason}"; - } + my ($url, $user, $pass) = @_; + my $driver = Neo4j::Driver->new($url); + $driver->basic_auth($user, $pass) if $user || $pass; + my $version = $driver->session->server->version; my ($major, $minor, $patch, $milestone) = - $version =~ /^(?:([0-9]+)\.)(?:([0-9]+)\.)?([0-9]+)?(?:-M([0-9]+))?/; + $version =~ /^Neo4j\/(?:([0-9]+)\.)(?:([0-9]+)\.)?([0-9]+)?(?:-M([0-9]+))?/; return wantarray ? ($major, $minor, $patch, $milestone) : $version; } diff --git a/lib/REST/Neo4p/Agent/Neo4j/Driver.pm b/lib/REST/Neo4p/Agent/Neo4j/Driver.pm index 65a7450..152c428 100644 --- a/lib/REST/Neo4p/Agent/Neo4j/Driver.pm +++ b/lib/REST/Neo4p/Agent/Neo4j/Driver.pm @@ -2,7 +2,7 @@ package REST::Neo4p::Agent::Neo4j::Driver; use v5.10; use lib '../../../../../lib'; # testing use base qw/REST::Neo4p::Agent/; -use Neo4j::Driver; +use Neo4j::Driver 0.1803; use JSON::ize; use REST::Neo4p::Agent::Neo4j::DriverActions; use REST::Neo4p::Exceptions; @@ -202,7 +202,7 @@ sub session { } my $session = $self->driver->session( $self->database ? (database => $self->database) : () ); if ($self->server_uri->scheme =~ /^http/) { - if (my $client = $session->{transport}{client}) { + if (my $client = $session->{net}{http_agent}{client}) { $client->setTimeout($self->timeout); $client->setCa($self->tls_ca); if ($self->{_ssl_opts}) { diff --git a/t/002_agent.t b/t/002_agent.t index d3c9fd0..fe1cba3 100644 --- a/t/002_agent.t +++ b/t/002_agent.t @@ -25,7 +25,7 @@ my $TEST_SERVER = $build ? $build->notes('test_server') : $ENV{REST_NEO4P_TEST_S my ($maj, @others); eval { - ($maj, @others) = REST::Neo4p::get_neo4j_version($TEST_SERVER); + ($maj, @others) = REST::Neo4p::get_neo4j_version($TEST_SERVER, $user, $pass); }; SKIP : {