| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package REST::Cypher::Agent; | 
| 2 |  |  |  |  |  |  | { | 
| 3 |  |  |  |  |  |  | $REST::Cypher::Agent::DIST = 'REST-Cypher'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  | $REST::Cypher::Agent::VERSION = '0.0.3'; | 
| 6 |  |  |  |  |  |  | # ABSTRACT: Experimental client for using neo4j's REST/Cypher interface | 
| 7 |  |  |  |  |  |  | # KEYWORDS: neo4j graph graphdb cypher REST | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 3 |  |  | 3 |  | 124589 | use Moo; | 
|  | 3 |  |  |  |  | 14792 |  | 
|  | 3 |  |  |  |  | 23 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 3 |  |  | 3 |  | 7965 | use REST::Cypher::Exception; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 97 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 3 |  |  | 3 |  | 909 | use MooX::Types::MooseLike::Base qw/Bool/; | 
|  | 3 |  |  |  |  | 6875 |  | 
|  | 3 |  |  |  |  | 187 |  | 
| 14 | 3 |  |  | 3 |  | 808 | use MooseX::Params::Validate; | 
|  | 3 |  |  |  |  | 569648 |  | 
|  | 3 |  |  |  |  | 24 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 3 |  |  | 3 |  | 4328 | use JSON::Any; | 
|  | 3 |  |  |  |  | 10692 |  | 
|  | 3 |  |  |  |  | 15 |  | 
| 17 | 3 |  |  | 3 |  | 23730 | use LWP::UserAgent; | 
|  | 3 |  |  |  |  | 191536 |  | 
|  | 3 |  |  |  |  | 1819 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | has base_url => ( | 
| 20 |  |  |  |  |  |  | is          => 'rw', | 
| 21 |  |  |  |  |  |  | required    => 1, | 
| 22 |  |  |  |  |  |  | writer      => '_base_url', | 
| 23 |  |  |  |  |  |  | ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | has cypher_url => ( | 
| 26 |  |  |  |  |  |  | is          => 'ro', | 
| 27 |  |  |  |  |  |  | lazy        => 1, | 
| 28 |  |  |  |  |  |  | default => sub { | 
| 29 |  |  |  |  |  |  | my $self = shift; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | my $base = $self->base_url; | 
| 32 |  |  |  |  |  |  | if($base =~ s{/$}{}) { | 
| 33 |  |  |  |  |  |  | $self->_base_url( $base ); | 
| 34 |  |  |  |  |  |  | warn $self->base_url; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sprintf( | 
| 38 |  |  |  |  |  |  | '%s/db/data/cypher', | 
| 39 |  |  |  |  |  |  | $self->base_url, | 
| 40 |  |  |  |  |  |  | ) | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | ); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | has agent_string => ( | 
| 45 |  |  |  |  |  |  | is      => 'ro', | 
| 46 |  |  |  |  |  |  | default => sub { q[REST::Cypher::Agent/0.0.0] }, | 
| 47 |  |  |  |  |  |  | ); | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | has agent => ( | 
| 50 |  |  |  |  |  |  | is      => 'ro', | 
| 51 |  |  |  |  |  |  | lazy    => 1, | 
| 52 |  |  |  |  |  |  | default => sub { | 
| 53 |  |  |  |  |  |  | my $self = shift; | 
| 54 |  |  |  |  |  |  | LWP::UserAgent->new( | 
| 55 |  |  |  |  |  |  | agent               => $self->agent_string, | 
| 56 |  |  |  |  |  |  | protocols_allowed   => [ 'http', 'https'], | 
| 57 |  |  |  |  |  |  | default_header      => [ Accept => 'application/json' ], | 
| 58 |  |  |  |  |  |  | ); | 
| 59 |  |  |  |  |  |  | }, | 
| 60 |  |  |  |  |  |  | ); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | has auth_token => ( | 
| 63 |  |  |  |  |  |  | is      => 'rw', | 
| 64 |  |  |  |  |  |  | lazy    => 1, | 
| 65 |  |  |  |  |  |  | default => 'bmVvNGo6bmVvNGo=', | 
| 66 |  |  |  |  |  |  | ); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | has last_response => ( | 
| 69 |  |  |  |  |  |  | is      => 'rw', | 
| 70 |  |  |  |  |  |  | ); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | has debug => ( | 
| 73 |  |  |  |  |  |  | is      => 'rw', | 
| 74 |  |  |  |  |  |  | isa     => Bool, | 
| 75 |  |  |  |  |  |  | default => 0, | 
| 76 |  |  |  |  |  |  | ); | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub GET { | 
| 80 | 0 |  |  | 0 | 1 | 0 | my ($self, %params) = validated_hash( | 
| 81 |  |  |  |  |  |  | \@_, | 
| 82 |  |  |  |  |  |  | query_string => { isa => 'Str' }, | 
| 83 |  |  |  |  |  |  | ); | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | my $string = | 
| 86 |  |  |  |  |  |  | sprintf( | 
| 87 |  |  |  |  |  |  | '%s/db/data%s', | 
| 88 |  |  |  |  |  |  | $self->base_url, | 
| 89 |  |  |  |  |  |  | $params{query_string}, | 
| 90 | 0 |  |  |  |  | 0 | ); | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 |  |  |  |  | 0 | $self->last_response( | 
| 93 |  |  |  |  |  |  | $self->agent->get($string) | 
| 94 |  |  |  |  |  |  | ); | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | sub POST { | 
| 99 | 2 |  |  | 2 | 1 | 90 | my ($self, %params) = validated_hash( | 
| 100 |  |  |  |  |  |  | \@_, | 
| 101 |  |  |  |  |  |  | query_string    => { isa => 'Str',      optional => 0, }, | 
| 102 |  |  |  |  |  |  | query_params    => { isa => 'HashRef',  optional => 1, }, | 
| 103 |  |  |  |  |  |  | ); | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | my $json = JSON::Any->objToJson( | 
| 106 |  |  |  |  |  |  | { | 
| 107 |  |  |  |  |  |  | query   => $params{query_string}, | 
| 108 |  |  |  |  |  |  | params  => $params{query_params}, | 
| 109 |  |  |  |  |  |  | } | 
| 110 | 2 |  |  |  |  | 1104 | ); | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 2 | 50 |  |  |  | 122 | if ($self->debug) { | 
| 113 | 0 |  |  |  |  | 0 | my $tmp =  $params{query_string}; | 
| 114 | 0 |  |  |  |  | 0 | $tmp =~ s{\s+}{ }g; | 
| 115 | 0 |  |  |  |  | 0 | warn "[POST] $tmp\n"; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | $self->last_response( | 
| 119 | 2 |  |  |  |  | 586 | $self->agent->post( | 
| 120 |  |  |  |  |  |  | $self->cypher_url, | 
| 121 |  |  |  |  |  |  | Content => $json, | 
| 122 |  |  |  |  |  |  | 'Content-Type' => 'application/json', | 
| 123 |  |  |  |  |  |  | 'Authorization' => "Basic " . $self->auth_token, | 
| 124 |  |  |  |  |  |  | ) | 
| 125 |  |  |  |  |  |  | ); | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 2 | 50 |  |  |  | 539857 | if (! $self->last_response->is_success) { | 
| 128 | 2 |  |  |  |  | 107 | REST::Cypher::Exception::Response->throw({ | 
| 129 |  |  |  |  |  |  | response => $self->last_response, | 
| 130 |  |  |  |  |  |  | }); | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | 1; | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | __END__ | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =pod | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =encoding UTF-8 | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =head1 NAME | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | REST::Cypher::Agent - Experimental client for using neo4j's REST/Cypher interface | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =head1 VERSION | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | version 0.0.3 | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | Interact with a neo4j Cypher API. | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head2 base_url | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | This is the full URL value of the neo4j server to connect to. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | base_url => http://my.neo4j.example.com:7474 | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | It is a B<required> attribute, with no default value. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =head2 cypher_url | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | This is the URL used to connect to the Cypher endpoint(s). | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | It is a B<derived> value, based on C<base_url> | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =head2 agent_string | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | This attribute provides the value for the User Agent string when making API calls. | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | This attribute has a B<default value> of C<REST::Cypher::Agent/0.0.0>, but may be overridden. | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | =head2 agent | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | This attribute holds the agent object, used for making the HTTP calls to the API endpoint. | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | The default value is an instance of L<LWP::UserAgent>. You may override this. I<At your own risk>. | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =head2 auth_token | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | neo4j allows authentication to be enabled for connections to the database. | 
| 185 |  |  |  |  |  |  | For I<recent> versions this is enabled by default. | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | The auth-token value is 'I<a base64 encoded string of "username:password">' | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | The B<default value> is set to the equivalent of C<encode_base64('neo4j:neo4j')>. | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =head2 last_response | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | This attribute stores the response object from the most recent call to the API. | 
| 194 |  |  |  |  |  |  | See L<HTTP::Response> for a description of the interface it provides. | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | =head2 debug | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | This I<boolean> attribute enables debugging output for the class. | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | =head1 METHODS | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =head2 GET | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | This method provides low-level access to C<LWP::UserAgent->get()>. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | It takes care of constructing the URL, using the provided query parameters. | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | The method returns the response object, after storing it in C<last_response>. | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | # (just) GET the base URL | 
| 211 |  |  |  |  |  |  | $response = $cypher_agent->GET({query_string => '' }); | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =head2 POST | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | =head1 GENERATING AUTH TOKEN | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | You can generate your own C<auth_token> value using L<MIME::Base64> | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | perl -MMIME::Base64 -e "warn encode_base64('neo4j:neo4j');" | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | =over 4 | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | =item * | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | L<neo4j|http://neo4j.org> | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =item * | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | L<REST::Neo4p> | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =back | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | =head1 AUTHOR | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | Chisel <chisel@chizography.net> | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | This software is copyright (c) 2015 by Chisel Wright. | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 244 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  | =cut |