summaryrefslogtreecommitdiffstats
path: root/extensions/Bitly/lib/WebService.pm
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/Bitly/lib/WebService.pm')
-rw-r--r--extensions/Bitly/lib/WebService.pm190
1 files changed, 92 insertions, 98 deletions
diff --git a/extensions/Bitly/lib/WebService.pm b/extensions/Bitly/lib/WebService.pm
index 4b44faa0e..b900e17ee 100644
--- a/extensions/Bitly/lib/WebService.pm
+++ b/extensions/Bitly/lib/WebService.pm
@@ -27,121 +27,115 @@ use URI::Escape;
use URI::QueryParam;
use constant PUBLIC_METHODS => qw(
- list
- shorten
+ list
+ shorten
);
sub _validate_uri {
- my ($self, $params) = @_;
-
- # extract url from params
- if (!defined $params->{url}) {
- ThrowCodeError(
- 'param_required',
- { function => 'Bitly.shorten', param => 'url' }
- );
- }
- my $url = ref($params->{url}) ? $params->{url}->[0] : $params->{url};
-
- # only allow buglist queries for this bugzilla install
- my $uri = URI->new($url);
- $uri->query(undef);
- $uri->fragment(undef);
- if ($uri->as_string ne Bugzilla->localconfig->{urlbase} . 'buglist.cgi') {
- ThrowUserError('bitly_unsupported');
- }
-
- return URI->new($url);
+ my ($self, $params) = @_;
+
+ # extract url from params
+ if (!defined $params->{url}) {
+ ThrowCodeError('param_required', {function => 'Bitly.shorten', param => 'url'});
+ }
+ my $url = ref($params->{url}) ? $params->{url}->[0] : $params->{url};
+
+ # only allow buglist queries for this bugzilla install
+ my $uri = URI->new($url);
+ $uri->query(undef);
+ $uri->fragment(undef);
+ if ($uri->as_string ne Bugzilla->localconfig->{urlbase} . 'buglist.cgi') {
+ ThrowUserError('bitly_unsupported');
+ }
+
+ return URI->new($url);
}
sub shorten {
- my ($self) = shift;
- my $uri = $self->_validate_uri(@_);
+ my ($self) = shift;
+ my $uri = $self->_validate_uri(@_);
- # the list_id is user-specific, remove it
- $uri->query_param_delete('list_id');
+ # the list_id is user-specific, remove it
+ $uri->query_param_delete('list_id');
- return $self->_bitly($uri);
+ return $self->_bitly($uri);
}
sub list {
- my ($self) = shift;
- my $uri = $self->_validate_uri(@_);
-
- # map params to cgi vars, converting quicksearch if required
- my $params = $uri->query_param('quicksearch')
- ? Bugzilla::CGI->new(quicksearch($uri->query_param('quicksearch')))->Vars
- : Bugzilla::CGI->new($uri->query)->Vars;
-
- # execute the search
- my $search = Bugzilla::Search->new(
- params => $params,
- fields => ['bug_id'],
- limit => Bugzilla->params->{max_search_results},
- );
- my $data = $search->data;
-
- # form a bug_id only url, sanity check the length
- $uri = URI->new(Bugzilla->localconfig->{urlbase} . 'buglist.cgi?bug_id=' . join(',', map { $_->[0] } @$data));
- if (length($uri->as_string) > CGI_URI_LIMIT) {
- ThrowUserError('bitly_failure', { message => "Too many bugs returned by search" });
- }
-
- # shorten
- return $self->_bitly($uri);
+ my ($self) = shift;
+ my $uri = $self->_validate_uri(@_);
+
+ # map params to cgi vars, converting quicksearch if required
+ my $params
+ = $uri->query_param('quicksearch')
+ ? Bugzilla::CGI->new(quicksearch($uri->query_param('quicksearch')))->Vars
+ : Bugzilla::CGI->new($uri->query)->Vars;
+
+ # execute the search
+ my $search = Bugzilla::Search->new(
+ params => $params,
+ fields => ['bug_id'],
+ limit => Bugzilla->params->{max_search_results},
+ );
+ my $data = $search->data;
+
+ # form a bug_id only url, sanity check the length
+ $uri
+ = URI->new(Bugzilla->localconfig->{urlbase}
+ . 'buglist.cgi?bug_id='
+ . join(',', map { $_->[0] } @$data));
+ if (length($uri->as_string) > CGI_URI_LIMIT) {
+ ThrowUserError('bitly_failure',
+ {message => "Too many bugs returned by search"});
+ }
+
+ # shorten
+ return $self->_bitly($uri);
}
sub _bitly {
- my ($self, $uri) = @_;
-
- # form request url
- # http://dev.bitly.com/links.html#v3_shorten
- my $bitly_url = sprintf(
- 'https://api-ssl.bitly.com/v3/shorten?access_token=%s&longUrl=%s',
- Bugzilla->params->{bitly_token},
- uri_escape($uri->as_string)
- );
-
- # is Mozilla::CA isn't installed, skip certificate verification
- eval { require Mozilla::CA };
- $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = $@ ? 0 : 1;
-
- # request
- my $ua = LWP::UserAgent->new(agent => 'Bugzilla');
- $ua->timeout(10);
- $ua->protocols_allowed(['http', 'https']);
- if (my $proxy_url = Bugzilla->params->{proxy_url}) {
- $ua->proxy(['http', 'https'], $proxy_url);
- }
- else {
- $ua->env_proxy();
- }
- my $response = $ua->get($bitly_url);
- if ($response->is_error) {
- ThrowUserError('bitly_failure', { message => $response->message });
- }
- my $result = decode_json($response->decoded_content);
- if ($result->{status_code} != 200) {
- ThrowUserError('bitly_failure', { message => $result->{status_txt} });
- }
-
- # return just the short url
- return { url => $result->{data}->{url} };
+ my ($self, $uri) = @_;
+
+ # form request url
+ # http://dev.bitly.com/links.html#v3_shorten
+ my $bitly_url = sprintf(
+ 'https://api-ssl.bitly.com/v3/shorten?access_token=%s&longUrl=%s',
+ Bugzilla->params->{bitly_token},
+ uri_escape($uri->as_string)
+ );
+
+ # is Mozilla::CA isn't installed, skip certificate verification
+ eval { require Mozilla::CA };
+ $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = $@ ? 0 : 1;
+
+ # request
+ my $ua = LWP::UserAgent->new(agent => 'Bugzilla');
+ $ua->timeout(10);
+ $ua->protocols_allowed(['http', 'https']);
+ if (my $proxy_url = Bugzilla->params->{proxy_url}) {
+ $ua->proxy(['http', 'https'], $proxy_url);
+ }
+ else {
+ $ua->env_proxy();
+ }
+ my $response = $ua->get($bitly_url);
+ if ($response->is_error) {
+ ThrowUserError('bitly_failure', {message => $response->message});
+ }
+ my $result = decode_json($response->decoded_content);
+ if ($result->{status_code} != 200) {
+ ThrowUserError('bitly_failure', {message => $result->{status_txt}});
+ }
+
+ # return just the short url
+ return {url => $result->{data}->{url}};
}
sub rest_resources {
- return [
- qr{^/bitly/shorten$}, {
- GET => {
- method => 'shorten',
- },
- },
- qr{^/bitly/list$}, {
- GET => {
- method => 'list',
- },
- },
- ]
+ return [
+ qr{^/bitly/shorten$}, {GET => {method => 'shorten',},},
+ qr{^/bitly/list$}, {GET => {method => 'list',},},
+ ];
}
1;