<commit_before>package Net::Limesco;

use 5.012004;
use strict;
use warnings;

use Carp;
use LWP::UserAgent;
use JSON;

our $VERSION = "1.0";

=head1 NAME

Net::Limesco - Perl interface to the Limesco REST API

=head1 SYNOPSIS

  use Net::Limesco;
  use Data::Dumper;
  my $lim = Net::Limesco->new();
  $lim->obtainToken();
  print Dumper($lim->getMyInvoices());

=head1 DESCRIPTION

This module is an interface to the Limesco API found at
http://api.limesco.org/.

=head1 METHODS

=head2 new ([hostname, [port, [debug]]])

=cut

sub new {
	my ($pkg, $hostname, $port, $debug) = @_;
	$hostname ||= "api.limesco.org";
	$port ||= 80;
	$debug ||= 0;
	my $self = {
		hostname => $hostname,
		port => $port,
		debug => $debug,
		ua => LWP::UserAgent->new(),
	};
	$self->{ua}->timeout(2000);
	$self->{ua}->env_proxy();
	$self->{ua}->agent("Net::Limesco/$VERSION (".$self->{ua}->_agent().", Perl/$])");
	bless $self, $pkg;
	return $self;
}

=head2 obtainToken (username, password)

=cut

sub obtainToken {
	my ($self, $username, $password) = @_;
	$self->_debug("Obtaining token...\n");
	my $resp = $self->_post("/auth/login/obtainToken", {username => $username, password => $password});
	if(!$resp->{body}) {
		warn $resp->{error} . "\n";
		return;
	}
	$self->{token} = $resp->{body};
	$self->_debug("Token obtained: '%s'\n", $self->{token});
	return $self->{token};
}

=head2 getMySims ()

=cut

sub getMySims {
	my ($self) = @_;
	$self->_assertToken();
	$self->_debug("Obtaining my SIMs...\n");
	return $self->_get_json("/sims");
}

=head2 getMyInvoices ()

=cut

sub getMyInvoices {
	my ($self) = @_;
	$self->_assertToken();
	$self->_debug("Obtaining my invoices...\n");
	return $self->_get_json("/accounts/~/invoices");
}

=head2 getMyPayments ()

=cut

sub getMyPayments {
	my ($self) = @_;
	$self->_assertToken();
	$self->_debug("Obtaining my payments...\n");
	return $self->_get_json("/accounts/~/payments");
}

=head2 getIssuers ()

=cut

sub getIssuers {
	my ($self) = @_;
	$self->_debug("Obtaining issuers...\n");
	return $self->_get_json("/ideal/issuers");
}

=head1 ADMINISTRATOR METHODS

Methods to communicate with the administration part of the Limesco REST API.

=head2 getAccount (accountId)

=cut

sub getAccount {
	my ($self, $account) = @_;
	croak "Missing account ID" if(!$account);
	$self->_assertToken();
	$self->_debug("Obtaining information about account ID $account...\n");
	return $self->_get_json("/accounts/$account");
}

=head2 saveAccount (account)

=cut

sub saveAccount {
	my ($self, $account) = @_;
	my $id = $account->{'id'};
	croak "Missing account ID (use createAccount to create one)" if(!$id);
	$self->_assertToken();
	$self->_debug("Storing account with ID $id\n");
	return $self->_put("/accounts/$id", $account);
}

=head2 saveSim (sim)

=cut

sub saveSim {
	my ($self, $sim) = @_;
	my $id = $sim->{'iccid'};
	croak "Missing SIM ID (use createSim to create one)" if(!$id);
	$self->_assertToken();
	$self->_debug("Storing SIM with ID $id\n");
	return $self->_put("/sims/$id", $sim);
}

=head2 getAccountValidation (accountId)

=cut

sub getAccountValidation {
	my ($self, $accountid) = @_;
	croak "Missing account ID" if(!$accountid);
	$self->_assertToken();
	$self->_debug("Obtaining validation information about account ID $accountid...\n");
	my $response = $self->_get("/accounts/$accountid/validate");
	if($response->{'success'} && $response->{'code'} == 204) { # No content
		return ();
	}
	if(!$response->{'body'}) {
		warn $response->{'error'} . "\n";
		return;
	}
	return @{decode_json($response->{'body'})};
}

=head2 getSimsByOwnerId (accountId)

=cut

sub getSimsByOwnerId {
	my ($self, $accountid) = @_;
	croak "Missing account ID" if(!$accountid);
	$self->_assertToken();
	$self->_debug("Obtaining SIMs from owner ID $accountid...\n");
	return @{$self->_get_json("/accounts/$accountid/sims")};
}

=head2 getSim (simId)
<commit_msg>Implement SIM validation in Net::Limesco.<commit_after>package Net::Limesco;

use 5.012004;
use strict;
use warnings;

use Carp;
use LWP::UserAgent;
use JSON;

our $VERSION = "1.0";

=head1 NAME

Net::Limesco - Perl interface to the Limesco REST API

=head1 SYNOPSIS

  use Net::Limesco;
  use Data::Dumper;
  my $lim = Net::Limesco->new();
  $lim->obtainToken();
  print Dumper($lim->getMyInvoices());

=head1 DESCRIPTION

This module is an interface to the Limesco API found at
http://api.limesco.org/.

=head1 METHODS

=head2 new ([hostname, [port, [debug]]])

=cut

sub new {
	my ($pkg, $hostname, $port, $debug) = @_;
	$hostname ||= "api.limesco.org";
	$port ||= 80;
	$debug ||= 0;
	my $self = {
		hostname => $hostname,
		port => $port,
		debug => $debug,
		ua => LWP::UserAgent->new(),
	};
	$self->{ua}->timeout(2000);
	$self->{ua}->env_proxy();
	$self->{ua}->agent("Net::Limesco/$VERSION (".$self->{ua}->_agent().", Perl/$])");
	bless $self, $pkg;
	return $self;
}

=head2 obtainToken (username, password)

=cut

sub obtainToken {
	my ($self, $username, $password) = @_;
	$self->_debug("Obtaining token...\n");
	my $resp = $self->_post("/auth/login/obtainToken", {username => $username, password => $password});
	if(!$resp->{body}) {
		warn $resp->{error} . "\n";
		return;
	}
	$self->{token} = $resp->{body};
	$self->_debug("Token obtained: '%s'\n", $self->{token});
	return $self->{token};
}

=head2 getMySims ()

=cut

sub getMySims {
	my ($self) = @_;
	$self->_assertToken();
	$self->_debug("Obtaining my SIMs...\n");
	return $self->_get_json("/sims");
}

=head2 getMyInvoices ()

=cut

sub getMyInvoices {
	my ($self) = @_;
	$self->_assertToken();
	$self->_debug("Obtaining my invoices...\n");
	return $self->_get_json("/accounts/~/invoices");
}

=head2 getMyPayments ()

=cut

sub getMyPayments {
	my ($self) = @_;
	$self->_assertToken();
	$self->_debug("Obtaining my payments...\n");
	return $self->_get_json("/accounts/~/payments");
}

=head2 getIssuers ()

=cut

sub getIssuers {
	my ($self) = @_;
	$self->_debug("Obtaining issuers...\n");
	return $self->_get_json("/ideal/issuers");
}

=head1 ADMINISTRATOR METHODS

Methods to communicate with the administration part of the Limesco REST API.

=head2 getAccount (accountId)

=cut

sub getAccount {
	my ($self, $account) = @_;
	croak "Missing account ID" if(!$account);
	$self->_assertToken();
	$self->_debug("Obtaining information about account ID $account...\n");
	return $self->_get_json("/accounts/$account");
}

=head2 saveAccount (account)

=cut

sub saveAccount {
	my ($self, $account) = @_;
	my $id = $account->{'id'};
	croak "Missing account ID (use createAccount to create one)" if(!$id);
	$self->_assertToken();
	$self->_debug("Storing account with ID $id\n");
	return $self->_put("/accounts/$id", $account);
}

=head2 saveSim (sim)

=cut

sub saveSim {
	my ($self, $sim) = @_;
	my $id = $sim->{'iccid'};
	croak "Missing SIM ID (use createSim to create one)" if(!$id);
	$self->_assertToken();
	$self->_debug("Storing SIM with ID $id\n");
	return $self->_put("/sims/$id", $sim);
}

=head2 getAccountValidation (accountId)

=cut

sub getAccountValidation {
	my ($self, $accountid) = @_;
	croak "Missing account ID" if(!$accountid);
	$self->_assertToken();
	$self->_debug("Obtaining validation information about account ID $accountid...\n");
	my $response = $self->_get("/accounts/$accountid/validate");
	if($response->{'success'} && $response->{'code'} == 204) { # No content
		return ();
	}
	if(!$response->{'body'}) {
		warn $response->{'error'} . "\n";
		return;
	}
	return @{decode_json($response->{'body'})};
}

=head2 getSimValidation (simId)

=cut

sub getSimValidation {
	my ($self, $simid) = @_;
	croak "Missing SIM ICCID" if(!$simid);
	$self->_assertToken();
	$self->_debug("Obtaining validation information about SIM ICCID $simid...\n");
	my $response = $self->_get("/sims/$simid/validate");
	if($response->{'success'} && $response->{'code'} == 204) { # No content
		return ();
	}
	if(!$response->{'body'}) {
		warn $response->{'error'} . "\n";
		return;
	}
	return @{decode_json($response->{'body'})};
}

=head2 getSimsByOwnerId (accountId)

=cut

sub getSimsByOwnerId {
	my ($self, $accountid) = @_;
	croak "Missing account ID" if(!$accountid);
	$self->_assertToken();
	$self->_debug("Obtaining SIMs from owner ID $accountid...\n");
	return @{$self->_get_json("/accounts/$accountid/sims")};
}

=head2 getSim (simId)

{'fraction_non_alphanumeric': 0.21166384931176044, 'fraction_numerical': 0.009176527408838444, 'mean_word_length': 4.589068825910931, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 131, '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': '1411974', 'n_tokens_mistral': 3236, 'n_tokens_neox': 3124, 'n_words': 724}