API.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 118 CC 10 SCAR 24.1
CC
10
Cov
0%
new
20.0
_call_v2
20.0
_call_v1
12.0
CRAP 11.1
1 package WWW::Bizowie::API;
2  
3 use strict;
4 use warnings;
5  
6 use WWW::Bizowie::API::Response;
7 use LWP::UserAgent;
8 use HTTP::Request::Common;
9 use Try::Tiny;
10 use JSON;
11  
12 our $VERSION = '0.05';
13  
14 =head1 NAME
15  
16 WWW::Bizowie::API - Perl API interface to Bizowie's L<erp software|http://bizowie.com/>.
17  
18 =head1 SYNOPSIS
19  
20 my $bz = WWW::Bizowie::API->new(
21 api_key => '02cc7058-cd22-4c8e-ad7c-a8f3f2a64bd0',
22 secret_key => '58c57abc-1e16-3571-bb35-73876bcef746',
23 site => 'mysite.bizowie.com',
24 );
25  
26 $bz->call(
27 'databases/add_note/3/10/123', {
28 comment => "I added this comment via the API!",
29 },
30 );
31  
32 =head1 METHODS
33  
34 =head2 new
35  
36 Returns a new instance of WWW::Bizowie::API
37  
38 Requires three parameters: api_key (your Bizowie API key), secret_key, and site (the hostname of your Bizowie instance).
39  
40 Optionally accepts:
41  
42 =over 4
43  
44 =item * v2 - if true, calls are routed through the v2 API endpoint (C</bz/apiv2/call/>) instead of the v1 endpoint (C</bz/api/>). Recommended for new integrations.
45  
46 =item * api_version - when v2 mode is enabled, specifies the API version sent with each request (defaults to C<'1.00'>).
47  
48 =item * debug - if true, warns with the raw HTTP response when a response cannot be decoded as JSON.
49  
50 =back
51  
52 =cut
53  
54 sub new
55 {
56 my ($class, %params) = @_;
57  
58 my $bzua = LWP::UserAgent->new;
59 $bzua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0 );
60 $bzua->agent('Bizowie::API');
61  
62 die 'site not specified' unless $params{site};
63 die 'api_key not specified' unless $params{api_key};
64 die 'secret_key not specified' unless $params{secret_key};
65  
66 my $self = {
67 ua => $bzua,
68 api_key => $params{api_key},
69 secret_key => $params{secret_key},
70 site => $params{site},
71 v2 => $params{v2},
72 debug => $params{debug},
73 api_version => $params{api_version},
74 };
75  
76 bless($self);
77  
78 return $self;
79 }
80  
81 =head2 call
82  
83 Makes a Bizowie API call.
84  
85 Takes two parameters: a string indicating the path to the API method you wish to call, and a hash reference of the parameters to be passed.
86  
87 If the object was constructed with C<< v2 => 1 >>, the request is dispatched to the v2 endpoint; otherwise the v1 endpoint is used.
88  
89 =cut
90  
91 sub call
92 {
93 my ($self, $method, $params) = @_;
94  
95 my $api_method = $self->{v2} ? '_call_v2' : '_call_v1';
96  
97 return $self->$api_method($method, $params);
98 }
99  
100 sub _call_v1
101 {
102 my ($self, $method, $params) = @_;
103  
104 my $site = $self->{site};
105  
106 die "[Bizowie::API] fatal error: no method given" unless $method;
107  
108 my $request = encode_json($params || { });
109  
110 my $q = $self->{ua}->request(POST("https://${site}/bz/api/$method",
111 Content_Type => 'form-data',
112 Content => [
113 api_key => $self->{api_key},
114 secret_key => $self->{secret_key},
115 site => $site,
116 request => $request,
117 ])
118 );
119  
120 my $o;
121 {
122 local $SIG{__DIE__} = sub { };
123 try {
124 $o = decode_json($q->decoded_content);
125 } catch {
126 if ($self->{debug})
127 {
128 warn $q->as_string;
129 }
130  
131 $o = { unprocessed => 1 };
132 };
133 }
134  
135 return WWW::Bizowie::API::Response->new(
136 data => $o,
137 success => delete $o->{success} || 0,
138 );
139 }
140  
141 sub _call_v2
142 {
143 my ($self, $method, $params) = @_;
144  
145 my $site = $self->{site};
146  
147 die "[Bizowie::API] fatal error: no method given" unless $method;
148  
149 $params = $params || {};
150 $params->{api_key} = $self->{api_key};
151 $params->{secret_key} = $self->{secret_key};
152 $params->{api_version} = $self->{api_version} || '1.00'
153 unless $params->{api_version};
154  
155 my $request = encode_json($params || { });
156  
157 my $q = $self->{ua}->request(POST("https://${site}/bz/apiv2/call/$method",
158 Content_Type => 'form-data',
159 Content => $request,
160 )
161 );
162  
163 my $o;
164 {
165 local $SIG{__DIE__} = sub { };
166 try {
167 $o = decode_json($q->decoded_content);
168 } catch {
169 $o = { unprocessed => 1 };
170  
171 if ($self->{debug})
172 {
173 warn $q->as_string;
174 }
175 };
176 }
177  
178 return WWW::Bizowie::API::Response->new(
179 data => $o,
180 success => delete $o->{success} || 0,
181 );
182 }
183  
184  
185  
186  
187 =head1 DEPENDENCIES
188  
189 HTTP::Request::Common, LWP::UserAgent, Try::Tiny, JSON, Mo
190  
191 =head1 AUTHORS
192  
193 Bizowie
194  
195 =head1 COPYRIGHT AND LICENSE
196  
197 Copyright (C) 2013 Bizowie
198  
199 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
200  
201 =cut
202  
203 1;
204