File Coverage

blib/lib/Regru/API.pm
Criterion Covered Total %
statement 41 41 100.0
branch 6 8 75.0
condition 2 3 66.6
subroutine 20 20 100.0
pod n/a
total 69 72 95.8


line stmt bran cond sub pod time code
1             package Regru::API;
2              
3             # ABSTRACT: Perl bindings for Reg.ru API v2
4              
5 11     11   733275 use strict;
  11         117  
  11         303  
6 11     11   53 use warnings;
  11         20  
  11         262  
7 11     11   5773 use Moo;
  11         121607  
  11         53  
8 11     11   15503 use Carp ();
  11         27  
  11         230  
9 11     11   4862 use Class::Load qw(try_load_class);
  11         158206  
  11         666  
10 11     11   4817 use namespace::autoclean;
  11         104359  
  11         42  
11              
12             our $VERSION = '0.051'; # VERSION
13             our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY
14              
15             with 'Regru::API::Role::Client';
16              
17 11     11   102 sub available_methods {[qw(
18             nop
19             reseller_nop
20             get_user_id
21             get_service_id
22             )]}
23              
24 11     11   77 sub available_namespaces {[qw(
25             user
26             domain
27             zone
28             dnssec
29             bill
30             folder
31             service
32             hosting
33             shop
34             )]}
35              
36             sub _get_namespace_handler {
37 16     16   40 my $self = shift;
38 16         36 my $namespace = shift;
39              
40 16 50       71 unless ( $self->{_handlers}->{$namespace} ) {
41 16 100       107 my $ns = 'Regru::API::' . ( $namespace eq 'dnssec' ? uc($namespace) : ucfirst($namespace) );
42              
43 16 50       82 try_load_class $ns or Carp::croak 'Unable to load namespace: ' . $ns;
44              
45 16         553 my %params;
46              
47 16         64 foreach my $opt (qw(username password io_encoding lang debug)) {
48             # predicate
49 80         157 my $has = 'has_' . $opt;
50              
51             # pass option if it exists
52 80 100 66     514 $params{$opt} = $self->$opt if $self->can($has) && $self->$has;
53             }
54              
55 16         211 $self->{_handlers}->{$namespace} = $ns->new(@_, %params);
56             }
57              
58 16         199 return $self->{_handlers}->{$namespace};
59             }
60              
61             sub namespace_handlers {
62 11     11   35 my $class = shift;
63              
64 11         72 my $meta = $class->meta;
65              
66 11         10349 foreach my $namespace ( @{ $class->available_namespaces } ) {
  11         74  
67 99         5590269 $namespace = lc $namespace;
68 99         234 $namespace =~ s/\s/_/g;
69              
70             my $handler = sub {
71 16     16   207 my ($self, @args) = @_;
        16      
        16      
        16      
        16      
        16      
        16      
        16      
        16      
        16      
72 16         65 $self->_get_namespace_handler($namespace => @args);
73 99         375 };
74              
75 99         519 $meta->add_method($namespace => $handler);
76             }
77             }
78              
79             __PACKAGE__->namespace_handlers;
80             __PACKAGE__->namespace_methods;
81             __PACKAGE__->meta->make_immutable;
82              
83             1; # End of Regru::API
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             Regru::API - Perl bindings for Reg.ru API v2
94              
95             =head1 VERSION
96              
97             version 0.051
98              
99             =head1 SYNOPSIS
100              
101             my $client = Regru::API->new(
102             username => 'test',
103             password => 'test',
104             );
105              
106             # trivial API request
107             my $resp = $client->nop;
108              
109             if ($resp->is_success) {
110             print $resp->get('user_id');
111             }
112             else {
113             print "Error code: " . $resp->error_code . ", Error text: " . $resp->error_text;
114             }
115              
116             =head1 DESCRIPTION
117              
118             Regru::API implements simplified access to the REG.API v2 provided by REG.RU LLC. This is a JSON-driven implementation.
119             Input/output request data will transforms from/to JSON transparently.
120              
121             =head2 Rate limiting
122              
123             Rate limiting in version 2 of the REG.API is considered on a per-user and per-ip basic. The REG.API methods have not
124             divided into groups by limit level. There is no difference between them. At the moment REG.API v2 allows to execute
125             C<1200> requests per-user and per-ip within C<1 hour> window. Both limits are acting at the same time.
126             If the limits has exceeded then REG.API sets the error code (depends on kind of) to C<IP_EXCEEDED_ALLOWED_CONNECTION_RATE> or
127             C<ACCOUNT_EXCEEDED_ALLOWED_CONNECTION_RATE> which might be checked via attribute L<error_code|Regru::API::Response/error_code>.
128              
129             The following tips are there might helps to reduce the possibility of being rate limited:
130              
131             =over
132              
133             =item B<Caching>
134              
135             Store all domain name or service related data locally and use the REG.API in cases you want to change some data in
136             the registry (e.g. contact data, DNS servers, etc).
137              
138             =item B<Bulk requests>
139              
140             Group similar items and execute a bulk API request. A bunch of methods supports sending request for the list of items at
141             the same time (e.g. multiple domain names). Check the details at
142             L<REG.API Service list identification parameters|https://www.reg.com/support/help/api2#common_service_list_identification_params>.
143              
144             =item B<Journaling>
145              
146             Keep the logs of interactions with REG.API (requests and responses). This will helps quickly resolve the issues
147             instead of sending additional requests to find out what's happened.
148              
149             =back
150              
151             =head2 Categories (namespaces)
152              
153             REG.API methods are divided into categories (namespaces). When you wish to make an API request to some REG.API method,
154             that belongs to some namespace (category) you should get a namespace handler (defined as trivial client's method):
155              
156             # suppose we already have a client
157             $client->user->nop;
158              
159             # or like this
160             $zone = $client->zone;
161             $zone->register_ns(...);
162              
163             At the moment there are the following namespaces:
164              
165             =over
166              
167             =item B<root>
168              
169             General purpose methods such as L</nop>, L</reseller_nop> etc which are described below. Actually is a virtual namespace
170             defined by client. No needs to get namespace handler. The methods of this C<namespace> are available as client's methods
171             directly.
172              
173             $client->nop;
174             $client->reseller_nop;
175              
176             See L</"REG.API METHODS">.
177              
178             =item B<user>
179              
180             User account management methods.
181              
182             # suppose we already have a client
183             $client->user->nop;
184              
185             See L<Regru::API::User> for details and
186             L<REG.API Account management functions|https://www.reg.com/support/help/api2#user_functions>.
187              
188             =item B<domain>
189              
190             Domain names management methods.
191              
192             # suppose we already have a client
193             $client->domain->get_nss(
194             domain_name => 'gallifrey.ru',
195             );
196              
197             See L<Regru::API::Domain> for details and
198             L<REG.API Domain management functions|https://www.reg.com/support/help/api2#domain_functions>.
199              
200             =item B<zone>
201              
202             DNS resource records management methods.
203              
204             # suppose we already have a client
205             $client->zone->clear(
206             domain_name => 'pyrovilia.net',
207             );
208              
209             See L<Regru::API::Zone> for details and
210             L<REG.API DNS management functions|https://www.reg.com/support/help/api2#zone_functions>.
211              
212             =item B<dnssec>
213              
214             DNSSEC management methods.
215              
216             # suppose we already have a client
217             $client->dnssec->enable(
218             domain_name => 'tvilgo.com',
219             );
220              
221             See L<Regru::API::DNSSEC> for details and
222             L<REG.API DNSSEC management functions|https://www.reg.com/support/help/api2#dnssec_functions>.
223              
224             =item B<service>
225              
226             Service management methods.
227              
228             # suppose we already have a client
229             $client->service->delete(
230             domain_name => 'sontar.com',
231             servtype => 'srv_hosting_plesk',
232             );
233              
234             See L<Regru::API::Service> for details and
235             L<REG.API Service management functions|https://www.reg.com/support/help/api2#service_functions>.
236              
237             =item B<folder>
238              
239             User folders management methods.
240              
241             # suppose we already have a client
242             $client->folder->create(
243             folder_name => 'UNIT',
244             );
245              
246             See L<Regru::API::Folder> for details and
247             L<REG.API Folder management functions|https://www.reg.com/support/help/api2#folder_functions>.
248              
249             =item B<bill>
250              
251             Invoice management methods.
252              
253             # suppose we already have a client
254             $client->bill->get_not_payed(
255             limit => 10,
256             );
257              
258             See L<Regru::API::Bill> for details and
259             L<REG.API Invoice management functions|https://www.reg.com/support/help/api2#bill_functions>.
260              
261             =item B<hosting>
262              
263             Hosting management methods.
264              
265             # suppose we already have a client
266             $client->hosting->set_jelastic_refill_url(
267             url => 'http://mysite.com?service_id=<service_id>&email=<email>'
268             );
269              
270             See L<Regru::API::Hosting> for details and
271             L<REG.API Hosting management functions|https://www.reg.com/support/help/api2#hosting_functions>.
272              
273             =item B<shop>
274              
275             Domain shop management methods.
276              
277             # suppose we already have a client
278             $client->shop->get_info();
279              
280             See L<Regru::API::Shop> for details and
281             L<REG.API Domain shop management functions|https://www.reg.com/support/help/api2#shop_functions>.
282              
283             =back
284              
285             =head2 Methods accessibility
286              
287             All REG.API methods can be divided into categories of accessibility. On manual pages of this distibution accessibility
288             marked by C<scope> tag. At the moment the following categories of accessibility present:
289              
290             =over
291              
292             =item B<everyone>
293              
294             All methods tagged by this one are accessible to all users. Those methods does not require authentication before call.
295              
296             =item B<clients>
297              
298             This tag indicates the methods which accessible only for users registered on L<reg.com|https://www.reg.com> website.
299             Strongly required an authenticated API request.
300              
301             =item B<partners>
302              
303             Group of methods which accessible only for partners (resellers) of the REG.RU LLC. Actually, partners (resellers)
304             able to execute all methods of the REG.API without any restrictions.
305              
306             =back
307              
308             =head2 Request parameters
309              
310             Each API request should contains a set of parameters. There are the following parameters:
311              
312             =over
313              
314             =item B<authentication parameters>
315              
316             These parameters are mandatory for the each method that requires authentication. This group of parameters includes
317             C<username> and C<password>. Both parameters should be passed to the L<constructor|/new> and their will be added
318             to API request.
319              
320             =item B<management parameters>
321              
322             This group include parameters defines input/output formats, encodings and language prefecence. Some parameters are fixed to
323             certains values, some might be set via passing values to the L<constructor|/new>: see C<io_encoding> and C<lang> options.
324              
325             =item B<service identification parameters>
326              
327             The group of parameters with aims to point to the particular service or group of services such as domain names,
328             folders, etc. Should be passed to an API request together with C<method specific parameters>.
329              
330             More info at
331             L<REG.API Service identification parameters|https://www.reg.com/support/help/api2#common_service_identification_params>
332              
333             =item B<method specific parameters>
334              
335             Parameters applicable to a particular API method. Very wide group. Strongly recommended to consult with REG.API documentation
336             for each method before perform an API request to it. The distribution's manual pages includes links to documentation
337             for each API method call. The main source for the method specific parameters available at
338             L<REG.API General description of functions|https://www.reg.com/support/help/api2#common_functions_description>.
339              
340             =back
341              
342             =head2 Response parameters
343              
344             Response parameters of the API request automatically handles by L<Regru::API::Response> module. There is no reasons to
345             do some addtional work on them. Each response may contains the following set of fileds:
346              
347             =over
348              
349             =item B<result>
350              
351             The result of API request. Either C<success> or C<error>. Can be accessed via attribute
352             L<is_success|Regru::API::Response/is_success> in boolean context.
353              
354             =item B<answer>
355              
356             The answer of API method call. May appear only when result of API request was successful. Can be accessed via attribute
357             L<answer|Regru::API::Response/answer>. Default value is C<{}> (empty HashRef). Gets assigned a default value if
358             result of API request was finished with error.
359              
360             =item B<error_code>
361              
362             The error code of API method call. May appear only when result of API request finished with error. Can be accessed via
363             attribute L<error_code|Regru::API::Response/error_code>.
364             See details at L<REG.API Common error codes|https://www.reg.com/support/help/api2#common_errors>.
365              
366             =item B<error_text>
367              
368             The short description of error. The language depends on option lang L</new> passed to constructor. May appear only when result
369             of API request finished with error. Can be accessed via attribute L<error_text|Regru::API::Response/error_text>.
370             See details at L<REG.API Common error codes|https://www.reg.com/support/help/api2#common_errors>.
371              
372             =item B<error_params>
373              
374             Additional parameters included to the error. May appear only when result of API request finished with error. Can be accessed
375             via attribute L<error_params|Regru::API::Response/error_params>.
376              
377             =back
378              
379             =head2 Access to REG.API in test mode
380              
381             REG.RU LLC provides an access to REG.API in test mode. For this, might be used a test account with C<username> and C<password>
382             equals to B<test>.
383              
384             my $client = Regru::API->new(username => 'test', password => 'test');
385             # we're in test mode now
386             $client->domain->get_prices;
387              
388             In the test mode REG.API engine (at server-side) handles API request: ensures necessary checks of input parameters,
389             produces response but actually does not perform any real actions/changes.
390              
391             Also, for debugging purposes REG.API provides a special set of methods allows to ensure the remote system for availability
392             without workload at minimal response time. Each namespace has method called B<nop> for that.
393              
394             =head1 METHODS
395              
396             =head2 new
397              
398             Creates a client instance to interract with REG.API.
399              
400             my $client = Regru::API->new(
401             username => 'Rassilon',
402             password => 'You die with me, Doctor!'
403             );
404              
405             my $resp = $client->user->get_balance;
406              
407             print $resp->get('prepay') if $resp->is_success;
408              
409             # another cool code...
410              
411             Available options:
412              
413             =over
414              
415             =item B<username>
416              
417             Account name of the user to access to L<reg.com|https://www.reg.com> website. Required. Should be passed at instance
418             create time. Although it might be changed at runtime.
419              
420             my $client = Regru::API->new(username => 'Cyberman', password => 'Exterminate!');
421             ...
422             # at runtime
423             $client->username('Dalek');
424              
425             =item B<password>
426              
427             Account password of the user to access to L<reg.com|https://www.reg.com> website or an alternative password for API
428             defined at L<Reseller settings|https://www.reg.com/reseller/details> page. Required. Should be passed at instance create time.
429             Although it might be changed at runtime.
430              
431             my $client = Regru::API->new(username => 'Master', password => 'Doctor');
432             ...
433             # at runtime
434             $client->password('The-Master.');
435              
436             =item B<io_encoding>
437              
438             Defines encoding that will be used for data exchange between the Service and the Client. At the moment REG.API v2
439             supports the following encodings: C<utf8>, C<cp1251>, C<koi8-r>, C<koi8-u>, C<cp866>. Optional. Default value is B<utf8>.
440              
441             my $client = Regru::API->new(..., io_encoding => 'cp1251');
442             ...
443             # or at runtime
444             $client->io_encoding('cp1251');
445              
446             my $resp = $client->user->create(
447             user_login => 'othertest',
448             user_password => '111',
449             user_email => 'test@test.ru',
450             user_first_name => $cp1251_encoded_name
451             );
452              
453             =item B<lang>
454              
455             Defines the language which will be used in error messages. At the moment REG.API v2 supports the following languages:
456             C<en> (English), C<ru> (Russian) and C<th> (Thai). Optional. Default value is B<en>.
457              
458             my $client = Regru::API->new(..., lang => 'ru');
459             ...
460             # or at runtime
461             $client->lang('ru');
462              
463             $client->username('bogus-user');
464             print $client->nop->error_text; # -> "Ошибка аутентификации по паролю"
465              
466             =item B<debug>
467              
468             A few messages will be printed to STDERR. Default value is B<0> (suppressed debug activity).
469              
470             my $client = Regru::API->new(..., debug => 1);
471             ...
472             # or at runtime
473             $client->debug(1);
474              
475             =back
476              
477             =head2 user
478              
479             Returns a handler to access to REG.API user account management methods. See L<Regru::API::User>.
480              
481             =head2 domain
482              
483             Returns a handler to access to REG.API domain name management methods. See L<Regru::API::Domain>.
484              
485             =head2 zone
486              
487             Returns a handler to access to REG.API DNS resource records management methods. See L<Regru::API::Zone>.
488              
489             =head2 dnssec
490              
491             Returns a handler to access to REG.API DNSSEC management methods. See L<Regru::API::DNSSEC>.
492              
493             =head2 service
494              
495             Returns a handler to access to REG.API service management methods. See L<Regru::API::Service>.
496              
497             =head2 folder
498              
499             Returns a handler to access to REG.API folder management methods. See L<Regru::API::Folder>.
500              
501             =head2 bill
502              
503             Returns a handler to access to REG.API invoice management methods. See L<Regru::API::Bill>.
504              
505             =head2 hosting
506              
507             Returns a handler to access to REG.API hosting management methods. See L<Regru::API::Hosting>.
508              
509             =head2 shop
510              
511             Returns a handler to access to REG.API domain shop management methods. See L<Regru::API::Shop>.
512              
513             =head2 namespace_handlers
514              
515             Creates shortcuts to REG.API categories (namespaces). Used internally.
516              
517             =head1 REG.API METHODS
518              
519             =head2 nop
520              
521             For testing purposes. Scope: B<everyone>. Typical usage:
522              
523             $resp = $client->nop;
524              
525             Answer will contains an user_id and login fields.
526              
527             More info at L<Common functions: nop|https://www.reg.com/support/help/api2#common_nop>.
528              
529             =head2 reseller_nop
530              
531             Similar to previous one but only for partners. Scope: B<partners>. Typical usage:
532              
533             $resp = $client->reseller_nop;
534              
535             Answer will contains an user_id and login fields.
536              
537             More info at L<Common functions: nop|https://www.reg.com/support/help/api2#common_reseller_nop>.
538              
539             =head2 get_user_id
540              
541             Get the identifier of the current user. Scope: B<clients>. Typical usage:
542              
543             $resp = $client->get_user_id;
544              
545             Answer will contains an user_id field.
546              
547             More info at L<Common functions: nop|https://www.reg.com/support/help/api2#common_get_user_id>.
548              
549             =head2 get_service_id
550              
551             Get service or domain name identifier by its name. Scope: B<clients>. Typical usage:
552              
553             $resp = $client->get_service_id(
554             domain_name => 'teselecta.ru',
555             );
556              
557             Answer will contains a service_id field or error code if requested domain name/service not found.
558              
559             More info at L<Common functions: nop|https://www.reg.com/support/help/api2#common_get_service_id>.
560              
561             =head1 SEE ALSO
562              
563             L<Regru::API::Bill>
564              
565             L<Regru::API::Domain>
566              
567             L<Regru::API::Folder>
568              
569             L<Regru::API::Service>
570              
571             L<Regru::API::User>
572              
573             L<Regru::API::Zone>
574              
575             L<Regru::API::Hosting>
576              
577             L<Regru::API::Shop>
578              
579             L<Regru::API::Response>
580              
581             L<REG.API Common functions|https://www.reg.com/support/help/api2#common_functions>
582              
583             L<REG.API Common error codes|https://www.reg.com/support/help/api2#common_errors>
584              
585             =head1 BUGS
586              
587             Please report any bugs or feature requests on the bugtracker website
588             L<https://github.com/regru/regru-api-perl/issues>
589              
590             When submitting a bug or request, please include a test-file or a
591             patch to an existing test-file that illustrates the bug or desired
592             feature.
593              
594             =head1 AUTHORS
595              
596             =over 4
597              
598             =item *
599              
600             Polina Shubina <shubina@reg.ru>
601              
602             =item *
603              
604             Anton Gerasimov <a.gerasimov@reg.ru>
605              
606             =back
607              
608             =head1 COPYRIGHT AND LICENSE
609              
610             This software is copyright (c) 2013 by REG.RU LLC.
611              
612             This is free software; you can redistribute it and/or modify it under
613             the same terms as the Perl 5 programming language system itself.
614              
615             =cut