File Coverage

blib/lib/WebService/Freshservice/API.pm
Criterion Covered Total %
statement 40 94 42.5
branch 0 38 10.5
condition n/a
subroutine 17 26 65.3
pod n/a
total 57 158 38.6


line stmt bran cond sub pod time code
1             package WebService::Freshservice::API;
2              
3 6     6   14968 use v5.010;
  6         33  
4 6     6   45 use strict;
  6         18  
  6         161  
5 6     6   39 use warnings;
  6         17  
  6         421  
6 6     6   4584 use Method::Signatures 20140224;
  6         588201  
  6         52  
7 6     6   7853 use JSON qw( from_json to_json );
  6         80902  
  6         49  
8 6     6   4487 use MIME::Base64 qw( encode_base64 );
  6         3978  
  6         428  
9 6     6   50 use Try::Tiny;
  6         15  
  6         358  
10 6     6   41 use Carp qw( croak );
  6         16  
  6         320  
11 6     6   4113 use LWP::UserAgent;
  6         300553  
  6         251  
12 6     6   4588 use Data::Dumper;
  6         43638  
  6         488  
13 6     6   3459 use Moo;
  6         47938  
  6         45  
14 6     6   12480 use namespace::clean;
  6         30240  
  6         48  
15              
16             # ABSTRACT: Request abstraction to the Freshservice API
17              
18             our $VERSION = '0.004'; # VERSION: Generated by DZP::OurPkg:Version
19              
20              
21             our $DEBUG = $ENV{FRESHSERVICE_DEBUG} || 0;
22              
23             has 'apikey' => ( is => 'ro', required => 1 );
24             has 'apiurl' => ( is => 'ro', default => sub { "https://imdexlimited.freshservice.com" } );
25             has '_ua' => ( is => 'rw', lazy => 1, builder => 1 );
26              
27 6 0   6   12106 method _build__ua {
  0     0      
  0            
28 0           my $ua = LWP::UserAgent->new();
29 0           $ua->agent('WebService-Freshservice');
30 0           $ua->timeout(60);
31 0           $ua->env_proxy;
32 0           $ua->default_headers->push_header( 'Content-Type' => "application/json" );
33              
34             # The Freshservice api isn't standards complaint as far as auth goes. It returns 200 OK
35             # whether you're authenticated or not and LWP doesn't provide credentials unless it
36             # it is challenged to do so. I've raised a support case..
37 0           $ua->default_headers->push_header( Authorization => "Basic ".encode_base64($self->apikey.":X" ) );
38 0           return $ua;
39             }
40              
41              
42 6 0   6   817700 method get_api ($endpoint) {
  0 0   0      
  0            
  0            
  0            
43 0           my $result = $self->_ua->get($self->apiurl."/".$endpoint);
44              
45             # uncoverable branch true
46 0 0         say Dumper($result) if $DEBUG;
47 0 0         croak "API failed - error: '".$result->message."'" unless $result->is_success;
48            
49 0           my $data;
50             try {
51 0     0     $data = from_json($result->decoded_content);
52             } catch {
53 0     0     croak("Failed to parse json $_");
54 0           };
55 0           return $data;
56             }
57              
58              
59 6 0   6   24757 method post_api ($endpoint,$content) {
  0 0   0      
  0 0          
  0            
  0            
  0            
  0            
60 0           my $result = $self->_ua->post(
61             $self->apiurl."/".$endpoint,
62             "Content_Type" => 'application/json',
63             Content => to_json($content),
64             );
65              
66             # uncoverable branch true
67 0 0         say Dumper($result) if $DEBUG;
68 0 0         croak "API failed - error: '".$result->message."'" unless $result->is_success;
69            
70 0           my $data;
71             try {
72 0     0     $data = from_json($result->decoded_content);
73             } catch {
74 0     0     croak("Failed to parse json $_");
75 0           };
76 0           return $data;
77             }
78              
79              
80 6 0   6   24772 method put_api ($endpoint,$content) {
  0 0   0      
  0 0          
  0            
  0            
  0            
  0            
81 0           my $data = to_json(
82             $content, {
83             allow_blessed => 1,
84             convert_blessed => 1,
85             }
86             );
87 0           my $result = $self->_ua->put(
88             $self->apiurl."/".$endpoint,
89             "Content_Type" => 'application/json',
90             Content => $data,
91             );
92              
93             # uncoverable branch true
94 0 0         say Dumper($result) if $DEBUG;
95 0 0         croak "API failed - error: '".$result->message."'" unless $result->is_success;
96 0           return 1;
97             }
98              
99              
100 6 0   6   20124 method delete_api ($endpoint) {
  0 0   0      
  0            
  0            
  0            
101 0           my $result = $self->_ua->delete($self->apiurl."/".$endpoint);
102              
103             # uncoverable branch true
104 0 0         say Dumper($result) if $DEBUG;
105 0 0         croak "API failed - error: '".$result->message."'" unless $result->is_success;
106            
107 0           return 1;
108             }
109              
110             1;
111              
112             __END__
113              
114             =pod
115              
116             =encoding UTF-8
117              
118             =head1 NAME
119              
120             WebService::Freshservice::API - Request abstraction to the Freshservice API
121              
122             =head1 VERSION
123              
124             version 0.004
125              
126             =head1 SYNOPSIS
127              
128             use WebService::Freshservice::API;
129              
130             my $request = WebService::Freshservice::API->new( apikey => 'xxxxxxxxxxxxxxxxxxxxxx' );
131              
132             =head1 DESCRIPTION
133              
134             Provides a light wrapper to LWP::UserAgent against the Freshservice APIs
135              
136             =head1 METHODS
137              
138             =head2 get_api
139              
140             $api->get_api( "itil/requesters/123456.json" );
141              
142             Returns a perl object of the JSON decoded data structure API. Croaks
143             on failure.
144              
145             =head2 post_api
146              
147             $api->post_api( "itil/requesters.json", $data );
148              
149             Returns a perl object of the JSON decoded data structure API. Croaks
150             on failure.
151              
152             =head2 put_api
153              
154             $api->put_api( "itil/requesters.json", $data );
155              
156             Returns 1 on success. Croaks on failure.
157              
158             =head2 delete_api
159              
160             $api->delete_api( "itil/requesters/123456.json" );
161              
162             Returns 1 on success. Croaks on failure.
163              
164             =head1 AUTHOR
165              
166             Leon Wright <techman@cpan.org>
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is copyright (c) 2016 by Leon Wright.
171              
172             This is free software; you can redistribute it and/or modify it under
173             the same terms as the Perl 5 programming language system itself.
174              
175             =cut