WWW-KlickTel-API

 view release on metacpan or  search on metacpan

lib/WWW/KlickTel/API.pm  view on Meta::CPAN

package WWW::KlickTel::API;

use 5.008001;  # perl 5.8.1
use strict;
use warnings FATAL => 'all';
use Carp qw(croak carp);
use feature 'say';

use REST::Client;
use JSON::XS;
use DB_File;
use Fcntl;

=head1 NAME

WWW::KlickTel::API - A module to use openapi.klicktel.de (Linux only)

=head1 VERSION

Version $Revision: 34 $

$Id: API.pm 34 2013-03-14 14:51:02Z sysdef $

=cut

our ($VERSION) = ( q$Revision: 34 $ =~ /(\d+)/ );

=head1 SYNOPSIS

This module provides a basic access to the KlickTel API
http://openapi.klicktel.de

NOTE: This POC version supports reverse lookups only.

Get an API key at http://openapi.klicktel.de/login/register

  #!/usr/bin/perl
  use strict;
  use warnings;
  use WWW::KlickTel::API;

  my $klicktel = WWW::KlickTel::API->new(
      api_key       => '1234567890123456789013456789012',
  );

  #     -OR-
  # create a key file at ~/.klicktel/api_key.txt and run

  my $klicktel = WWW::KlickTel::API->new();

=cut

# --- GLOBAL VARIABLES ---
my %cache_invers = ();

# system username
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);

=head1 METHODS

=head2 new

Create the object. All parameter are optional.

  my $klicktel = WWW::KlickTel::API->new(
      api_key     => '01234567890abcdef01234567890abcd',
      protocol    => 'https',           # or 'http' (http is default)
      cache_path  => '/var/cache/www-klicktel-api/',
      uri_invers  => 'openapi.klicktel.de/searchapi/invers',
      timeout     => 10,                # ( 1- 600 seconds)
      ca_file     => '/path/to/ca.file',
      client_auth => {
          'cert'      => '/path/to/ssl.crt',
          'key'       => '/path/to/ssl.key',
      },
      proxy_url       => 'http://proxy.example.com',
  );

=cut

sub new {
    my $class = shift;
    croak 'Odd number of elements passed when even was expected' if @_ % 2;
    my %args = @_;

    my $self = {
        PROTOCOL     => $args{protocol}
            || 'http',
        CACHE_PATH   => $args{'cache_path'}
            || '/var/cache/www-klicktel-api/',
        URI_INVERS   => $args{'uri_invers'}
            || 'openapi.klicktel.de/searchapi/invers',
        REST_TIMEOUT => $args{'timeout'}
            || 10,
        REST_CA_FILE => $args{'ca_file'}
            || q{},
        CLIENT_CERT  => $args{'client_auth'}{'cert'}
            || q{},
        CLIENT_KEY   => $args{'client_auth'}{'key'}
            || q{},
        PROXY_URL    => $args{'proxy_url'}
            || q{},
    };

    $self->{API_KEY} = $args{'api_key'};
    if ( !$self->{API_KEY} ) {

        # checking for user's API Key
        $self->{API_KEY_FILE} = '/home/' . $username . '/.klicktel/api_key.txt';
        if ( -r $self->{API_KEY_FILE} ) {

            # loading user's api key
            my $api_key_fh;
            open $api_key_fh, "<", $self->{API_KEY_FILE};
            binmode $api_key_fh;
            read $api_key_fh, $self->{API_KEY}, 32;
        }



( run in 1.236 second using v1.01-cache-2.11-cpan-817d5f8af8b )