-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_shiftboard_api.pl
More file actions
executable file
·62 lines (47 loc) · 1.71 KB
/
call_shiftboard_api.pl
File metadata and controls
executable file
·62 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/perl
use warnings ('FATAL' => 'all');
use 5.014;
my $ACCESS_KEY_ID = 'Your Shiftboard API key';
my $SIGNATURE_KEY = 'Your Shiftboard API signature key -- keep this very protected';
use MIME::Base64 ();
use Digest::HMAC_SHA1 ();
use URI::Escape ();
use JSON::XS ();
use LWP::UserAgent ();
# Call the Shiftboard API's system.echo method
my %params = (
'dinner' => 'nachos',
);
my $data = _call_api( 'system.echo', \%params );
# Print out what we got back.
use Data::Dumper ();
say Data::Dumper::Dumper( $data );
exit;
sub _call_api {
my ( $method, $params ) = @_;
# Convert the params to JSON
my $json_params = JSON::XS::encode_json(\%params);
# Take the JSON, BASE-64 encode it, then URI escape that.
my $uri64_params = URI::Escape::uri_escape(MIME::Base64::encode_base64($json_params,''));
# Sign this request using our secret signature key
my $sign = "method" . $method . "params" . $json_params;
my $signature = MIME::Base64::encode_base64( Digest::HMAC_SHA1::hmac_sha1( $sign, $SIGNATURE_KEY ), '' );
# Assemble the URL
my $url = join( '&',
'https://api.shiftdata.com/api/api.cgi?jsonrpc=2.0',
"access_key_id=$ACCESS_KEY_ID",
"method=$method",
"params=$uri64_params",
"signature=$signature",
'id=1',
);
# Create an http request
my $request = HTTP::Request->new(GET => $url);
# Pass request to the user agent and get a response back
my $ua = LWP::UserAgent->new();
my $response = $ua->request($request);
# Get the returned content
my $return_string = $response->content();
# The API returns JSON, decode that and return the data structure.
return JSON::XS::decode_json($return_string);
}