Arango-Tango

 view release on metacpan or  search on metacpan

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

                      my $self = shift;
                      my %required = ();
                      my %optional = ();
                      if (exists($value->{signature})) {
                          if (scalar(@_) < scalar( grep { !/^\?/ } @{$value->{signature}})) {
                              die sprintf("Arango::Tango | %s | Missing %s", $method, $value->{signature}[scalar(@_) - 1]);
                          }
                          %required = ( map { $_ => shift @_ } grep { !/^\?/ } @{$value->{signature}} );
                          %optional = ( map {
                              /^\?(.+)$/ and $a = $1;
                              ref($_[0]) ? () : ($a => shift @_)
                          } grep {  /^\?/ } @{$value->{signature}} );

                          %required = ( %required, %optional );
                      }

                      if (exists($value->{inject_properties})) {
                          foreach my $property (@{$value->{inject_properties}}) {
                              if (ref($property) eq "HASH") {
                                  die "Property injection without property" unless exists $property->{prop};
                                  die "Property injection without alias"    unless exists $property->{as};
                                  $required{$property->{as}} = $self->{$property->{prop}};
                              }
                              else {
                                  $required{$property} = $self->{$property};
                              }
                          }
                      }
                      die sprintf("Arango::Tango | %s | Odd number of elements on options hash", $method) if scalar(@_) % 2;
                      my $arango = ref($self) eq "Arango::Tango" ? $self : $self->{arango};
                      return $arango->__api( $value, { @_, %required });
                  })
          };
    }
}

my %API = (
    'bulk_import_list'   => {
        rest => [ post => '{{database}}_api/import?collection={collection}'],
        url_schema => {
            type        => { type => 'string', pattern => 'documents|list|auto'  },
            fromPrefix  => { type => 'string'  },
            toPrefix    => { type => 'string'  },
            overwrite   => { type => 'boolean' },
            waitForSync => { type => 'boolean' },
            onDuplicate => { type => 'string', pattern => 'error|update|replace|ignore'  },
            complete    => { type => 'boolean' },
            details     => { type => 'boolean' } 
        }
    },
    'create_document'    => {
        rest => [ post  => '{{database}}_api/document/{collection}']
    },
    'replace_document'   => {
        rest => [ put => '{{database}}_api/document/{collection}/{key}' ],
    },
    'list_collections'   => {
        rest => [ get   => '{{database}}_api/collection'],
        schema => { excludeSystem => { type => 'boolean' } }
    },
    'cursor_next'        => {
        rest => [ put => '{{database}}_api/cursor/{id}']
    },
    'cursor_delete'      => {
        rest => [ delete => '{{database}}_api/cursor/{id}']
    },
    'accessible_databases' => {
        rest => [ get => '_api/database/user']
    },
    'all_keys' => {
        rest => [ put => '{{database}}_api/simple/all-keys' ],
        schema => { type => { type => 'string' }, collection => { type => 'string' } },
    },
    'create_cursor' => {
        rest => [ post => '{{database}}_api/cursor' ],
        schema => {
            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' },
                 }
            },
        },
      },

      create_user => {
          method => 'post',
          uri => '_api/user',
          schema => {
              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(replace_invalid_values => 1)->get_adjusted($params, $schema);
    return $prepared_data;
}

sub _api {
    my ($self, $action, $params) = @_;
    return $self->__api( $API{$action}, $params);
}

sub __api {
    my ($self, $conf, $params) = @_;

    my ($method, $uri) = @{$conf->{rest}};
    $method = uc $method;

    my $params_copy = clone $params; ## FIXME: decide if this is relevant



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