| 1 | package SelectPdf::UsageClient; | ||
| 2 | |||
| 3 | use JSON; | ||
| 4 | use SelectPdf::ApiClient; | ||
| 5 | use strict; | ||
| 6 | our @ISA = qw(SelectPdf::ApiClient); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | SelectPdf::UsageClient - Get usage details for SelectPdf Online API. | ||
| 11 | |||
| 12 | =head1 METHODS | ||
| 13 | |||
| 14 | =head2 new( $apiKey ) | ||
| 15 | |||
| 16 | Construct the Usage client. | ||
| 17 | |||
| 18 | my $client = SelectPdf::UsageClient->new($apiKey); | ||
| 19 | |||
| 20 | Parameters: | ||
| 21 | |||
| 22 | - $apiKey API Key. | ||
| 23 | =cut | ||
| 24 | sub new { | ||
| 25 | my $type = shift; | ||
| 26 | my $self = $type->SUPER::new; | ||
| 27 | |||
| 28 | # API endpoint | ||
| 29 | $self->{apiEndpoint} = "https://selectpdf.com/api2/usage/"; | ||
| 30 | |||
| 31 | $self->{parameters}{"key"} = shift; | ||
| 32 | |||
| 33 | bless $self, $type; | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | =head2 getUsage( $getHistory ) | ||
| 38 | |||
| 39 | Get API usage information with history, if specified. | ||
| 40 | |||
| 41 | my $client = SelectPdf::UsageClient->new($apiKey); | ||
| 42 | $usageInfo = $client->getUsage($getHistory); | ||
| 43 | print("Conversions remained this month: ". $usageInfo->{"available"}); | ||
| 44 | |||
| 45 | Parameters: | ||
| 46 | |||
| 47 | - $getHistory Get history or not. | ||
| 48 | |||
| 49 | Returns: | ||
| 50 | |||
| 51 | - Usage information. | ||
| 52 | =cut | ||
| 53 | sub getUsage($) { | ||
| 54 | my($self, $getHistory) = @_; | ||
| 55 | |||
| 56 | $self->{headers}{"Accept"} = "text/json"; | ||
| 57 | |||
| 58 | if ($getHistory) { | ||
| 59 | $self->{parameters}{"get_history"} = "True"; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $result = $self->SUPER::performPost(); | ||
| 63 | |||
| 64 | if ($result) { | ||
| 65 | return decode_json($result); | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | return {}; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; |