<commit_before>package Eixo::Rest::Api;

use strict;
use Eixo::Base::Clase;

use Data::Dumper;
use Attribute::Handlers;
use Eixo::Rest::Client;
use Carp;

use Eixo::Rest::Uri;

my $JOB_ID = 1;

has (

	client => undef,

	jobs=>[],

);

sub AUTOLOAD{
	my ($self, @args) = @_;

	my ($method) = our $AUTOLOAD =~ /\:\:(\w+)$/;

	if($method =~ /^get|^post|^patch|^delete|^update|^put/){
		@args = $self->__analyzeRequest($method, @args);
	}
    else{
        die("Unknown REST method " . $method);
    }

	$self->client->$method(@args);
}


sub DESTROY {}

sub initialize{

    my ($self, $endpoint, %opts) = @_;

    $self->client(
        Eixo::Rest::Client->new($endpoint, %opts)
    );

    $self->SUPER::initialize(%opts);

    $self;
}

sub produce{
	my ($self, $class, %args) = @_;
	
	$self->__loadProduct($class);

	$class->new(

		api=>$self,

		%args

	)
}

sub newJob{
	my ($self, $request, $id) = @_;

	push @{$self->jobs}, $request;

	$self;
}

sub jobFinished{
	my ($self, $request) = @_;

	$self->jobs([ grep {

		$_ != $request

	} @{$self->jobs} ] );
}

sub waitForJob{
	my ($self, $job_id) = @_;

	$self->waitForJobs($job_id);
	
}

sub waitForJobs{
	my ($self, $id) = @_;

	while(scalar(@{$self->jobs})){

		if($id){
			return unless(scalar(grep { $_->job_id == $id } @{$self->jobs}));
		}

		foreach my $job (@{$self->jobs}){

			$job->process;

			select(undef, undef, undef, 0.05);

		}
	}
}

sub __analyzeRequest {
	my ($self, $method, %args) = @_;

	my $params = $args{args};

	# print "params:".Dumper($params);
	# if(exists $args{onError}){
	# 	$self->client->error_callback($args{onError});
	# }

	# check args needed
	if($args{needed}){

		foreach (@{$args{needed}}){

			&{$self->client->error_callback}(

				$method, 

				'PARAM_NEEDED', 

				$_ ) unless(defined($params->{$_}));

		}		

	}	

	# build GET_DATA & POST_DATA
	unless(exists($params->{GET_DATA})){
        $self->__buildGetParams($args{get_params}, $params);

	}

	unless(exists($args{POST_DATA})){
        $self->__buildPostParams($args{post_params}, $params);
	}

    # build URI (if provided)
    if($args{uri_mask}){

        my ($uri, @implicit_params) = Eixo::Rest::Uri->new(

            args=>$params,

            uri_mask=>$args{uri_mask}

        )->build;

        # erase implicit params from the get_params array
        delete $params->{GET_DATA}->{$_} foreach(@implicit_params);

        # we pass the newly created uri
        $params->{uri} = $uri;
    }

	delete($params->{$_}) foreach(@{$args{get_params}}, @{$args{post_params}});

	
	# default callback function
	#
	# identity function

	my $default_func = sub {

		(wantarray)? @_ : $_[0];

	};


	# needed, maybe must die if not provided
	$params->{__callback} = $args{__callback} || $default_func;



	# build PROCESS_DATA<commit_msg>POST_PARAMS must be erased if present in the uri<commit_after>package Eixo::Rest::Api;

use strict;
use Eixo::Base::Clase;

use Data::Dumper;
use Attribute::Handlers;
use Eixo::Rest::Client;
use Carp;

use Eixo::Rest::Uri;

my $JOB_ID = 1;

has (

	client => undef,

	jobs=>[],

);

sub AUTOLOAD{
	my ($self, @args) = @_;

	my ($method) = our $AUTOLOAD =~ /\:\:(\w+)$/;

	if($method =~ /^get|^post|^patch|^delete|^update|^put/){
		@args = $self->__analyzeRequest($method, @args);
	}
    else{
        die("Unknown REST method " . $method);
    }

	$self->client->$method(@args);
}


sub DESTROY {}

sub initialize{

    my ($self, $endpoint, %opts) = @_;

    $self->client(
        Eixo::Rest::Client->new($endpoint, %opts)
    );

    $self->SUPER::initialize(%opts);

    $self;
}

sub produce{
	my ($self, $class, %args) = @_;
	
	$self->__loadProduct($class);

	$class->new(

		api=>$self,

		%args

	)
}

sub newJob{
	my ($self, $request, $id) = @_;

	push @{$self->jobs}, $request;

	$self;
}

sub jobFinished{
	my ($self, $request) = @_;

	$self->jobs([ grep {

		$_ != $request

	} @{$self->jobs} ] );
}

sub waitForJob{
	my ($self, $job_id) = @_;

	$self->waitForJobs($job_id);
	
}

sub waitForJobs{
	my ($self, $id) = @_;

	while(scalar(@{$self->jobs})){

		if($id){
			return unless(scalar(grep { $_->job_id == $id } @{$self->jobs}));
		}

		foreach my $job (@{$self->jobs}){

			$job->process;

			select(undef, undef, undef, 0.05);

		}
	}
}

sub __analyzeRequest {
	my ($self, $method, %args) = @_;

	my $params = $args{args};

	# print "params:".Dumper($params);
	# if(exists $args{onError}){
	# 	$self->client->error_callback($args{onError});
	# }

	# check args needed
	if($args{needed}){

		foreach (@{$args{needed}}){

			&{$self->client->error_callback}(

				$method, 

				'PARAM_NEEDED', 

				$_ ) unless(defined($params->{$_}));

		}		

	}	

	# build GET_DATA & POST_DATA
	unless(exists($params->{GET_DATA})){
        $self->__buildGetParams($args{get_params}, $params);

	}

	unless(exists($args{POST_DATA})){
        $self->__buildPostParams($args{post_params}, $params);
	}

    # build URI (if provided)
    if($args{uri_mask}){

        my ($uri, @implicit_params) = Eixo::Rest::Uri->new(

            args=>$params,

            uri_mask=>$args{uri_mask}

        )->build;

        # erase implicit params from the get_params and post_params arrays
        delete $params->{GET_DATA}->{$_} foreach(@implicit_params);
        delete $params->{POST_DATA}->{$_} foreach(@implicit_params);

        # we pass the newly created uri
        $params->{uri} = $uri;
    }

	delete($params->{$_}) foreach(@{$args{get_params}}, @{$args{post_params}});
	
	# default callback function
	#
	# identity function

	my $default_func = sub {

		(wantarray)? @_ : $_[0];

	};


	# needed, maybe must die if not provided
	$params->{__callback} = $args{__callback} || $default_func;



	# build PROCESS_DATA
{'fraction_non_alphanumeric': 0.20748775367389782, 'fraction_numerical': 0.0017494751574527643, 'mean_word_length': 3.360793287566743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2316967', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2154, 'n_words': 414}