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