File Coverage

blib/lib/App/Spoor/ApiClient.pm
Criterion Covered Total %
statement 31 31 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 41 41 100.0


line stmt bran cond sub pod time code
1             package App::Spoor::ApiClient;
2              
3 1     1   244067 use v5.10;
  1         11  
4 1     1   5 use strict;
  1         2  
  1         20  
5 1     1   5 use warnings;
  1         2  
  1         26  
6 1     1   5 use JSON;
  1         2  
  1         5  
7 1     1   555 use PHP::HTTPBuildQuery qw(http_build_query);
  1         605  
  1         60  
8 1     1   8 use MIME::Base64 qw(encode_base64);
  1         2  
  1         256  
9              
10             =head1 NAME
11              
12             App::Spoor::ApiClient
13              
14             =head1 VERSION
15              
16             Version 0.07
17              
18             =cut
19              
20             our $VERSION = '0.07';
21              
22             =head1 SYNOPSIS
23              
24             Interacts with the Spoor API. see the individual subroutines for more detail.
25              
26             use Sys::Hostname;
27             use App::Spoor::ApiClient;
28              
29             my %data = (
30             ...
31             );
32             my $user_agent = LWP::UserAgent->new;
33              
34             my $transmission_config = App::Spoor::Config::get_transmission_config();
35              
36             App::Spoor::ApiClient::most_recent_reports(hostname, $user_agent, $transmission_config);
37             App::Spoor::ApiClient::most_recent_mailbox_events(hostname, $user_agent, $transmission_config);
38              
39             =head1 SUBROUTINES/METHODS
40              
41             =head2 most_recent_reports
42              
43             Fetches the most recent reports from the SpoorAPI. When the SpoorAPI receives data, it creates a report instance. This
44             report instance is then parsed and, if all is in order, a mailbox event is created.
45              
46             This subroutine returns a reference to an array of hashes representing the individual report instances.
47              
48             use Sys::Hostname;
49             use App::Spoor::ApiClient;
50              
51             my %data = (
52             ...
53             );
54             my $user_agent = LWP::UserAgent->new;
55              
56             my $transmission_config = App::Spoor::Config::get_transmission_config();
57              
58             App::Spoor::ApiClient::most_recent_reports(hostname, $user_agent, $transmission_config);
59              
60             =cut
61              
62             sub most_recent_reports {
63 1     1 1 4021 my $host = shift;
64 1         2 my $user_agent = shift;
65 1         3 my $config = shift;
66              
67 1         8 my $uri = $config->{host} . '/api/reports?' . http_build_query({reports => {host => $host}});
68              
69             my $credentials = 'Basic ' . encode_base64(
70             $config->{credentials}{api_identifier} . ':' . $config->{credentials}{api_secret}
71 1         142 );
72              
73 1         11 my $response = $user_agent->get(
74             $uri,
75             'Authorization' => $credentials,
76             'HTTP-Accept' => 'application/json'
77             );
78              
79 1         11813 from_json($response->content)->{reports};
80             }
81              
82             =head2 most_recent_mailbox_events
83              
84             Fetches the most recent mailbox events from the SpoorAPI.
85              
86             Thsi subroutine returns a reference to an array of hashes representing individual mailbox events.
87              
88             use Sys::Hostname;
89             use App::Spoor::ApiClient;
90              
91             my %data = (
92             ...
93             );
94             my $user_agent = LWP::UserAgent->new;
95              
96             my $transmission_config = App::Spoor::Config::get_transmission_config();
97              
98             App::Spoor::ApiClient::most_recent_mailbox_events(hostname, $user_agent, $transmission_config);
99              
100             =cut
101              
102             sub most_recent_mailbox_events {
103 1     1 1 4419 my $host = shift;
104 1         3 my $user_agent = shift;
105 1         2 my $config = shift;
106              
107 1         7 my $uri = $config->{host} . '/api/mailbox_events?' . http_build_query({mailbox_events => {host => $host}});
108              
109             my $credentials = 'Basic ' . encode_base64(
110             $config->{credentials}{api_identifier} . ':' . $config->{credentials}{api_secret}
111 1         104 );
112              
113 1         5 my $response = $user_agent->get(
114             $uri,
115             'Authorization' => $credentials,
116             'HTTP-Accept' => 'application/json'
117             );
118              
119 1         1157 from_json($response->content)->{mailbox_events};
120             }
121              
122             =head1 AUTHOR
123              
124             Rory McKinley, C<< <rorymckinley at capefox.co> >>
125              
126             =head1 BUGS
127              
128             Please report any bugs or feature requests to C<bug-. at rt.cpan.org>, or through
129             the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>. I will be notified, and then you'll
130             automatically be notified of progress on your bug as I make changes.
131              
132              
133             =head1 SUPPORT
134              
135             You can find documentation for this module with the perldoc command.
136              
137             perldoc App::Spoor::ApiClient
138              
139              
140             You can also look for information at:
141              
142             =over 4
143              
144             =item * RT: CPAN's request tracker (report bugs here)
145              
146             L<httops://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
147              
148             =item * AnnoCPAN: Annotated CPAN documentation
149              
150             L<http://annocpan.org/dist/.>
151              
152             =item * CPAN Ratings
153              
154             L<https://cpanratings.perl.org/d/.>
155              
156             =item * Search CPAN
157              
158             L<https://metacpan.org/release/.>
159              
160             =back
161              
162             =head1 LICENSE AND COPYRIGHT
163              
164             Copyright 2019 Rory McKinley.
165              
166             This program is free software; you can redistribute it and/or modify it
167             under the terms of the the Artistic License (2.0). You may obtain a
168             copy of the full license at:
169              
170             L<http://www.perlfoundation.org/artistic_license_2_0>
171              
172             Any use, modification, and distribution of the Standard or Modified
173             Versions is governed by this Artistic License. By using, modifying or
174             distributing the Package, you accept this license. Do not use, modify,
175             or distribute the Package, if you do not accept this license.
176              
177             If your Modified Version has been derived from a Modified Version made
178             by someone other than you, you are nevertheless required to ensure that
179             your Modified Version complies with the requirements of this license.
180              
181             This license does not grant you the right to use any trademark, service
182             mark, tradename, or logo of the Copyright Holder.
183              
184             This license includes the non-exclusive, worldwide, free-of-charge
185             patent license to make, have made, use, offer to sell, sell, import and
186             otherwise transfer the Package with respect to any patent claims
187             licensable by the Copyright Holder that are necessarily infringed by the
188             Package. If you institute patent litigation (including a cross-claim or
189             counterclaim) against any party alleging that the Package constitutes
190             direct or contributory patent infringement, then this Artistic License
191             to you shall terminate on the date that such litigation is filed.
192              
193             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
194             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
195             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
196             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
197             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
198             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
199             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
200             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
201              
202              
203             =cut
204              
205             1; # End of App::Spoor::ApiClient