plenigo/RestClient.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 100 CC 6 SCAR 14.4
CC
6
Cov
0%
_createRestClient
6.0
_checkResponse
6.0
post
6.0
CRAP 4.2
1 package plenigo::RestClient;
2  
3 =head1 NAME
4 plenigo::RestClient - Only for internal usage.
5 =head1 SYNOPSIS
6 Only for internal usage. Can contain breacking changes. DO NOT USE.
7 =head1 DESCRIPTION
8 plenigo::RestClient provides functionality for communication with the plenigo API. Only for internal usage. Can contain breacking changes. DO NOT USE.
9 =cut
10  
11 use REST::Client;
12 use JSON;
13 use Carp qw(confess);
14 use Crypt::JWT qw(encode_jwt);
15 use Data::UUID;
16 use Moo;
17 use plenigo::Ex;
18  
19 our $VERSION = '0.2000';
20  
21 has configuration => (
22 is => 'ro',
23 required => 1
24 );
25  
26 sub _createRestClient {
27 my ($self) = @_;
28  
29 my $client = REST::Client->new({
30 host => $self->configuration->use_stage ? $self->configuration->api_host_stage : $self->configuration->api_host,
31 timeout => 10,
32 });
33 $client->addHeader('X-plenigo-token', $self->configuration->access_token);
34 $client->addHeader('Content-Type', 'application/json');
35 $client->addHeader('Accept', 'application/json');
36  
37 return $client;
38 }
39  
40 sub _checkResponse {
41 my ($self, $client) = @_;
42  
43 return if $client->responseCode < 400;
44  
45 plenigo::Ex->throw({
46 code => $client->responseCode,
47 message => decode_json($client->responseContent)->{errorMessage} || 'Bad Request',
48 errorDetails => decode_json($client->responseContent || '{}'),
49 });
50 }
51  
52 sub get {
53 my ($self, $url_path, $query_params) = @_;
54  
55 my $client = $self->_createRestClient;
56 $client->GET($self->configuration->api_url . $url_path . $client->buildQuery($query_params));
57 $self->_checkResponse($client);
58 return(response_code => $client->responseCode, response_content => decode_json($client->responseContent));
59 }
60  
61 sub post {
62 my ($self, $url_path, $query_params, %body) = @_;
63  
64 my $client = $self->_createRestClient;
65 my $json_text = encode_json \%body;
66 $client->POST($self->configuration->api_url . $url_path . $client->buildQuery($query_params), $json_text);
67 $self->_checkResponse($client);
68 my $responseContent;
69 if (not $client->responseContent eq "") {
70 $responseContent = decode_json($client->responseContent);
71 }
72 return(response_code => $client->responseCode, response_content => $responseContent);
73 }
74  
75 sub put {
76 my ($self, $url_path, $query_params, %body) = @_;
77  
78 my $client = $self->_createRestClient;
79 my $json_text = encode_json \%body;
80 $client->PUT($self->configuration->api_url . $url_path . $client->buildQuery($query_params), $json_text);
81 $self->_checkResponse($client);
82 my $responseContent;
83 if (not $client->responseContent eq "") {
84 $responseContent = decode_json($client->responseContent);
85 }
86 return(response_code => $client->responseCode, response_content => $responseContent);
87 }
88  
89 sub delete {
90 my ($self, $url_path, $query_params) = @_;
91  
92 my $client = $self->_createRestClient;
93 $client->DELETE($self->configuration->api_url . $url_path . $client->buildQuery($query_params));
94 $self->_checkResponse($client);
95 my $responseContent;
96 if (not $client->responseContent eq "") {
97 $responseContent = decode_json($client->responseContent);
98 }
99 return(response_code => $client->responseCode, response_content => $responseContent);
100 }
101  
102 1;