Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TfL] Add passthrough integration for Atlas backend. #347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions conf/council-tfl.yml-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
endpoint: https://localhost:4000/
api_key: 123

oauth2_url: https://localhost:5000/
oauth2_client_id: client_id
oauth2_client_secret: client_secret
oauth2_tenant_id: tenant_id
ocp_apim_subscription_key: subscription_key
13 changes: 11 additions & 2 deletions perllib/Open311/Endpoint/Integration/Passthrough.pm
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,29 @@ the function to die.
sub _request {
my ($self, $method, $url, $params) = @_;
$url = URI->new($self->endpoint . $url);
my $headers = $self->_headers();
my $resp;
if ($method eq 'POST') {
$params->{api_key} = $self->api_key;
$resp = $self->ua->post($url, $params);
$resp = $self->ua->post($url, %$headers, Content => $params);
} else {
$url->query_form(%$params);
$resp = $self->ua->get($url);
$resp = $self->ua->get($url, %$headers);
}
my $content = $resp->decoded_content;
my $xml = $self->pt_xml->XMLin(\$content);
die $xml->{error}[0]->{description} . "\n" if $xml->{error};
return $xml;
}

=head2 _headers

This can be overriden to return headers used by C<_request>.

=cut

sub _headers { {} }

=head2 services

Transparently passed through to the backend to fetch a list of services.
Expand Down
67 changes: 67 additions & 0 deletions perllib/Open311/Endpoint/Integration/UK/TfL.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
=head1 NAME

Open311::Endpoint::Integration::UK::TfL - Tfl Atlas backend.

=head1 SUMMARY

This is a TfL-specific Passthrough intergration for their Atlas backend.
It is a standard Open311 server apart from it passes a bearer token
fetched from their OAuth2 API and a 'Ocp-Apim-Subscription-Key' header.

=cut

package Open311::Endpoint::Integration::UK::TfL;

use HTTP::Request::Common;
use JSON::MaybeXS;
use LWP::UserAgent;
use Moo;
extends 'Open311::Endpoint::Integration::Passthrough';

around BUILDARGS => sub {
my ($orig, $class, %args) = @_;
$args{jurisdiction_id} = 'tfl';
return $class->$orig(%args);
};

has oauth2_url => ( is => 'ro' );
has oauth2_client_id => ( is => 'ro' );
has oauth2_client_secret => ( is => 'ro' );
has oauth2_tenant_id => ( is => 'ro' );
has ocp_apim_subscription_key => ( is => 'ro' );

has oauth2_token => (
is => 'lazy',
default => sub {
my $self = shift;

my $token = $self->memcache->get('tfl_atlas_oauth2_token');
unless ($token) {
my $url = $self->oauth2_url . $self->oauth2_tenant_id . "/oauth2/token";
my $req = POST $url, [
grant_type => "client_credentials",
client_id => $self->oauth2_client_id,
client_secret => $self->oauth2_client_secret,
];
my $response = $self->ua->request($req);
unless ($response->is_success) {
$self->logger->warn("Getting OAuth2 token failed: $url");
return;
}
my $content = decode_json($response->content);
$token = $content->{access_token};
$self->memcache->set('tfl_atlas_oauth2_token', $token, time() + $content->{expires_in} - 10);
}
return $token;
},
);

sub _headers {
my $self = shift;
return {
'Ocp-Apim-Subscription-Key' => $self->ocp_apim_subscription_key,
'Authorization' => 'Bearer ' . $self->oauth2_token,
};
}

1;
Loading