| 1 | use 5.010; # mro | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | package Net::Travis::API::UA; | ||
| 7 | $Net::Travis::API::UA::VERSION = '0.001001'; | ||
| 8 | # ABSTRACT: Travis Specific User Agent that handles authorization | ||
| 9 | |||
| 10 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | use Moo qw( extends has ); | ||
| 25 | use mro; | ||
| 26 | extends 'HTTP::Tiny'; | ||
| 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 | use Carp qw(croak); | ||
| 62 | |||
| 63 | sub _package_version { | ||
| 64 | my $vfield = join q[::], __PACKAGE__, q[VERSION]; | ||
| 65 | ## no critic (BuiltinFunctions::ProhibitStringyEval,Lax::ProhibitStringyEval::ExceptForRequire) | ||
| 66 | if ( eval "defined \$$vfield" ) { | ||
| 67 | ## no critic(ErrorHandling::RequireCheckingReturnValueOfEval) | ||
| 68 | return eval "\$$vfield"; | ||
| 69 | } | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _ua_version { | ||
| 74 | my ( $self, ) = @_; | ||
| 75 | return 0 if not defined $self->_package_version; | ||
| 76 | return $self->_package_version; | ||
| 77 | } | ||
| 78 | sub _ua_name { return __PACKAGE__ } | ||
| 79 | |||
| 80 | sub _ua_flags { | ||
| 81 | my ( $self, ) = @_; | ||
| 82 | my $flags = ['cpan']; | ||
| 83 | push @{$flags}, 'dev' if not defined $self->_package_version; | ||
| 84 | return $flags; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _agent { | ||
| 88 | my ($self) = @_; | ||
| 89 | my $own_ua = sprintf '%s/%s (%s)', $self->_ua_name, $self->_ua_version, ( join q{; }, @{ $self->_ua_flags } ); | ||
| 90 | my @agent_strings = ($own_ua); | ||
| 91 | for my $class ( @{ mro::get_linear_isa(__PACKAGE__) } ) { | ||
| 92 | next unless $class->can('_agent'); | ||
| 93 | next if $class eq __PACKAGE__; | ||
| 94 | push @agent_strings, $class->_agent(); | ||
| 95 | } | ||
| 96 | return join q[ ], @agent_strings; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | has 'http_prefix' => ( | ||
| 110 | is => ro =>, | ||
| 111 | lazy => 1, | ||
| 112 | builder => sub { return 'https://api.travis-ci.org' }, | ||
| 113 | ); | ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | has 'authtokens' => ( | ||
| 130 | is => rw =>, | ||
| 131 | predicate => 'has_authtokens', | ||
| 132 | ); | ||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | has 'json' => ( | ||
| 143 | is => ro =>, | ||
| 144 | lazy => 1, | ||
| 145 | builder => sub { | ||
| 146 | require JSON; | ||
| 147 | return JSON->new(); | ||
| 148 | }, | ||
| 149 | ); | ||
| 150 | |||
| 151 | sub _add_auth_tokens { | ||
| 152 | my ( $self, $options ) = @_; | ||
| 153 | $options = {} if not defined $options; | ||
| 154 | if ( exists $options->{travis_no_auth} ) { | ||
| 155 | delete $options->{travis_no_auth}; | ||
| 156 | return $options; | ||
| 157 | } | ||
| 158 | return $options unless $self->has_authtokens; | ||
| 159 | return $options if exists $options->{headers} and exists $options->{headers}->{Authorization}; | ||
| 160 | $options->{headers}->{Authorization} = [ map { 'token ' . $_ } @{ $self->authtokens } ]; | ||
| 161 | return $options; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub _expand_uri { | ||
| 165 | my ( $self, $uri ) = @_; | ||
| 166 | require URI; | ||
| 167 | return URI->new_abs( $uri, $self->http_prefix ); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub FOREIGNBUILDARGS { | ||
| 171 | my ( undef, @elems ) = @_; | ||
| 172 | my $hash; | ||
| 173 | if ( 1 == @elems and ref $elems[0] ) { | ||
| 174 | $hash = $elems[0]; | ||
| 175 | } | ||
| 176 | elsif ( @elems % 2 == 0 ) { | ||
| 177 | $hash = {@elems}; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | croak q[Uneven number of parameters or non-ref passed]; | ||
| 181 | } | ||
| 182 | my %not = map { $_ => 1 } qw( http_prefix authtokens ); | ||
| 183 | my %out; | ||
| 184 | for my $key ( keys %{$hash} ) { | ||
| 185 | next if $not{$key}; | ||
| 186 | $out{$key} = $hash->{$key}; | ||
| 187 | } | ||
| 188 | return %out; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | sub request { | ||
| 201 | my ( $self, $method, $uri, $opts ) = @_; | ||
| 202 | my $result = $self->SUPER::request( $method, $self->_expand_uri($uri), $self->_add_auth_tokens($opts) ); | ||
| 203 | require Net::Travis::API::UA::Response; | ||
| 204 | return Net::Travis::API::UA::Response->new( | ||
| 205 | json => $self->json, | ||
| 206 | %{$result}, | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | no Moo; | ||
| 215 | |||
| 216 | 1; | ||
| 217 | |||
| 218 | __END__ |