| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # ABSTRACT: Internal module with the API specification | 
| 2 |  |  |  |  |  |  | package Arango::DB::API; | 
| 3 |  |  |  |  |  |  | $Arango::DB::API::VERSION = '0.004'; | 
| 4 | 4 |  |  | 4 |  | 1597 | use Arango::DB::Database; | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 112 |  | 
| 5 | 4 |  |  | 4 |  | 1587 | use Arango::DB::Collection; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 152 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 4 |  |  | 4 |  | 26 | use strict; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 72 |  | 
| 8 | 4 |  |  | 4 |  | 16 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 87 |  | 
| 9 | 4 |  |  | 4 |  | 2811 | use HTTP::Tiny; | 
|  | 4 |  |  |  |  | 200846 |  | 
|  | 4 |  |  |  |  | 165 |  | 
| 10 | 4 |  |  | 4 |  | 2592 | use JSON; | 
|  | 4 |  |  |  |  | 48148 |  | 
|  | 4 |  |  |  |  | 31 |  | 
| 11 | 4 |  |  | 4 |  | 2064 | use Clone 'clone'; | 
|  | 4 |  |  |  |  | 8817 |  | 
|  | 4 |  |  |  |  | 248 |  | 
| 12 | 4 |  |  | 4 |  | 1658 | use MIME::Base64 3.11 'encode_base64url'; | 
|  | 4 |  |  |  |  | 2373 |  | 
|  | 4 |  |  |  |  | 261 |  | 
| 13 | 4 |  |  | 4 |  | 1769 | use URI::Encode qw(uri_encode); | 
|  | 4 |  |  |  |  | 45077 |  | 
|  | 4 |  |  |  |  | 239 |  | 
| 14 | 4 |  |  | 4 |  | 1852 | use JSON::Schema::Fit; | 
|  | 4 |  |  |  |  | 136601 |  | 
|  | 4 |  |  |  |  | 2949 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | my %API = ( | 
| 17 |  |  |  |  |  |  | 'list_databases'  => { | 
| 18 |  |  |  |  |  |  | method => 'get', | 
| 19 |  |  |  |  |  |  | uri => '_api/database', | 
| 20 |  |  |  |  |  |  | 'params' => { details => { type => 'boolean' }} | 
| 21 |  |  |  |  |  |  | }, | 
| 22 |  |  |  |  |  |  | 'create_database' => { | 
| 23 |  |  |  |  |  |  | method => 'post', | 
| 24 |  |  |  |  |  |  | uri => '_api/database', | 
| 25 |  |  |  |  |  |  | 'builder' => sub { | 
| 26 |  |  |  |  |  |  | my ($self, %params) = @_; | 
| 27 |  |  |  |  |  |  | return Arango::DB::Database->_new(arango => $self, 'name' => $params{name}); | 
| 28 |  |  |  |  |  |  | }, | 
| 29 |  |  |  |  |  |  | params => { name => { type => 'string' }} | 
| 30 |  |  |  |  |  |  | }, | 
| 31 |  |  |  |  |  |  | 'create_document' => { | 
| 32 |  |  |  |  |  |  | method => 'post', | 
| 33 |  |  |  |  |  |  | uri => '{database}_api/document/{collection}' | 
| 34 |  |  |  |  |  |  | }, | 
| 35 |  |  |  |  |  |  | 'create_collection' => { | 
| 36 |  |  |  |  |  |  | method => 'post', | 
| 37 |  |  |  |  |  |  | uri => '{database}_api/collection', | 
| 38 |  |  |  |  |  |  | 'builder' => sub { | 
| 39 |  |  |  |  |  |  | my ($self, %params) = @_; | 
| 40 |  |  |  |  |  |  | return Arango::DB::Collection->_new(arango => $self, database => $params{database}, 'name' => $params{name}); | 
| 41 |  |  |  |  |  |  | }, | 
| 42 |  |  |  |  |  |  | params => { name => { type => 'string' }} | 
| 43 |  |  |  |  |  |  | }, | 
| 44 |  |  |  |  |  |  | 'delete_collection' => { | 
| 45 |  |  |  |  |  |  | method => 'delete', | 
| 46 |  |  |  |  |  |  | uri => '{database}_api/collection/{name}' | 
| 47 |  |  |  |  |  |  | }, | 
| 48 |  |  |  |  |  |  | 'delete_database' => { | 
| 49 |  |  |  |  |  |  | method => 'delete', | 
| 50 |  |  |  |  |  |  | uri => '_api/database/{name}' | 
| 51 |  |  |  |  |  |  | }, | 
| 52 |  |  |  |  |  |  | 'list_collections' => { | 
| 53 |  |  |  |  |  |  | method => 'get', | 
| 54 |  |  |  |  |  |  | uri => '{database}_api/collection' | 
| 55 |  |  |  |  |  |  | }, | 
| 56 |  |  |  |  |  |  | 'all_keys' => { | 
| 57 |  |  |  |  |  |  | method => 'put', | 
| 58 |  |  |  |  |  |  | uri => '{database}_api/simple/all-keys', | 
| 59 |  |  |  |  |  |  | params => { type => { type => 'string' }, collection => { type => 'string' } }, | 
| 60 |  |  |  |  |  |  | }, | 
| 61 |  |  |  |  |  |  | 'version' => { | 
| 62 |  |  |  |  |  |  | method => 'get', | 
| 63 |  |  |  |  |  |  | uri => '_api/version', | 
| 64 |  |  |  |  |  |  | params => {  details => { type => 'boolean' } } , | 
| 65 |  |  |  |  |  |  | }, | 
| 66 |  |  |  |  |  |  | 'create_cursor' => { | 
| 67 |  |  |  |  |  |  | method => 'post', | 
| 68 |  |  |  |  |  |  | uri => '{database}_api/cursor', | 
| 69 |  |  |  |  |  |  | params => { query => { type => 'string' }, count => { type => 'boolean' }}, | 
| 70 |  |  |  |  |  |  | }, | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub _check_options { | 
| 77 | 0 |  |  | 0 |  |  | my ($params, $properties) = @_; | 
| 78 | 0 |  |  |  |  |  | my $schema = { type => 'object', additionalProperties => 0, properties => $properties }; | 
| 79 | 0 |  |  |  |  |  | my $prepared_data = JSON::Schema::Fit->new()->get_adjusted($params, $schema); | 
| 80 | 0 |  |  |  |  |  | return $prepared_data; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub _api { | 
| 84 | 0 |  |  | 0 |  |  | my ($self, $action, $params) = @_; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | my $uri = $API{$action}{uri}; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 0 |  |  |  |  |  | my $params_copy = clone $params; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 0 | 0 |  |  |  |  | $uri =~ s!\{database\}! defined $params->{database} ? "_db/$params->{database}/" : "" !e; | 
|  | 0 |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  |  | $uri =~ s/\{([^}]+)\}/$params->{$1}/g; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 0 |  |  |  |  |  | my $url = "http://" . $self->{host} . ":" . $self->{port} . "/" . $uri; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 | 0 | 0 |  |  |  | my $body = ref($params) eq "HASH" && exists $params->{body} ? $params->{body} : undef; | 
| 96 | 0 | 0 |  |  |  |  | my $opts = ref($params) eq "HASH" ? $params : {}; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 | 0 |  |  |  |  | $opts = exists($API{$action}{params}) ? _check_options($opts, $API{$action}{params}) : {}; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 | 0 | 0 |  |  |  | if ($API{$action}{method} eq 'get' && scalar(keys %$opts)) { | 
| 101 | 0 |  |  |  |  |  | $url .= "?" . join("&", map { "$_=" . uri_encode($opts->{$_} )} keys %$opts); | 
|  | 0 |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | } else { | 
| 103 | 0 | 0 | 0 |  |  |  | if ($body && ref($body) eq "HASH") { | 
|  |  | 0 |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | $opts = { content => encode_json $body } | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  | elsif (defined($body)) { # JSON | 
| 107 | 0 |  |  |  |  |  | $opts = { content => $body } | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | else { | 
| 110 | 0 |  |  |  |  |  | $opts = { content => encode_json $opts } | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | #use Data::Dumper; | 
| 115 |  |  |  |  |  |  | # print STDERR "\n -- $API{$action}{method} | $url\n"; | 
| 116 |  |  |  |  |  |  | #print STDERR "\n\nOPTS:\n\n", Dumper($opts); | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 0 |  |  |  |  |  | my $response = $self->{http}->request($API{$action}{method}, $url, $opts); | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 0 | 0 |  |  |  |  | if ($response->{success}) { | 
| 122 | 0 |  |  |  |  |  | my $ans = decode_json($response->{content}); | 
| 123 | 0 | 0 |  |  |  |  | if ($ans->{error}) { | 
|  |  | 0 |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | return $ans; | 
| 125 |  |  |  |  |  |  | } elsif (exists($API{$action}{builder})) { | 
| 126 | 0 |  |  |  |  |  | return $API{$action}{builder}->( $self, %$params_copy ); | 
| 127 |  |  |  |  |  |  | } else { | 
| 128 | 0 |  |  |  |  |  | return $ans; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  | else { | 
| 132 | 0 |  |  |  |  |  | die "Arango::DB | ($response->{status}) $response->{reason}"; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | 1; | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | __END__ |