| 1 | # | ||
| 2 | # This file is part of Net-Gandi | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2012 by Natal Ngétal. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | package Net::Gandi::Client; | ||
| 10 | { | ||
| 11 | $Net::Gandi::Client::VERSION = '1.122180'; | ||
| 12 | } | ||
| 13 | |||
| 14 | # ABSTRACT: A Perl interface for gandi api | ||
| 15 | |||
| 16 | use Moose; | ||
| 17 | use MooseX::Types::URI qw(Uri); | ||
| 18 | use namespace::autoclean; | ||
| 19 | |||
| 20 | use Net::Gandi::Types qw(Apikey); | ||
| 21 | |||
| 22 | use Module::Load; | ||
| 23 | |||
| 24 | with 'MooseX::Traits'; | ||
| 25 | |||
| 26 | |||
| 27 | has 'apikey' => ( | ||
| 28 | is => 'rw', | ||
| 29 | required => 0, | ||
| 30 | isa => Apikey, | ||
| 31 | ); | ||
| 32 | |||
| 33 | |||
| 34 | has 'apiurl' => ( | ||
| 35 | is => 'rw', | ||
| 36 | isa => Uri, | ||
| 37 | coerce => 1, | ||
| 38 | default => 'https://rpc.gandi.net/xmlrpc/2.0/', | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | has 'useragent' => ( | ||
| 43 | is => 'rw', | ||
| 44 | isa => 'Str', | ||
| 45 | default => "Net::Gandi/1.0", | ||
| 46 | ); | ||
| 47 | |||
| 48 | |||
| 49 | has 'timeout' => ( | ||
| 50 | is => 'rw', | ||
| 51 | isa => 'Int', | ||
| 52 | default => 5, | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | has 'err' => ( | ||
| 57 | is => 'rw', | ||
| 58 | isa => 'Int', | ||
| 59 | ); | ||
| 60 | |||
| 61 | |||
| 62 | has 'errstr' => ( | ||
| 63 | is => 'rw', | ||
| 64 | isa => 'Str', | ||
| 65 | ); | ||
| 66 | |||
| 67 | |||
| 68 | has 'date_to_datetime' => ( | ||
| 69 | is => 'rw', | ||
| 70 | default => 0, | ||
| 71 | isa => 'Bool', | ||
| 72 | ); | ||
| 73 | |||
| 74 | sub _date_to_datetime { | ||
| 75 | my ( $self, $object ) = @_; | ||
| 76 | |||
| 77 | ref($object) or return $object; | ||
| 78 | |||
| 79 | load 'DateTime::Format::HTTP'; | ||
| 80 | my $array = ref($object) ne 'ARRAY' ? [ $object ] : $object; | ||
| 81 | my $dt = 'DateTime::Format::HTTP'; | ||
| 82 | my @special_keys = ('ips', 'disks', 'ifaces'); | ||
| 83 | |||
| 84 | foreach my $obj (@{$array}) { | ||
| 85 | while ( my ($key, $value) = each %{$obj} ) { | ||
| 86 | if ( $key ~~ @special_keys ) { | ||
| 87 | $self->_date_to_datetime($value); | ||
| 88 | } | ||
| 89 | $obj->{$key} = $dt->parse_datetime($value) if $key =~ m/date_/; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | return $object; | ||
| 94 | } | ||
| 95 | |||
| 96 | no Moose; | ||
| 97 | __PACKAGE__->meta->make_immutable; | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ |