#!/usr/bin/perl

use DBI;
use strict;
use POSIX;
#use ESL;
use Switch;
use Time::HiRes;
use WWW::Curl::Easy;
use JSON;
use Config::Tiny;
use Data::Dumper;
use Cwd;
use URI::Encode qw(uri_encode uri_decode);
use threads;
use Thread::Semaphore;
use threads::shared;
use POSIX qw(strftime);

#dig -t naptr @NodeIP Phone_number_in_enum_format.e164enum.tmtvelocity.com


   Query_ANI( "541138429040","apikey","apisecret" );

sub Query_ANI {

	my $number = $_[0];
	my $apikey = $_[1];
	my $apisecret = $_[2];

	my $url = sprintf( "https://api.tmtvelocity.com/standard/json/%s/%s/%s", $apikey, $apisecret, $number ); 
   printf( "%s\n", $url );

   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
#  $curl->setopt( CURLOPT_POSTFIELDS, $jsonEncoded );
#  $curl->setopt( CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
#  $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
#  $curl->setopt( CURLOPT_USERPWD, $auth);
   
   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";
	
   if ($retcode == 0) {
	   my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
         printf( "%s\n", $response_body );
        	my $resultSend = decode_json($response_body);
			return $resultSend;
     	}
      else {
         printf( "retcode: %d\n", $response_code );
      }
   }
   else {
      printf( "retcode: %d\n", $retcode );
   }
	return "";
}


