lib/Net/Twitch/API.pm

filter: Statementstmt 66.6% 28 / 42 Branchbran 20.0% 2 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 56.5% 39 / 69 CC 8 SCAR 24.0
CC
8
Cov
54%
_responseParser
30.0
getUsers
6.0
new
3.0
CRAP 11.1
↑ Summary
1 package Net::Twitch::API;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use WebService::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base 'WebService::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.11';
12  
13 # overide our base modules WebService::Simple _agent() method with our own:
14 1 ▶sub _agent { "libwww-perl/$LWP::VERSION+WebService::Simple/". $WebService::Simple::VERSION ."+". __PACKAGE__ .'/'.$VERSION }
Subroutine _agent: called CC=1 SCAR=0.0
15  
16 # similarly for the WebService::Simple built-in config(), which we overwrite like this:
17 1 __PACKAGE__->config(
18 base_url => 'https://api.twitch.tv/helix/', # needs a trailing slash
19 response_parser => 'JSON',
20 );
21  
22 # we wrap the underlying base's new() method to attach credentials (which are normally discarded)
23 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
24 1 my $class = shift;
25 1 my %args = @_;
26  
27 1 ▶ die "Net::Twitch::API: new: no access_token provided!" unless $args{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"access_token"} 0 1 1
28 1 ▶ die "Net::Twitch::API: new: no client_id provided!" unless $args{client_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"client_id"} 0 1 1
29  
30 1 $args{croak} = 0; # WebService::Simple by default croaks() - which we do not want
31  
32 1 my $self = $class->SUPER::new(%args);
33  
34 1 $self->{access_token} = $args{access_token};
35 1 $self->{client_id} = $args{client_id};
36  
37 1 return $self;
38 }
39  
40 0 ▶sub getUsers {
Subroutine getUsers: not called CC=2 SCAR=17.9
Pod documented
41 0 my $self = shift;
42 0 ▶ my $params = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
43  
44 my $response = $self->get('/users', $params,
45 'Authorization' => 'Bearer '. $self->{access_token},
46 'Client-Id' => $self->{client_id},
47 0 );
48  
49 0 return $self->_responseParser($response,'getUsers');
50 }
51  
52 0 ▶sub _responseParser {
Subroutine _responseParser: not called CC=5 SCAR=34.0
53 0 my $self = shift;
54 0 my $response = shift;
55 0 ▶ my $methodName = shift || '<empty methodName>';
Condition0%0/2
shift() || '<empty methodName>'
l!l
00
MC/DC0%0/1
shift() || '<empty methodName>'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
56  
57 ## WebService::Simple doesn't wrap error responses, so handle this case here
58 0 ▶ unless($response->is_success){
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
59 0 ▶ unless($response->content_length() ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->content_length) 0 0 0
60 0 return { error => $methodName ." request failed!", status => $response->code, message => $response->message, response => $response };
61 }
62  
63 0 $response = WebService::Simple::Response->new_from_response(
64 response => $response,
65 parser => $self->response_parser
66 );
67 }
68  
69 0 my $result = {};
70  
71 0 ▶ $result = $response->parse_response() if $response->content_length();
Branch: 1 branch
BranchTrueFalseTotal
if $response->content_length 0 0 0
72  
73 0 return $result;
74 }
75  
76 =pod
77  
78 =head1 NAME
79  
80 Net::Twitch::API - Helper methods for Twitch's "new" helix API
81  
82 =head1 SYNOPSIS
83  
84 use Net::Twitch::API;
85  
86 my $api = Net::Twitch::API->new(
87 access_token => 'your-token',
88 client_id => 'your-id',
89 debug => 1,
90 );
91  
92 my $response = $api->getUsers({ login => 'twitchdev' });
93  
94 =head1 DESCRIPTION
95  
96 This module provides methods and helper wrappers to work with what Twitch "new" helix API. The I<new> API is
97 prefixed with the I<helix> codename/namespace and the successor of the old I<kraken> API which was decommissioned
98 on February 28, 2022. A little more about that on dev.twitch.tv L<"legacy v5 integrations" migration guide|https://dev.twitch.tv/docs/api/migration>.
99  
100 Using this module to issue requests against Twitch's API requires you to register your "application" with Twitch
101 first. Authentication then is either faciliated via L<OAuth 2.0|https://dev.twitch.tv/docs/authentication/getting-tokens-oauth>
102 or L<OpenID Connect|https://dev.twitch.tv/docs/authentication/getting-tokens-oidc>. We here use the OAuth2 scheme.
103  
104 Twitch uses several types of auth tokens. Use the twitch CLI client to obtain an "app access token". This type of
105 token enables your app to make secure API requests that are not on behalf of a specific user. App access tokens are
106 meant only for server-to-server API requests and should never be included in client code. Normally, such tokens
107 would be programmatically refreshed at arbitrary intervals according to OAuth2 RFC but on Twitch app access tokens
108 are valid for 60 days and cannot be refreshed.
109  
110 This module uses Yosukebe's excellent L<WebService::Simple> as base calss. So look there for addditonal documentation.
111 You might also note that Yosukebe himself recently switched over to the newer L<WebService::Client>, but that's a
112 Moo based module.
113  
114 =head1 FUNCTIONS
115  
116 =head2 new()
117  
118 Calls underlying WebService::Simple's new(), with additional checks and defaults for Twitch.
119  
120 You must provide your I<access_token> and I<client_id>.
121  
122 If I<debug> is set, the request URL will be dumped via warn() on get or post method calls.
123  
124 WebService::Simple by default croaks (dies) on a failed request. This module returns on error and success with a
125 reference to a hash containing received data. The hash key I<error> is defined on unsuccessful requests.
126  
127 If you supply a Cache object to new(), each request is prepended by a cache look-up. Refer to L<WebService::Simple>
128 for an example.
129  
130 =head2 getUsers()
131  
132 Expects a hashref. Users can be looked up either via their I<login> name (username / nickname) or via their numeric
133 user I<id>. Twitch allows to ask for multiple names in one request. Current limit is 100. Use an arrayref of values
134 instead of a scalar then.
135  
136 Returns a hashref with hash-key I<data> holding a reference to an array of users.
137  
138 =head1 EXPORT
139  
140 Nothing by default.
141  
142 =head1 CAVEATS
143  
144 Note that Net::Twitch::API is a WIP module. Things are incomplete or may change without notice.
145  
146 =head1 SEE ALSO
147  
148 Official Twitch documents:
149  
150 =over
151  
152 =item *
153  
154 L<Getting Started|https://dev.twitch.tv/docs/api> walks you through basic setup of your app and helps with
155 a first request.
156  
157 =item *
158  
159 L<Reference|https://dev.twitch.tv/docs/api/guide> gives an overview of core principles like pagination and rate
160 limits.
161  
162 =item *
163  
164 L<Reference|https://dev.twitch.tv/docs/api/reference> si the canonical Twitch API endpoints reference. A little
165 more an be found in the L<api docs|https://github.com/twitchdev/twitch-cli/blob/main/docs/api.md> for the twitch-cli command-line client.
166  
167 =back
168  
169 Within the Perl universe, Twitch related code can be found in outdated modules L<App::Twitch> and L<Net::Twitch::Oauth2>
170 and Corion's non-API helper module L<WWW::Twitch|https://github.com/Corion/WWW-Twitch>.
171  
172 =head1 AUTHOR
173  
174 Clipland GmbH L<https://www.clipland.com/>
175  
176 This module was developed for L<"Sendung verpasst?"|https://mediatheksuche.de/> I<video> search engine L<MediathekSuche.de|https://mediatheksuche.de/>.
177  
178 =head1 COPYRIGHT & LICENSE
179  
180 Copyright 2022 Clipland GmbH. All rights reserved.
181  
182 This library is free software, dual-licensed under L<GPLv3|http://www.gnu.org/licenses/gpl>/L<AL2|http://opensource.org/licenses/Artistic-2.0>.
183 You can redistribute it and/or modify it under the same terms as Perl itself.
184  
185 =cut
186  
187 1 1;
↑ Summary