File Coverage

blib/lib/WebService/ShiftPlanning.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 1     1   20730 use strict;
  1         2  
  1         41  
4 1     1   6 use warnings;
  1         2  
  1         29  
5 1     1   16 use 5.10.1;
  1         14  
  1         57  
6              
7 1     1   582 use JSON;
  0            
  0            
8             use LWP::UserAgent;
9             use LWP::Protocol::https;
10              
11             package WebService::ShiftPlanning;
12              
13             our $VERSION = 0.01;
14              
15             =head1 NAME
16              
17             WebService::ShiftPlanning - Minimal ShiftPlanning API call support for Perl
18              
19             =head1 SYNOPSIS
20              
21             use WebService::ShiftPlanning;
22             my $caller = WebService::ShiftPlanning->new;
23             $caller->doLogin('username', 'password');
24             use Data::Dumper;
25             print ::Dumper($caller->doCall('GET', 'dashboard.onnow'));
26              
27             =head1 DESCRIPTION
28              
29             A basic API wrapper for ShiftPlanning, supporting authentication, making calls, and throwing exceptions on error.
30              
31             =head1 METHODS
32              
33             =head2 new
34              
35             Create a new WebService::ShiftPlanning object.
36              
37             Takes the http endpoint and api key as optional hash parameters.
38              
39             my $agent = WebService::ShiftPlanning->new();
40              
41             my $agent = WebService::ShiftPlanning->new(
42             endpoint => 'https://www.shiftplanning.com/api/',
43             key => '234243iyu23i4y23409872309470923740987234',
44             );
45              
46             =cut
47              
48             sub new {
49             my $class = shift;
50             my %parms = (
51             endpoint => 'https://www.shiftplanning.com/api/',
52             key => undef,
53             token => undef,
54             @_
55             );
56             return bless \%parms,$class;
57             }
58              
59             =head2 doLogin
60              
61             Log in to shiftplanning.com
62              
63             =cut
64              
65             sub doLogin {
66             my $self = shift;
67             my ($username, $password) = @_;
68             if (!defined($self->{key})) {
69             die("You must specify your API key to the 'new' method; see perldoc WebService::ShiftPlanning");
70             }
71             my $ua = LWP::UserAgent->new();
72             my $json = JSON::encode_json({
73             key => $self->{key},
74             request => {
75             module => 'staff.login',
76             method => 'GET',
77             username => $username,
78             password => $password
79             }
80             });
81             # The request format is awful, it's application/x-www-form-urlencoded with
82             # the json data in a single key 'data'. The API does not understand application/json
83             my $response = $ua->post($self->{endpoint}, { data => $json } );
84             if ($response->is_success) {
85             # Shiftplanning API likes to return 200 OK with an error code embedded in the JSON result
86             my(%r) = %{JSON::decode_json($response->decoded_content)};
87             if ($r{'status'} == 1) {
88             $self->{'token'} = $r{'token'};
89             } else {
90             die("Shiftplanning API error $r{status} with error $r{error} on login attempt; login failed.");
91             }
92             } else {
93             die("HTTP error " . $response->code . ": " . $response->message . "; unable to login to shiftplanning");
94             }
95             }
96              
97             =head2 doCall
98              
99             Make a ShiftPlanning API call. Usage:
100              
101             doCall(method, module, param => value);
102              
103             eg:
104              
105             doCall('GET', 'dashboard.onnow');
106              
107             Dies on HTTP error or on ShiftPlanning.com API error (non-1 status). Otherwise
108             returns Perl hash/array decoded from the JSON response from the server.
109              
110             For the details of usage, you'll need to use the ShiftPlanning API docs.
111              
112             =cut
113              
114             sub doCall {
115             my $self = shift;
116             my ($method, $module, %request) = @_;
117             my $ua = LWP::UserAgent->new();
118             my $json = JSON::encode_json({
119             token => $self->{token},
120             key => $self->{key},
121             module => $module,
122             method => $method,
123             request => \%request
124             });
125             my $response = $ua->post($self->{endpoint}, { data => $json } );
126             if ($response->is_success) {
127             my (%r) = %{JSON::decode_json($response->decoded_content)};
128             if ($r{'status'} == 1) {
129             # Request successful, return the json result
130             return $r{'data'};
131             } else {
132             die("ShiftPlanning API error $r{status} with error $r{error} on request $method");
133             }
134             } else {
135             die("HTTP error " . $response->code . ": " . $response->message . "; unable to perform request");
136             }
137             }
138              
139             1;