move api functions to modules, add switches

parent b74b91bc
package Account;
use strict;
use warnings;
use lib "..";
use Request;
use JSON;
sub Info {
my ($succ, $req) = Request::api_request("v1/account/info", "GET", 1, "");
if ($succ) {
my %data = %{decode_json($req)};
my $bal_avail = sprintf "%.2f", ($data{"balance"} + $data{"pending_charges"}) * -1;
my $bal = sprintf "%.2f", $data{"balance"} * -1;
my $charges = sprintf "%.2f", $data{"pending_charges"};
my $last_charges = sprintf "%.2f", $data{"last_payment_amount"} * -1;
print "Balance: \$$bal (\$$bal_avail available)\n";
print "Next billing charges: \$$charges\n";
print "Last payment: \$$last_charges at $data{'last_payment_date'}.\n";
}
}
1;
package App;
use strict;
use warnings;
use lib "..";
use Request;
use JSON;
sub List {
my ($succ, $req) = Request::api_request("v1/app/list", "GET", 0, "");
if ($succ) {
my %data = %{decode_json($req)};
print "Available applications:\n";
foreach my $app (keys %data) {
my $surcharge = sprintf '%.2f', $data{$app}{surcharge};
printf "[\$%6s] %-24s: %-64s\n", $surcharge, "$data{$app}{name} ($data{$app}{short_name})", $data{$app}{deploy_name}
}
}
}
1;
package Request;
use LWP::UserAgent;
my $VULTR_API_URL = "https://api.vultr.com/";
my %API_RESP_CODES = ( 400 => "An Invalid API URL was specified. Check the URL and try again.",
403 => "An Invalid API key was supplied. Make sure the API key is present and correct.",
405 => "The wrong method was used for this function. Entire that the correct method is used (GET/POST).",
412 => "Request failed for some reason. Check the response body for more information.",
500 => "An unknown internal server error occured. Try again later.",
503 => "Excessive requests (2/s) has been detected. Slow down and try again later." );
sub api_request {
my ($api, $request_type, $auth_required, $request_data) = @_;
my $ua = LWP::UserAgent->new;
my $api_request = HTTP::Request->new($request_type => $VULTR_API_URL . $api);
if ($auth_required) {
$api_request->header("API-Key" => $main::API_KEY);
}
$api_request->content($request_data);
my $api_response = $ua->request($api_request);
if ($api_response->is_success) {
return (1, $api_response->decoded_content);
} else {
print "Error: ", $api_response->code, " ",$api_response->message, "\n";
print "$API_RESP_CODES{$api_response->code}\n";
return (0, $api_response->decoded_content);
}
}
1;
......@@ -3,52 +3,63 @@
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use Getopt::Long;
use File::Basename;
my $VULTR_API_URL = "https://api.vultr.com/";
my $API_KEY = "API_KEY_HERE";
use API::Account;
use API::App;
my %API_RESP_CODES = ( 400 => "An Invalid API URL was specified. Check the URL and try again.",
403 => "An Invalid API key was supplied. Make sure the API key is present and correct.",
405 => "The wrong method was used for this function. Entire that the correct method is used (GET/POST).",
412 => "Request failed for some reason. Check the response body for more information.",
500 => "An unknown internal server error occured. Try again later.",
503 => "Excessive requests (2/s) has been detected. Slow down and try again later." );
our $API_KEY;
sub debug {
my $DEBUG = $ENV{"DEBUG"};
if (defined $DEBUG and $DEBUG eq "1")
{ return 1; } else { return 0; }
}
my $KEY_FROM_ENV = $ENV{"API_KEY"};
if (defined $KEY_FROM_ENV and $KEY_FROM_ENV ne "") {
$API_KEY = $KEY_FROM_ENV;
print "API key from env: $API_KEY\n";
if (debug) {print "API key from env: $API_KEY\n";}
} else {
if (debug) {print "API key from source: $API_KEY\n";}
}
else {
print "API key from source: $API_KEY\n";
sub usage {
my $prog_name = basename($0);
print("Usage: $prog_name [--help] [options]\n");
}
sub vultr_api_request {
my ($api, $request_type, $auth_required, $request_data) = @_;
my $ua = LWP::UserAgent->new;
my $api_request = HTTP::Request->new($request_type => $VULTR_API_URL . $api);
if ($auth_required) {
$api_request->header("API-Key" => $API_KEY);
}
$api_request->content($request_data);
my $api_response = $ua->request($api_request);
if ($api_response->is_success) {
return $api_response->decoded_content;
} else {
print "Error: ", $api_response->code, " ",$api_response->message, "\n";
print "$API_RESP_CODES{$api_response->code}\n";
return 0;
}
sub help {
usage();
print <<EOF;
Options with a star (*) require an api key.
You can set an api key by exporting API_KEY=<key> as an enviroment variable.
--account-info * - Show account information and balance.
--app-list - Show a list of available applications.
EOF
}
my $req = vultr_api_request("v1/account/info", "GET", 1, "");
my $arg_account_info;
my $arg_app_list;
my $arg_help;
GetOptions( "account-info" => \$arg_account_info,
"app-list" => \$arg_app_list,
"help" => \$arg_help )
or die("Error in command line arguments.\n");
if (!defined $KEY_FROM_ENV or $KEY_FROM_ENV eq "") {
print("Warning: No API key was specified. Many operations will fail without one!\n");
}
if ($req) {
my %data = %{decode_json($req)};
my $bal = ($data{"balance"} + $data{"pending_charges"}) * -1;
print "Balance: $bal\n";
if ($arg_account_info) {
Account::Info();
} elsif ($arg_app_list) {
App::List();
} elsif ($arg_help) {
help();
} else {
usage();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment