line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Regru::API::Role::Client; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: something that makes requests to API |
4
|
|
|
|
|
|
|
|
5
|
12
|
|
|
12
|
|
111144
|
use strict; |
|
12
|
|
|
|
|
28
|
|
|
12
|
|
|
|
|
345
|
|
6
|
12
|
|
|
12
|
|
63
|
use warnings; |
|
12
|
|
|
|
|
25
|
|
|
12
|
|
|
|
|
285
|
|
7
|
12
|
|
|
12
|
|
59
|
use Moo::Role; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
64
|
|
8
|
12
|
|
|
12
|
|
8890
|
use Regru::API::Response; |
|
12
|
|
|
|
|
37
|
|
|
12
|
|
|
|
|
424
|
|
9
|
12
|
|
|
12
|
|
82
|
use namespace::autoclean; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
72
|
|
10
|
12
|
|
|
12
|
|
748
|
use Carp; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
9990
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.051'; # VERSION |
13
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
with qw( |
16
|
|
|
|
|
|
|
Regru::API::Role::Namespace |
17
|
|
|
|
|
|
|
Regru::API::Role::Serializer |
18
|
|
|
|
|
|
|
Regru::API::Role::UserAgent |
19
|
|
|
|
|
|
|
Regru::API::Role::Loggable |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has username => ( |
23
|
|
|
|
|
|
|
is => 'rw', |
24
|
|
|
|
|
|
|
required => 1, |
25
|
|
|
|
|
|
|
predicate => 'has_username', |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
has password => ( |
28
|
|
|
|
|
|
|
is => 'rw', |
29
|
|
|
|
|
|
|
required => 1, |
30
|
|
|
|
|
|
|
predicate => 'has_password', |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
has io_encoding => ( |
33
|
|
|
|
|
|
|
is => 'rw', |
34
|
|
|
|
|
|
|
isa => sub { |
35
|
|
|
|
|
|
|
my %valid = map { ($_ => 1) } qw(utf8 cp1251 cp866 koi8-r koi8-u); |
36
|
|
|
|
|
|
|
croak "Empty encoding value" unless $_[0]; |
37
|
|
|
|
|
|
|
croak "Unsupported encoding: $_[0]" unless exists $valid{$_[0]}; |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
predicate => 'has_io_encoding', |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
has lang => ( |
42
|
|
|
|
|
|
|
is => 'rw', |
43
|
|
|
|
|
|
|
isa => sub { |
44
|
|
|
|
|
|
|
my %valid = map { ($_ => 1) } qw(en ru th); |
45
|
|
|
|
|
|
|
croak "Empty language value" unless $_[0]; |
46
|
|
|
|
|
|
|
croak "Unsupported language: $_[0]" unless exists $valid{$_[0]}; |
47
|
|
|
|
|
|
|
}, |
48
|
|
|
|
|
|
|
predicate => 'has_lang', |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
has debug => ( |
51
|
|
|
|
|
|
|
is => 'rw', |
52
|
|
|
|
|
|
|
predicate => 'has_debug', |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
has namespace => ( |
56
|
|
|
|
|
|
|
is => 'ro', |
57
|
|
|
|
|
|
|
default => sub { '' }, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has endpoint => ( |
61
|
|
|
|
|
|
|
is => 'ro', |
62
|
|
|
|
|
|
|
default => sub { $ENV{REGRU_API_ENDPOINT} || 'https://api.reg.ru/api/regru2' }, |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub namespace_methods { |
66
|
21
|
|
|
21
|
|
1660
|
my $class = shift; |
67
|
|
|
|
|
|
|
|
68
|
21
|
|
|
|
|
145
|
my $meta = $class->meta; |
69
|
|
|
|
|
|
|
|
70
|
21
|
|
|
|
|
1373
|
foreach my $method ( @{ $class->available_methods } ) { |
|
21
|
|
|
|
|
84
|
|
71
|
150
|
|
|
|
|
814565
|
$method = lc $method; |
72
|
150
|
|
|
|
|
363
|
$method =~ s/\s/_/g; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $handler = sub { |
75
|
0
|
|
|
0
|
|
0
|
my ($self, @args) = @_; |
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
76
|
0
|
|
|
|
|
0
|
$self->api_request($method => @args); |
77
|
150
|
|
|
|
|
535
|
}; |
78
|
|
|
|
|
|
|
|
79
|
150
|
|
|
|
|
656
|
$meta->add_method($method => $handler); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub api_request { |
84
|
0
|
|
|
0
|
|
0
|
my ($self, $method, %params) = @_; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $url = join '' => $self->endpoint, |
87
|
0
|
0
|
|
|
|
0
|
$self->to_namespace(delete $params{namespace}), # compose namespace part |
88
|
|
|
|
|
|
|
($method ? '/' . $method : ''); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# protect I/O formats against modifying |
91
|
0
|
|
|
|
|
0
|
delete $params{output_format}; |
92
|
0
|
|
|
|
|
0
|
delete $params{input_format}; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
0
|
my %post_params = ( |
95
|
|
|
|
|
|
|
username => $self->username, |
96
|
|
|
|
|
|
|
password => $self->password, |
97
|
|
|
|
|
|
|
output_format => 'json', |
98
|
|
|
|
|
|
|
input_format => 'json', |
99
|
|
|
|
|
|
|
); |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
0
|
$post_params{lang} = $self->lang if $self->has_lang; |
102
|
0
|
0
|
|
|
|
0
|
$post_params{io_encoding} = $self->io_encoding if $self->has_io_encoding; |
103
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
0
|
$self->debug_warn('API request:', $url, "\n", 'with params:', \%params) if $self->debug; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
0
|
my $json = $self->serializer->encode( \%params ); |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
0
|
my $response = $self->useragent->post( |
109
|
|
|
|
|
|
|
$url, |
110
|
|
|
|
|
|
|
[ %post_params, input_data => $json ] |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
0
|
return Regru::API::Response->new( response => $response, debug => $self->debug ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub to_namespace { |
117
|
4
|
|
|
4
|
|
966
|
my ($self, $namespace) = @_; |
118
|
|
|
|
|
|
|
|
119
|
4
|
|
50
|
|
|
44
|
$namespace = $namespace || $self->namespace || undef; |
120
|
|
|
|
|
|
|
|
121
|
4
|
50
|
|
|
|
21
|
return $namespace ? '/' . $namespace : ''; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; # End of Regru::API::Role::Client |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
__END__ |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=pod |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=encoding UTF-8 |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 NAME |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Regru::API::Role::Client - something that makes requests to API |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 VERSION |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
version 0.051 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 SYNOPSIS |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# in some namespace package |
143
|
|
|
|
|
|
|
package Regru::API::Dummy; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
use strict; |
146
|
|
|
|
|
|
|
use warnings; |
147
|
|
|
|
|
|
|
use Moo; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
with 'Regru::API::Role::Client'; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
has '+namespace' => ( |
152
|
|
|
|
|
|
|
default => sub { 'dummy' }, |
153
|
|
|
|
|
|
|
); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub available_methods {[qw(foo bar baz)]} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__PACKAGE__->namespace_methods; |
158
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 DESCRIPTION |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Any class or role that consumes this role will able to execute requests to REG.API v2. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 username |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Account name of the user to access to L<reg.com|https://www.reg.com> website. Required. Should be passed at instance |
169
|
|
|
|
|
|
|
create time. Although it might be changed at runtime. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 password |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Account password of the user to access to L<reg.com|https://www.reg.com> website or an alternative password for API |
174
|
|
|
|
|
|
|
defined at L<Reseller settings|https://www.reg.com/reseller/details> page. Required. Should be passed at instance create time. |
175
|
|
|
|
|
|
|
Although it might be changed at runtime. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 io_encoding |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Defines encoding that will be used for data exchange between the Service and the Client. At the moment REG.API v2 |
180
|
|
|
|
|
|
|
supports the following encodings: C<utf8>, C<cp1251>, C<koi8-r>, C<koi8-u>, C<cp866>. Optional. Default value is B<utf8>. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 lang |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Defines the language which will be used in error messages. At the moment REG.API v2 supports the following languages: |
185
|
|
|
|
|
|
|
C<en> (English), C<ru> (Russian) and C<th> (Thai). Optional. Default value is B<en>. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 debug |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
A few messages will be printed to STDERR. Default value is B<0> (suppressed debug activity). |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head2 namespace |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Used internally. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 endpoint |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
REG.API v2 endpoint url. There's no needs to change it. Although it might be overridden by setting environment variable: |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
export REGRU_API_ENDPOINT=https://api.example.com |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Default value is |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
https://api.reg.ru/api/regru2 |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 METHODS |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 namespace_methods |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Dynamically creates methods-shortcuts in in namespaces (categories) for requests to appropriate REG.API v2 functions. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head2 api_request |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Performs an API request to REG.API service. Returns a L<Regru::API::Response> object. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 to_namespace |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Used internally. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head1 SEE ALSO |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L<Regru::API> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
L<Regru::API::Role::Namespace> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
L<Regru::API::Role::Serializer> |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
L<Regru::API::Role::UserAgent> |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
L<Regru::API::Role::Loggable> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
L<Regru::API::Response> |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 BUGS |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website |
236
|
|
|
|
|
|
|
L<https://github.com/regru/regru-api-perl/issues> |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
239
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
240
|
|
|
|
|
|
|
feature. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head1 AUTHORS |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=over 4 |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=item * |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Polina Shubina <shubina@reg.ru> |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=item * |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Anton Gerasimov <a.gerasimov@reg.ru> |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=back |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
This software is copyright (c) 2013 by REG.RU LLC. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
261
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=cut |