Arango-DB

 view release on metacpan or  search on metacpan

lib/Arango/DB/API.pm  view on Meta::CPAN

# ABSTRACT: Internal module with the API specification
package Arango::DB::API;
$Arango::DB::API::VERSION = '0.006';
use Arango::DB::Database;
use Arango::DB::Collection;

use strict;
use warnings;
use HTTP::Tiny;
use JSON;
use Clone 'clone';
use MIME::Base64 3.11 'encode_base64url';
use URI::Encode qw(uri_encode);
use JSON::Schema::Fit 0.02;

my %API = (
    create_document   => { method => 'post',   uri => '{database}_api/document/{collection}' },
    delete_collection => { method => 'delete', uri => '{database}_api/collection/{name}'     },
    delete_database   => { method => 'delete', uri => '_api/database/{name}'                 },
    list_collections  => { method => 'get',    uri => '{database}_api/collection'            },
    cursor_next       => { method => 'put',    uri => '{database}_api/cursor/{id}'           },
    cursor_delete     => { method => 'delete', uri => '{database}_api/cursor/{id}'           },
    list_databases    => { method => 'get',    uri => '_api/database'                        },
    status            => { method => 'get',    uri => '_admin/status'                        },
    time              => { method => 'get',    uri => '_admin/time'                          },
    statistics        => { method => 'get',    uri => '_admin/statistics'                    },
    statistics_description  => { method => 'get', uri => '_admin/statistics-description'     },
    'create_database' => {
        method  => 'post',
        uri     => '_api/database',
        params  => { name => { type => 'string' }},
        builder => sub { 
            my ($self, %params) = @_;
            return Arango::DB::Database->_new(arango => $self, 'name' => $params{name});
        },
    },
   'create_collection' => {
        method  => 'post',
        uri     => '{database}_api/collection',
        params  => { name => { type => 'string' }},
        builder => sub {
            my ($self, %params) = @_;
            return Arango::DB::Collection->_new(arango => $self, database => $params{database}, 'name' => $params{name});
        },
    },
    'all_keys' => {
        method => 'put',
        uri    => '{database}_api/simple/all-keys',
        params => { type => { type => 'string' }, collection => { type => 'string' } },
    },
    'version' => {
        method => 'get',
        uri    => '_api/version',
        params => {  details => { type => 'boolean' } } ,
    },
    'create_cursor' => {
        method => 'post',
        uri    => '{database}_api/cursor',
        params => { 
            query       => { type => 'string'  }, 
            count       => { type => 'boolean' },
            batchSize   => { type => 'integer' },
            cache       => { type => 'boolean' },
            memoryLimit => { type => 'integer' },
            ttl         => { type => 'integer' },
            bindVars => { type => 'object', additionalProperties => 1 },
            options  => { type => 'object', additionalProperties => 0, properties => {
                    failOnWarning               => { type => 'boolean' },
                    profile                     => { type => 'integer', maximum => 2, minimum => 0 }, # 0, 1, 2
                    maxTransactionSize          => { type => 'integer' },
                    stream                      => { type => 'boolean' },
                    skipInaccessibleCollections => { type => 'boolean' },
                    maxWarningCount             => { type => 'integer' },
                    intermediateCommitCount     => { type => 'integer' },
                    satelliteSyncWait           => { type => 'integer' },
                    fullCount                   => { type => 'boolean' },
                    intermediateCommitSize      => { type => 'integer' },
                    'optimizer.rules'           => { type => 'string'  },
                    maxPlans                    => { type => 'integer' },
                 }
            },
        },
      },
      delete_user => { method => 'delete', uri => '_api/user/{username}' },
      create_user => {
          method => 'post',
          uri => '_api/user',
          params => {
              password => { type => 'string' },
              active => { type => 'boolean' },
              user => { type => 'string' },
              extra => { type => 'object', additionalProperties => 1 },
          }
      },
);



sub _check_options {
    my ($params, $properties) = @_;
    my $schema = { type => 'object', additionalProperties => 0, properties => $properties };
    my $prepared_data = JSON::Schema::Fit->new()->get_adjusted($params, $schema);
    return $prepared_data;
}

sub _api {
    my ($self, $action, $params) = @_;
    
    my $uri = $API{$action}{uri};

    my $params_copy = clone $params;

    $uri =~ s!\{database\}! defined $params->{database} ? "_db/$params->{database}/" : "" !e;
    $uri =~ s/\{([^}]+)\}/$params->{$1}/g;
    
    my $url = "http://" . $self->{host} . ":" . $self->{port} . "/" . $uri;

    my $body = ref($params) eq "HASH" && exists $params->{body} ? $params->{body} : undef;



( run in 2.454 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )