File Coverage

blib/lib/WWW/DNSMadeEasy.pm
Criterion Covered Total %
statement 87 103 84.4
branch 14 24 58.3
condition n/a
subroutine 24 28 85.7
pod 6 14 42.8
total 131 169 77.5


line stmt bran cond sub pod time code
1             package WWW::DNSMadeEasy;
2             our $VERSION = '0.100';
3             our $AUTHORITY = 'cpan:GETTY';
4             # ABSTRACT: Accessing DNSMadeEasy API
5              
6 8     8   1133499 use feature qw/say/;
  8         16  
  8         1105  
7              
8 8     8   3811 use Moo;
  8         58851  
  8         39  
9 8     8   19006 use DateTime;
  8         5253357  
  8         508  
10 8     8   5401 use DateTime::Format::HTTP;
  8         67737  
  8         458  
11 8     8   5761 use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
  8         46688  
  8         592  
12 8     8   6467 use LWP::UserAgent;
  8         463447  
  8         466  
13 8     8   79 use HTTP::Request;
  8         18  
  8         245  
14 8     8   6129 use JSON;
  8         97541  
  8         51  
15              
16 8     8   5298 use WWW::DNSMadeEasy::Domain;
  8         41  
  8         356  
17 8     8   5113 use WWW::DNSMadeEasy::ManagedDomain;
  8         43  
  8         431  
18 8     8   5117 use WWW::DNSMadeEasy::Response;
  8         35  
  8         5695  
19              
20             has api_key => (is => 'ro', required => 1);
21             has secret => (is => 'ro', required => 1);
22             has sandbox => (is => 'ro', default => sub { 0 });
23             has last_response => (is => 'rw');
24             has _http_agent => (is => 'lazy');
25             has http_agent_name => (is => 'lazy');
26             has api_version => (
27             isa => sub {
28             $_ && (
29             $_ eq '1.2' or
30             $_ eq '2.0'
31             )
32             },
33             is => 'ro',
34             default => sub { '2.0' },
35             );
36              
37             sub _build__http_agent {
38 0     0   0 my $self = shift;
39 0         0 my $ua = LWP::UserAgent->new;
40 0         0 $ua->agent($self->http_agent_name);
41 0         0 return $ua;
42             }
43              
44 0     0   0 sub _build_http_agent_name { __PACKAGE__.'/'.$VERSION }
45              
46             sub api_endpoint {
47 23     23 0 7569 my ( $self ) = @_;
48 23 100       127 if ($self->sandbox) {
49 3         28 return 'https://api.sandbox.dnsmadeeasy.com/V'.$self->api_version.'/';
50             } else {
51 20         115 return 'https://api.dnsmadeeasy.com/V'.$self->api_version.'/';
52             }
53             }
54              
55             sub get_request_headers {
56 21     21 0 3807 my ( $self, $dt ) = @_;
57 21 100       232 $dt = DateTime->now->set_time_zone( 'GMT' ) if !$dt;
58 21         13926 my $date_string = DateTime::Format::HTTP->format_datetime($dt);
59             return {
60 21         9415 'x-dnsme-requestDate' => $date_string,
61             'x-dnsme-apiKey' => $self->api_key,
62             'x-dnsme-hmac' => hmac_sha1_hex($date_string, $self->secret),
63             };
64             }
65              
66             sub request {
67 19     19 0 463 my ( $self, $method, $path, $data ) = @_;
68 19         90 my $url = $self->api_endpoint.$path;
69 19 50       87 say "$method $url" if $ENV{WWW_DME_DEBUG};
70 19         208 my $request = HTTP::Request->new( $method => $url );
71 19         40629 my $headers = $self->get_request_headers;
72 19         782 $request->header($_ => $headers->{$_}) for (keys %{$headers});
  19         191  
73 19         5302 $request->header('Accept' => 'application/json');
74 19 100       988 if (defined $data) {
75 2         6 $request->header('Content-Type' => 'application/json');
76 2         127 $request->content(encode_json($data));
77 8 50   8   4308 use DDP; p $data if $ENV{WWW_DME_DEBUG};
  8         338445  
  8         83  
  2         36  
78             }
79 19         612 my $res = $self->_http_agent->request($request);
80 19         21711 $res = WWW::DNSMadeEasy::Response->new( http_response => $res );
81 19 50       5187 say $res->content if $ENV{WWW_DME_DEBUG};
82 19         170 $self->last_response($res);
83 19 50       402 die ' HTTP request failed: ' . $res->status_line . "\n" unless $res->is_success;
84 19         1084 return $res;
85             }
86              
87             sub requests_remaining {
88 1     1 0 10 my ( $self ) = @_;
89 1 50       13 return $self->last_response ? $self->last_response->requests_remaining : undef;
90             }
91              
92             sub last_request_id {
93 1     1 0 692 my ( $self ) = @_;
94 1 50       15 return $self->last_response ? $self->last_response->request_id : undef;
95             }
96              
97             sub request_limit {
98 1     1 0 770 my ( $self ) = @_;
99 1 50       8 return $self->last_response ? $self->last_response->request_limit : undef;
100             }
101              
102             #
103             # V1 DOMAINS (TODO - move this into a role)
104             #
105              
106 5     5 0 30 sub path_domains { 'domains' }
107              
108             sub create_domain {
109 0     0 1 0 my ( $self, $domain_name ) = @_;
110              
111 0         0 my $params = { dme => $self };
112 0 0       0 if (ref $domain_name eq 'HASH') {
113 0         0 $params->{obj} = $domain_name;
114 0         0 $params->{name} = $domain_name->{name}; # name is required
115             } else {
116 0         0 $params->{name} = $domain_name;
117             }
118              
119 0         0 return WWW::DNSMadeEasy::Domain->create($params);
120             }
121              
122             sub domain {
123 5     5 1 17060 my ( $self, $domain_name ) = @_;
124 5         134 return WWW::DNSMadeEasy::Domain->new({
125             name => $domain_name,
126             dme => $self,
127             });
128             }
129              
130             sub all_domains {
131 1     1 1 1975 my ( $self ) = @_;
132 1         18 my $data = $self->request('GET',$self->path_domains)->data;
133 1 50       15 return if !$data->{list};
134 1         2 my @domains;
135 1         1 for (@{$data->{list}}) {
  1         3  
136 2         1052 push @domains, WWW::DNSMadeEasy::Domain->new({
137             dme => $self,
138             name => $_,
139             });
140             }
141 1         18 return @domains;
142             }
143              
144             #
145             # V2 Managed domains (TODO - move this into a role)
146             #
147              
148 14     14 0 616 sub domain_path {'dns/managed/'}
149              
150             sub create_managed_domain {
151 0     0 1 0 my ($self, $name) = @_;
152 0         0 my $data = {name => $name};
153 0         0 my $response = $self->request(POST => $self->domain_path, $data);
154             return WWW::DNSMadeEasy::ManagedDomain->new(
155             dme => $self,
156             name => $response->as_hashref->{name},
157 0         0 as_hashref => $response->as_hashref,
158             );
159             }
160              
161             sub get_managed_domain {
162 6     6 1 39527 my ($self, $name) = @_;
163 6         247 return WWW::DNSMadeEasy::ManagedDomain->new(
164             name => $name,
165             dme => $self,
166             );
167             }
168              
169             sub managed_domains {
170 3     3 1 11216 my ($self) = @_;
171 3         19 my $data = $self->request(GET => $self->domain_path)->as_hashref->{data};
172              
173 3         118 my @domains;
174             push @domains, WWW::DNSMadeEasy::ManagedDomain->new({
175             dme => $self,
176             name => $_->{name},
177 3         68 }) for @$data;
178              
179 3         3276 return @domains;
180             }
181              
182              
183             1;
184              
185             __END__
186              
187             =pod
188              
189             =encoding UTF-8
190              
191             =head1 NAME
192              
193             WWW::DNSMadeEasy - Accessing DNSMadeEasy API
194              
195             =head1 VERSION
196              
197             version 0.100
198              
199             =head1 SYNOPSIS
200              
201             use WWW::DNSMadeEasy; # or WWW::DME as shortname
202            
203             # v2 api examples
204             my $dme = WWW::DNSMadeEasy->new({
205             api_key => '1c1a3c91-4770-4ce7-96f4-54c0eb0e457a',
206             secret => 'c9b5625f-9834-4ff8-baba-4ed5f32cae55',
207             sandbox => 1, # defaults to 0
208             api_version => '2.0', # defaults to '1.0'
209             });
210             my @managed_domains = $dme->managed_domains;
211             my $managed_domain = $dme->get_managed_domain('example.com');
212             my @records = $domain->records;
213             my $record = $domain->create_record(
214             ttl => 120,
215             gtd_location => 'DEFAULT',
216             name => 'www',
217             data => '1.2.3.4',
218             type => 'A',
219             );
220             $record->delete;
221             $domain->delete;
222            
223             # v1 api examples
224             my $dme = WWW::DNSMadeEasy->new({
225             api_key => '1c1a3c91-4770-4ce7-96f4-54c0eb0e457a',
226             secret => 'c9b5625f-9834-4ff8-baba-4ed5f32cae55',
227             sandbox => 1, # defaults to 0
228             });
229             my @domains = $dme->all_domains;
230             my $domain = $dme->create_domain('example.com');
231             my @records = $domain->all_records;
232             my $record = $domain->create_record({
233             ttl => 120,
234             gtdLocation => 'DEFAULT',
235             name => 'www',
236             data => '1.2.3.4',
237             type => 'A',
238             });
239             $record->delete;
240             $domain->delete;
241              
242             =head1 DESCRIPTION
243              
244             This distribution gives you easy access to the DNSMadeEasy API. You require a business or corporate account to use this. You can't use it with the free test account neither with the small business account. This module doesnt check any input values, I suggest so far that you know what you do.
245              
246             =head1 ATTRIBUTES
247              
248             =head2 api_key
249              
250             The API key which you can obtain from this page L<https://cp.dnsmadeeasy.com/account/info>.
251              
252             =head2 secret
253              
254             The secret can be found on the same page as the API key.
255              
256             =head2 sandbox
257              
258             If set to true, this will activate the usage of the sandbox, instead of the live system.
259              
260             =head2 http_agent_name
261              
262             Here you can set a different http useragent for the requests, it defaults to the package name including the distribution version.
263              
264             =head1 METHODS
265              
266             =head2 create_managed_domain($name)
267              
268             Creates the domain $name and returns a L<WWW::DNSMadeEasy::ManagedDomain> object.
269              
270             =head2 get_managed_domain($name)
271              
272             Searches for a domain with the name $name and returns a L<WWWW::DNSMadeEasy::ManagedDomain> object.
273              
274             =head2 managed_domains()
275              
276             Returns a list of L<WWWW::DNSMadeEasy::ManagedDomain> objects representing all domains.
277              
278             =head2 $obj->create_domain
279              
280             Arguments: $name
281              
282             Return value: L<WWW::DNSMadeEasy::Domain>
283              
284             Will be creating the domain $name on your account and returns the L<WWW::DNSMadeEasy::Domain> for this domain.
285              
286             =head2 $obj->domain
287              
288             Arguments: $name
289              
290             Return value: L<WWW::DNSMadeEasy::Domain>
291              
292             Returns the L<WWW::DNSMadeEasy::Domain> of the domain with name $name.
293              
294             =head2 $obj->all_domains
295              
296             Arguments: None
297              
298             Return value: Array of L<WWW::DNSMadeEasy::Domain>
299              
300             Returns an array of L<WWW::DNSMadeEasy::Domain> objects of all domains listed on this account.
301              
302             =head1 METHODS FOR API V2
303              
304             =head1 METHODS FOR API V1
305              
306             =head1 SEE ALSO
307              
308             DNSMadeEasy REST API landing page
309             http://www.dnsmadeeasy.com/services/rest-api/
310              
311             DNSMadeEasy documentation for v2.0
312             http://www.dnsmadeeasy.com/wp-content/uploads/2014/07/API-Docv2.pdf
313              
314             DNSMadeEasy documentation for v1.2
315             http://www.dnsmadeeasy.com/wp-content/themes/appdev/pdf/API-Documentation-DM_20110921.pdf
316              
317             =head1 SUPPORT
318              
319             IRC
320              
321             Join #duckduckgo on irc.freenode.net and highlight Getty or /msg me.
322              
323             Repository
324              
325             http://github.com/Getty/p5-www-dnsmadeeasy
326             Pull request and additional contributors are welcome
327              
328             Issue Tracker
329              
330             http://github.com/Getty/p5-www-dnsmadeeasy/issues
331              
332             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
333              
334             =head1 SUPPORT
335              
336             =head2 Source Code
337              
338             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
339             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
340             from your repository :)
341              
342             L<https://github.com/Getty/p5-www-dnsmadeeasy>
343              
344             git clone https://github.com/Getty/p5-www-dnsmadeeasy.git
345              
346             =head1 AUTHOR
347              
348             Torsten Raudssus <torsten@raudssus.de>
349              
350             =head1 COPYRIGHT AND LICENSE
351              
352             This software is copyright (c) 2012 by L<Torsten Raudssus|https://raudssus.de/>.
353              
354             This is free software; you can redistribute it and/or modify it under
355             the same terms as the Perl 5 programming language system itself.
356              
357             =cut