File Coverage

blib/lib/WebService/Qiita/V2/Client/Base.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


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