Skip to content

Commit

Permalink
Add support for Bolt to version pre-check
Browse files Browse the repository at this point in the history
  • Loading branch information
johannessen committed Jan 12, 2021
1 parent c0b65af commit 9589d0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 => {
Expand Down
43 changes: 14 additions & 29 deletions lib/REST/Neo4p.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +17,7 @@ use strict;
use warnings;

BEGIN {
$REST::Neo4p::VERSION = '0.4001';
$REST::Neo4p::VERSION = '0.4002';
}

our $CREATE_AUTO_ACCESSORS = 0;
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/REST/Neo4p/Agent/Neo4j/Driver.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}) {
Expand Down
2 changes: 1 addition & 1 deletion t/002_agent.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 : {
Expand Down

0 comments on commit 9589d0e

Please sign in to comment.