-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAgile.pm
113 lines (85 loc) · 2.15 KB
/
Agile.pm
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
=head1 NAME
Integrations::Agile
=head1 DESCRIPTION
This module provides an interface to the Agile Applications API
https://agileapplications.co.uk/
=cut
package Integrations::Agile;
use strict;
use warnings;
use HTTP::Request;
use JSON::MaybeXS;
use LWP::UserAgent;
use Moo;
use URI;
with 'Role::Config';
with 'Role::Logger';
has ua => (
is => 'lazy',
default => sub {
LWP::UserAgent->new( agent => 'FixMyStreet/open311-adapter' );
},
);
sub api_call {
my ( $self, %args ) = @_;
my $action = $args{action};
my $controller = $args{controller};
my $data = $args{data};
my $method = 'POST';
my $body = {
Method => $method,
Controller => $controller,
Action => $action,
Data => $data,
};
my $body_json = encode_json($body);
my $uri = URI->new( $self->config->{url} );
my $req = HTTP::Request->new( $method, $uri );
$req->content_type('application/json; charset=UTF-8');
$req->content($body_json);
$self->logger->debug($action);
$self->logger->debug($body_json);
my $res = $self->ua->request($req);
if ( $res->is_success ) {
$self->logger->debug( $res->content );
return decode_json( $res->content );
} else {
$self->logger->error($action);
$self->logger->error($body_json);
$self->logger->error( $res->content );
die $res->content;
}
}
sub Cancel {
my ( $self, $params ) = @_;
return $self->api_call(
action => 'cancel',
controller => 'servicecontract',
data => $params,
);
}
sub IsAddressFree {
my ( $self, $uprn ) = @_;
return $self->api_call(
action => 'isaddressfree',
controller => 'customer',
data => { UPRN => $uprn },
);
}
sub Renew {
my ( $self, $params ) = @_;
return $self->api_call(
action => 'renewal',
controller => 'servicecontract',
data => $params,
);
}
sub SignUp {
my ( $self, $params ) = @_;
return $self->api_call(
action => 'signup',
controller => 'customer',
data => $params,
);
}
1;