Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F97172367
PhutilTwitchFuture.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jan 3, 04:05
Size
2 KB
Mime Type
text/x-php
Expires
Sun, Jan 5, 04:05 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23344326
Attached To
rPHU libphutil
PhutilTwitchFuture.php
View Options
<?php
final
class
PhutilTwitchFuture
extends
FutureProxy
{
private
$future
;
private
$clientID
;
private
$accessToken
;
private
$action
;
private
$params
;
private
$method
=
'GET'
;
public
function
__construct
()
{
parent
::
__construct
(
null
);
}
public
function
setAccessToken
(
$token
)
{
$this
->
accessToken
=
$token
;
return
$this
;
}
public
function
setClientID
(
$client_id
)
{
$this
->
clientID
=
$client_id
;
return
$this
;
}
public
function
setRawTwitchQuery
(
$action
,
array
$params
=
array
())
{
$this
->
action
=
$action
;
$this
->
params
=
$params
;
return
$this
;
}
public
function
setMethod
(
$method
)
{
$this
->
method
=
$method
;
return
$this
;
}
protected
function
getProxiedFuture
()
{
if
(!
$this
->
future
)
{
$params
=
$this
->
params
;
if
(!
$this
->
action
)
{
throw
new
Exception
(
'You must setRawTwitchQuery()!'
);
}
if
(!
$this
->
accessToken
)
{
throw
new
Exception
(
'You must setAccessToken()!'
);
}
$uri
=
new
PhutilURI
(
'https://api.twitch.tv/'
);
$uri
->
setPath
(
'/kraken/'
.
ltrim
(
$this
->
action
,
'/'
));
$uri
->
setQueryParam
(
'oauth_token'
,
$this
->
accessToken
);
$future
=
new
HTTPSFuture
(
$uri
);
$future
->
setData
(
$this
->
params
);
$future
->
setMethod
(
$this
->
method
);
// NOTE: This is how the Twitch API is versioned.
$future
->
addHeader
(
'Accept'
,
'application/vnd.twitchtv.2+json'
);
// NOTE: This is required to avoid rate limiting.
$future
->
addHeader
(
'Client-ID'
,
$this
->
clientID
);
$this
->
future
=
$future
;
}
return
$this
->
future
;
}
protected
function
didReceiveResult
(
$result
)
{
list
(
$status
,
$body
,
$headers
)
=
$result
;
if
(
$status
->
isError
())
{
throw
$status
;
}
$data
=
null
;
try
{
$data
=
phutil_json_decode
(
$body
);
}
catch
(
PhutilJSONParserException
$ex
)
{
throw
new
PhutilProxyException
(
pht
(
'Expected JSON response from Twitch.'
),
$ex
);
}
if
(
idx
(
$data
,
'error'
))
{
$error
=
$data
[
'error'
];
throw
new
Exception
(
"Received error from Twitch: {$error}"
);
}
return
$data
;
}
}
Event Timeline
Log In to Comment