| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | =encoding utf-8 | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | EveOnline::SSO::Client - base class for any Eve Online ESI API calls | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | use EveOnline::SSO::Client; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | my $client = EveOnline::SSO::Client->new(token => '************', x_user_agent => 'Pilot Name '); | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | $api->get(['characters', 90922771, 'location'] ); | 
| 14 |  |  |  |  |  |  | # url converting to https://esi.tech.ccp.is/latest/characters/90922771/location/?datasource=tranquility | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | EveOnline::SSO::Client is base class for any EveOnline ESI API | 
| 19 |  |  |  |  |  |  | Contains get, post, put, delete methods for calls to any APIs. | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =cut | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | package EveOnline::SSO::Client; | 
| 24 | 1 |  |  | 1 |  | 650 | use 5.008001; | 
|  | 1 |  |  |  |  | 5 |  | 
| 25 | 1 |  |  | 1 |  | 499 | use utf8; | 
|  | 1 |  |  |  |  | 12 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 26 | 1 |  |  | 1 |  | 388 | use Modern::Perl; | 
|  | 1 |  |  |  |  | 8094 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 1 |  |  | 1 |  | 705 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 21388 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 29 | 1 |  |  | 1 |  | 6 | use Storable qw/ dclone /; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 58 |  | 
| 30 | 1 |  |  | 1 |  | 5 | use JSON::XS; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 31 | 1 |  |  | 1 |  | 451 | use Moo; | 
|  | 1 |  |  |  |  | 8829 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has 'ua' => ( | 
| 34 |  |  |  |  |  |  | is => 'ro', | 
| 35 |  |  |  |  |  |  | default => sub { | 
| 36 |  |  |  |  |  |  | my $ua = LWP::UserAgent->new(); | 
| 37 |  |  |  |  |  |  | $ua->agent( 'EveOnline::SSO::Client Perl API Client' ); | 
| 38 |  |  |  |  |  |  | $ua->timeout( 120 ); | 
| 39 |  |  |  |  |  |  | return $ua; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | ); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | has 'token' => ( | 
| 44 |  |  |  |  |  |  | is => 'rw', | 
| 45 |  |  |  |  |  |  | required => 1, | 
| 46 |  |  |  |  |  |  | ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | has 'endpoint' => ( | 
| 49 |  |  |  |  |  |  | is => 'rw', | 
| 50 |  |  |  |  |  |  | required => 1, | 
| 51 |  |  |  |  |  |  | default  => 'https://esi.tech.ccp.is/latest/', | 
| 52 |  |  |  |  |  |  | ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | has 'datasource' => ( | 
| 55 |  |  |  |  |  |  | is => 'rw', | 
| 56 |  |  |  |  |  |  | required => 1, | 
| 57 |  |  |  |  |  |  | default => 'tranquility', | 
| 58 |  |  |  |  |  |  | ); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | has 'x_user_agent' => ( | 
| 61 |  |  |  |  |  |  | is => 'rw', | 
| 62 |  |  |  |  |  |  | required => 1, | 
| 63 |  |  |  |  |  |  | ); | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | has 'demo' => ( | 
| 66 |  |  |  |  |  |  | is => 'rw' | 
| 67 |  |  |  |  |  |  | ); | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | =head1 CONSTRUCTOR | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =over | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =item B | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | Require arguments: token, x_user_agent | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | my $client = EveOnline::SSO::Client->new( | 
| 78 |  |  |  |  |  |  | token        => 'lPhJ_DLk345334532yfssfdJgFsnKI9rR4EZpcQnJ2', | 
| 79 |  |  |  |  |  |  | x_user_agent => 'Pilot Name ', | 
| 80 |  |  |  |  |  |  | endpoint     => 'https://esi.tech.ccp.is/latest/', # optional | 
| 81 |  |  |  |  |  |  | datasource   => 'tranquility',                     # optional | 
| 82 |  |  |  |  |  |  | ); | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | For module based on EveOnline::SSO::Client | 
| 85 |  |  |  |  |  |  | you can override atributes: | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | extends 'EveOnline::SSO::Client'; | 
| 88 |  |  |  |  |  |  | has '+endpoint' => ( | 
| 89 |  |  |  |  |  |  | is      => 'rw', | 
| 90 |  |  |  |  |  |  | default => 'https://esi.tech.ccp.is/dev/', | 
| 91 |  |  |  |  |  |  | ); | 
| 92 |  |  |  |  |  |  | has '+datasource' => ( | 
| 93 |  |  |  |  |  |  | is => 'rw', | 
| 94 |  |  |  |  |  |  | default => 'singularity', | 
| 95 |  |  |  |  |  |  | ); | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =back | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =head1 METHODS | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =over | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =item B | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | Send GET request. | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | $client->get(['array', 'of', 'path', 'blocks'], | 
| 108 |  |  |  |  |  |  | { | 
| 109 |  |  |  |  |  |  | # query string hash ref. Optional | 
| 110 |  |  |  |  |  |  | }); | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | $client->get(['characters', $character_id, 'location']); | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | # or | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | $api->get(['characters', 90922771, 'contacts'], { page => 1 } ); | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | # or full link | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | $client->get('https://esi.tech.ccp.is/latest/incursions/'); | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | You can add any params for query hashref, it should be added to query_string | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =cut | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | sub get { | 
| 127 | 0 |  |  | 0 | 1 | 0 | my ($self, $url, $query) = @_; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  |  |  | 0 | return $self->request( 'get', $url, $query ); | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =item B | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | Send POST request. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | $client->post(['array', 'of', 'path', 'blocks'], | 
| 137 |  |  |  |  |  |  | { | 
| 138 |  |  |  |  |  |  | # query string hash ref. Can be empty | 
| 139 |  |  |  |  |  |  | }, | 
| 140 |  |  |  |  |  |  | [ {}, [] ]  # Body structure. Converting to json. Optional | 
| 141 |  |  |  |  |  |  | ); | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | $client->post(['universe', 'names'], {}, [90922771,188956223]); | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =cut | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | sub post { | 
| 148 | 0 |  |  | 0 | 1 | 0 | my ($self, $url, $query, $body) = @_; | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 0 |  |  |  |  | 0 | return $self->request( 'post', $url, $query, $body ); | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =item B | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | Send PUT request. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | $client->put(['characters', 90922771, 'contacts'], { standing => 5, watched => 'false' }, [188956223] ); | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =cut | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | sub put { | 
| 162 | 0 |  |  | 0 | 1 | 0 | my ($self, $url, $query, $body) = @_; | 
| 163 |  |  |  |  |  |  |  | 
| 164 | 0 |  |  |  |  | 0 | return $self->request( 'put', $url, $query, $body ); | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =item B | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | Send DELETE request. | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | $client->delete(['characters', 90922771, 'contacts'], {}, [188956223] ); | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =cut | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | sub delete { | 
| 176 | 0 |  |  | 0 | 1 | 0 | my ($self, $url, $query, $body) = @_; | 
| 177 |  |  |  |  |  |  |  | 
| 178 | 0 |  |  |  |  | 0 | return $self->request( 'delete', $url, $query, $body ); | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | sub request { | 
| 182 | 0 |  |  | 0 | 0 | 0 | my ( $self, $method, $url, $query, $body ) = @_; | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 0 |  |  |  |  | 0 | my $req = HTTP::Request->new( uc $method => $self->make_url( $url, $query ) ); | 
| 185 |  |  |  |  |  |  |  | 
| 186 | 0 |  |  |  |  | 0 | $self->prepare_request( $req, $body ); | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 0 |  |  |  |  | 0 | my $answer = $self->ua->request( $req ); | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 0 | 0 |  |  |  | 0 | return $answer->code unless $answer->content; | 
| 191 |  |  |  |  |  |  |  | 
| 192 | 0 |  |  |  |  | 0 | return $self->parse_response( $answer->content ); | 
| 193 |  |  |  |  |  |  | } | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | sub make_url { | 
| 196 | 4 |  |  | 4 | 0 | 21 | my ( $self, $resource, $params ) = @_; | 
| 197 |  |  |  |  |  |  |  | 
| 198 | 4 | 100 |  |  |  | 20 | return $resource if $resource =~ /^http/i; | 
| 199 |  |  |  |  |  |  |  | 
| 200 | 3 |  |  |  |  | 4 | my $url = ''; | 
| 201 |  |  |  |  |  |  |  | 
| 202 | 3 |  |  |  |  | 22 | $url = $self->endpoint; | 
| 203 | 3 |  |  |  |  | 11 | $url .= join '/', @$resource; | 
| 204 | 3 |  |  |  |  | 16 | $url .= '/'; | 
| 205 |  |  |  |  |  |  |  | 
| 206 | 3 |  |  |  |  | 15 | my $uri = URI->new( $url ); | 
| 207 | 3 | 100 |  |  |  | 6699 | $uri->query_form( %{$params || {}}, datasource => $self->datasource ); | 
|  | 3 |  |  |  |  | 26 |  | 
| 208 | 3 |  |  |  |  | 245 | $url = $uri->as_string; | 
| 209 |  |  |  |  |  |  |  | 
| 210 | 3 |  |  |  |  | 32 | return $url; | 
| 211 |  |  |  |  |  |  | } | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | sub prepare_request { | 
| 214 | 2 |  |  | 2 | 0 | 2426 | my ( $self, $request, $body_param ) = @_; | 
| 215 |  |  |  |  |  |  |  | 
| 216 | 2 |  |  |  |  | 4 | my $body = ''; | 
| 217 | 2 | 100 |  |  |  | 6 | if ( $body_param ) { | 
| 218 | 1 |  |  |  |  | 99 | my $params = dclone( $body_param ); | 
| 219 |  |  |  |  |  |  |  | 
| 220 | 1 |  |  |  |  | 13 | state $json = JSON::XS->new->utf8; | 
| 221 |  |  |  |  |  |  |  | 
| 222 | 1 |  |  |  |  | 11 | $body = $json->encode( $params ); | 
| 223 |  |  |  |  |  |  |  | 
| 224 | 1 |  |  |  |  | 9 | $request->content( $body ); | 
| 225 |  |  |  |  |  |  | } | 
| 226 | 2 |  |  |  |  | 38 | $request->header( 'content-type'   => 'application/json; charset=UTF-8' ); | 
| 227 | 2 |  |  |  |  | 118 | $request->header( 'content-length' => length( $body ) ); | 
| 228 | 2 |  |  |  |  | 82 | $request->header( 'X-User-Agent'   => $self->x_user_agent ); | 
| 229 | 2 |  |  |  |  | 92 | $request->header( 'Authorization'  => 'Bearer ' . $self->token ); | 
| 230 |  |  |  |  |  |  |  | 
| 231 | 2 |  |  |  |  | 77 | return 1; | 
| 232 |  |  |  |  |  |  | } | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | sub parse_response { | 
| 235 | 1 |  |  | 1 | 0 | 1202 | my ( $self, $response ) = @_; | 
| 236 |  |  |  |  |  |  |  | 
| 237 | 1 |  |  |  |  | 6 | my $json = JSON::XS::decode_json( $response ); | 
| 238 |  |  |  |  |  |  |  | 
| 239 | 1 |  |  |  |  | 13 | return $json; | 
| 240 |  |  |  |  |  |  | } | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | 1; | 
| 244 |  |  |  |  |  |  | __END__ |