File Coverage

blib/lib/WebService/Qiita/V2/Client/Base.pm
Criterion Covered Total %
statement 52 129 40.3
branch 11 44 25.0
condition 1 2 50.0
subroutine 12 17 70.5
pod 0 8 0.0
total 76 200 38.0


line stmt bran cond sub pod time code
1             package WebService::Qiita::V2::Client::Base;
2 5     5   1789 use strict;
  5         8  
  5         107  
3 5     5   18 use warnings;
  5         9  
  5         91  
4              
5 5     5   2478 use JSON;
  5         45109  
  5         21  
6 5     5   3137 use LWP::UserAgent;
  5         182055  
  5         150  
7 5     5   35 use HTTP::Request;
  5         10  
  5         108  
8 5     5   26 use URI;
  5         10  
  5         129  
9              
10             use constant {
11 5         5887 API_URL => 'qiita.com/api/',
12             API_VER => 'v2',
13 5     5   22 };
  5         20  
14              
15             sub new {
16 4     4 0 10 my ($class, $args) = @_;
17              
18 4   50     20 $args ||= {};
19 4         19 $args = {%$args, (
20             ua => undef,
21             error => undef,
22             )};
23 4         10 my $self = bless $args, $class;
24 4         16 $self;
25             }
26              
27             sub ua {
28 4     4 0 7 my $self = shift;
29 4 100       21 return $self->{ua} if defined $self->{ua};
30 3         12 my $options = {
31             ssl_opts => { verify_hostname => 0 },
32             };
33 3         22 $self->{ua} = LWP::UserAgent->new(%$options);
34             }
35              
36             sub get {
37 4     4 0 12 my ($self, $func, $params, $args) = @_;
38              
39 4         24 my $url = 'https://' . $self->_team($args) . API_URL . API_VER . "/$func";
40 4         24 my $uri = URI->new($url);
41 4 50       19203 if ($params) {
42 0         0 $uri->query_form(%$params);
43             }
44              
45 4         24 my $req = HTTP::Request->new("GET", $uri->as_string);
46 4 100       632 if (defined $args->{headers}) {
47 1         3 for (keys %{$args->{headers}}) {
  1         5  
48 1         7 $req->header($_ => $args->{headers}->{$_});
49             }
50             }
51 4         154 $req->content_type('application/json');
52              
53 4         149 my $res = $self->ua->request($req);
54              
55 4 100       4838982 if ($res->code == 200) {
56 1 50       14 my $result = ($res->content) ? JSON::decode_json($res->content) : "";
57 1         72 return $result;
58             }
59              
60 3         64 $self->_set_error($res, $url, "GET");
61 3         101 return -1;
62             }
63              
64             sub post {
65 0     0 0 0 my ($self, $func, $params, $args) = @_;
66              
67 0         0 my $url = 'https://' . $self->_team($args) . API_URL . API_VER . "/$func";
68 0         0 my $uri = URI->new($url);
69              
70 0         0 my $req = HTTP::Request->new("POST", $uri);
71 0 0       0 if (defined $args->{headers}) {
72 0         0 for (keys %{$args->{headers}}) {
  0         0  
73 0         0 $req->header($_ => $args->{headers}->{$_});
74             }
75             }
76 0         0 $req->content_type('application/json');
77 0         0 $req->content(JSON::encode_json $params);
78              
79 0         0 my $res = $self->ua->request($req);
80              
81 0 0       0 if ($res->code == 201) {
82 0 0       0 my $result = ($res->content) ? JSON::decode_json($res->content) : "";
83 0         0 return $result;
84             }
85              
86 0         0 $self->_set_error($res, $url, "POST");
87 0         0 return -1;
88             }
89              
90             sub put {
91 0     0 0 0 my ($self, $func, $params, $args) = @_;
92              
93 0         0 my $url = 'https://' . $self->_team($args) . API_URL . API_VER . "/$func";
94 0         0 my $uri = URI->new($url);
95              
96 0         0 my $req = HTTP::Request->new("PUT", $uri);
97 0 0       0 if (defined $args->{headers}) {
98 0         0 for (keys %{$args->{headers}}) {
  0         0  
99 0         0 $req->header($_ => $args->{headers}->{$_});
100             }
101             }
102 0         0 $req->content_type('application/json');
103 0 0       0 $req->content(JSON::encode_json $params) if defined $params;
104              
105 0         0 my $res = $self->ua->request($req);
106              
107 0 0       0 return 1 if $res->code == 204;
108              
109 0         0 $self->_set_error($res, $url, "PUT");
110 0         0 return -1;
111             }
112              
113             sub patch {
114 0     0 0 0 my ($self, $func, $params, $args) = @_;
115              
116 0         0 my $url = 'https://' . $self->_team($args) . API_URL . API_VER . "/$func";
117 0         0 my $uri = URI->new($url);
118              
119 0         0 my $req = HTTP::Request->new("PATCH", $uri);
120 0 0       0 if (defined $args->{headers}) {
121 0         0 for (keys %{$args->{headers}}) {
  0         0  
122 0         0 $req->header($_ => $args->{headers}->{$_});
123             }
124             }
125 0         0 $req->content_type('application/json');
126 0         0 $req->content(JSON::encode_json $params);
127              
128 0         0 my $res = $self->ua->request($req);
129              
130 0 0       0 if ($res->code == 200) {
131 0 0       0 my $result = ($res->content) ? JSON::decode_json($res->content) : "";
132 0         0 return $result;
133             }
134              
135 0         0 $self->_set_error($res, $url, "PATCH");
136 0         0 return -1;
137             }
138              
139             sub delete {
140 0     0 0 0 my ($self, $func, $params, $args) = @_;
141              
142 0         0 my $url = 'https://' . $self->_team($args) . API_URL . API_VER . "/$func";
143 0         0 my $uri = URI->new($url);
144 0 0       0 if ($params) {
145 0         0 $uri->query_form(%$params);
146             }
147              
148 0         0 my $req = HTTP::Request->new("DELETE", $uri->as_string);
149 0 0       0 if (defined $args->{headers}) {
150 0         0 for (keys %{$args->{headers}}) {
  0         0  
151 0         0 $req->header($_ => $args->{headers}->{$_});
152             }
153             }
154 0         0 $req->content_type('application/json');
155              
156 0         0 my $res = $self->ua->request($req);
157              
158 0 0       0 return 1 if $res->code == 204;
159              
160 0         0 $self->_set_error($res, $url, "DELETE");
161 0         0 return -1;
162             }
163              
164             sub get_response_code {
165 0     0 0 0 my ($self, $func, $params, $args) = @_;
166              
167 0         0 my $url = 'https://' . $self->_team($args) . API_URL . API_VER . "/$func";
168 0         0 my $uri = URI->new($url);
169 0 0       0 if ($params) {
170 0         0 $uri->query_form(%$params);
171             }
172              
173 0         0 my $req = HTTP::Request->new("GET", $uri->as_string);
174 0 0       0 if (defined $args->{headers}) {
175 0         0 for (keys %{$args->{headers}}) {
  0         0  
176 0         0 $req->header($_ => $args->{headers}->{$_});
177             }
178             }
179 0         0 $req->content_type('application/json');
180              
181 0         0 my $res = $self->ua->request($req);
182 0 0       0 if ($res->code >= 300) {
183 0         0 $self->_set_error($res, $url, "GET");
184             }
185 0         0 return $res->code;
186             }
187              
188             sub _set_error {
189 3     3   11 my ($self, $res, $url, $method) = @_;
190              
191 3 50       13 my $content = ($res->content) ? JSON::decode_json($res->content) : "";
192              
193             $self->{error} = {
194 3         95 method => $method,
195             url => $url,
196             code => $res->code,
197             content => $content,
198             };
199             }
200              
201             sub _team {
202 4     4   10 my ($self, $args) = @_;
203 4 100       16 return $args->{team} . "." if (defined $args->{team});
204 3         15 return "";
205             }
206              
207             1;