SSO/Client.pm

filter: Statementstmt 80.2% 57 / 71 Branchbran 66.6% 4 / 6 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 68.7% 11 / 16 Podpod 50.0% 4 / 8 totaltotal 75.9% 79 / 104 CC 5 SCAR 7.4
CC
5
Cov
80%
request
6.0
make_url
3.0
get
2.0
CRAP 2.1
« SSO.pm ↑ Summary
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 <email@gmail.com>');
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 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Storable qw/ dclone /;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 has 'ua' => (
34 is => 'ro',
35 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 my $ua = LWP::UserAgent->new();
37 1 $ua->agent( 'EveOnline::SSO::Client Perl API Client' );
38 1 $ua->timeout( 120 );
39 1 return $ua;
40 }
41 1 );
42  
43 1 has 'token' => (
44 is => 'rw',
45 required => 1,
46 );
47  
48 1 has 'endpoint' => (
49 is => 'rw',
50 required => 1,
51 default => 'https://esi.tech.ccp.is/latest/',
52 );
53  
54 1 has 'datasource' => (
55 is => 'rw',
56 required => 1,
57 default => 'tranquility',
58 );
59  
60 1 has 'x_user_agent' => (
61 is => 'rw',
62 required => 1,
63 );
64  
65 1 has 'demo' => (
66 is => 'rw'
67 );
68  
69 =head1 CONSTRUCTOR
70  
71 =over
72  
73 =item B<new()>
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 <email@gmail.com>',
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<get()>
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 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
127 0 my ($self, $url, $query) = @_;
128  
129 0 return $self->request( 'get', $url, $query );
130 }
131  
132 =item B<post()>
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 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
148 0 my ($self, $url, $query, $body) = @_;
149  
150 0 return $self->request( 'post', $url, $query, $body );
151 }
152  
153 =item B<put()>
154  
155 Send PUT request.
156  
157 $client->put(['characters', 90922771, 'contacts'], { standing => 5, watched => 'false' }, [188956223] );
158  
159 =cut
160  
161 0 ▶sub put {
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
162 0 my ($self, $url, $query, $body) = @_;
163  
164 0 return $self->request( 'put', $url, $query, $body );
165 }
166  
167 =item B<delete()>
168  
169 Send DELETE request.
170  
171 $client->delete(['characters', 90922771, 'contacts'], {}, [188956223] );
172  
173 =cut
174  
175 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
176 0 my ($self, $url, $query, $body) = @_;
177  
178 0 return $self->request( 'delete', $url, $query, $body );
179 }
180  
181 0 ▶sub request {
Subroutine request: not called CC=2 SCAR=17.9
Pod undocumented
182 0 my ( $self, $method, $url, $query, $body ) = @_;
183  
184 0 my $req = HTTP::Request->new( uc $method => $self->make_url( $url, $query ) );
185  
186 0 $self->prepare_request( $req, $body );
187  
188 0 my $answer = $self->ua->request( $req );
189  
190 0 ▶ return $answer->code unless $answer->content;
Branch: 1 branch
BranchTrueFalseTotal
unless $answer->content 0 0 0
191  
192 0 return $self->parse_response( $answer->content );
193 }
194  
195 4 ▶sub make_url {
Subroutine make_url: called CC=3 SCAR=11.0
Pod undocumented
196 4 my ( $self, $resource, $params ) = @_;
197  
198 4 ▶ return $resource if $resource =~ /^http/i;
Branch: 1 branch
BranchTrueFalseTotal
if $resource =~ /^http/iu 1 3 4
199  
200 3 my $url = '';
201  
202 3 $url = $self->endpoint;
203 3 $url .= join '/', @$resource;
204 3 $url .= '/';
205  
206 3 my $uri = URI->new( $url );
207 3 ▶ $uri->query_form( %{$params || {}}, datasource => $self->datasource );
Condition100%2/2
$params || {}
l!l
12
MC/DC100%1/1
$params || {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
208 3 $url = $uri->as_string;
209  
210 3 return $url;
211 }
212  
213 2 ▶sub prepare_request {
Subroutine prepare_request: called CC=2 SCAR=6.9
Pod undocumented
214 2 my ( $self, $request, $body_param ) = @_;
215  
216 2 my $body = '';
217 2 ▶ if ( $body_param ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_param) 1 1 2
218 1 my $params = dclone( $body_param );
219  
220 1 state $json = JSON::XS->new->utf8;
221  
222 1 $body = $json->encode( $params );
223  
224 1 $request->content( $body );
225 }
226 2 $request->header( 'content-type' => 'application/json; charset=UTF-8' );
227 2 $request->header( 'content-length' => length( $body ) );
228 2 $request->header( 'X-User-Agent' => $self->x_user_agent );
229 2 $request->header( 'Authorization' => 'Bearer ' . $self->token );
230  
231 2 return 1;
232 }
233  
234 1 ▶sub parse_response {
Subroutine parse_response: called CC=1 SCAR=0.0
Pod undocumented
235 1 my ( $self, $response ) = @_;
236  
237 1 my $json = JSON::XS::decode_json( $response );
238  
239 1 return $json;
240 }
241  
242  
243 1 1;
244 __END__
« SSO.pm ↑ Summary