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