porting to internal apis

This commit is contained in:
Jeremy Kauffman 2017-02-21 21:44:25 -05:00
parent 4a94d376d5
commit 32ca0d7f94
5 changed files with 37 additions and 106 deletions

View file

@ -40,10 +40,10 @@ class DeveloperActions extends Actions
return ['developer/quickstart', $viewParams];
}
public static function prepareQuickstartOnePartial(array $vars)
public static function prepareQuickstartHomePartial(array $vars)
{
return $vars + [
'version' => '0.8.4'
'usdValue' => static::DEVELOPER_REWARD * LBRY::getLBCtoUSDRate()
];
}
@ -102,7 +102,8 @@ class DeveloperActions extends Actions
];
return Controller::redirect('https://github.com/login/oauth/authorize?' . http_build_query($githubParams));
}
return Controller::redirect('/developer-program');
return Controller::redirect(Request::getReferrer('/quickstart/credits'));
}
public static function executeDeveloperProgramGithubCallback()
@ -120,109 +121,17 @@ class DeveloperActions extends Actions
}
else
{
$authResponseData = Curl::post('https://github.com/login/oauth/access_token', [
'code' => $code,
'client_id' => Config::get('github_developer_credits_client_id'),
'client_secret' => Config::get('github_developer_credits_client_secret')
$newUserUrl = LBRY::getApiUrl('/user/new_developer');
$lbryApiResponseData = Curl::post($newUserUrl, [
'github_code' => $code
], [
'headers' => ['Accept: application/json'],
'json_response' => true
// 'json_response' => true
]);
if (!$authResponseData || !isset($authResponseData['access_token']))
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Request to GitHub failed.');
}
elseif (isset($authResponseData['error_description']))
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'GitHub replied: ' . $authResponseData['error_description']);
}
else
{
$accessToken = $authResponseData['access_token'];
$starResponseData = Curl::put('https://api.github.com/user/starred/lbryio/lbry', [], [
'headers' => ['Authorization: token ' . $accessToken, 'Accept: application/json', 'User-Agent: lbryio', 'Content-Length: 0'],
'json_response' => true
]);
$userResponseData = Curl::get('https://api.github.com/user', [], [
'headers' => ['Authorization: token ' . $accessToken, 'Accept: application/json', 'User-Agent: lbryio'],
'json_response' => true
]);
if (!$userResponseData || !$userResponseData['created_at'] || $starResponseData !== [])
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Received unexpected response from GitHub. Perhaps authorization was revoked?');
}
elseif(date('Y-m-d', strtotime($userResponseData['created_at'])) > '2017-01-30')
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This GitHub account is too recent.');
}
else
{
$lockName = 'github_dev_credits_write';
$dataFile = ROOT_DIR . '/data/writeable/github_developer_credits';
$lock = Lock::getLock($lockName, true); // EXCLUSIVE LOCK. IF SENDING CREDITS IS SLOW, THIS COULD BLOCK USERS
$existing = is_file($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
if (isset($existing[$userResponseData['login']]) || isset($existing[$userResponseData['id']]))
{
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'You (' . $userResponseData['login'] . ') already received credits.');
}
else
{
try
{
$response =
Curl::post('http://localhost:5279/lbryapi', [
'method' => 'send_amount_to_address',
'params' => [['amount' => 250, 'address' => $walletAddress]],
], [
'json_data' => true,
'json_response' => true,
]);
}
catch (Exception $e)
{
$response = null;
echo '<pre>';
print_r($lbryApiResponseData);
die('omg');
}
$response = [true];
if ($response === [true])
{
$existing[$userResponseData['id']] = [$userResponseData['email'], $walletAddress, date('Y-m-d H:i:s'), $userResponseData['login']];
file_put_contents($dataFile, json_encode($existing));
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_SUCCESS,
'Credits on their way to address ' . $walletAddress . ' for GitHub user ' . $userResponseData['login'] . '. It may take up to a minute for them to arrive.');
}
elseif (is_array($response) && (isset($response['faultString']) && stripos($response['faultString'], 'InsufficientFundsError') !== false))
{
Slack::sendErrorIfProd('Github dev credits need a refill');
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR,
'Our wallet is running low on funds. Please ping jeremy@lbry.io so he can refill it, then try again.');
}
else
{
Slack::sendErrorIfProd($response === null ?
'Error connecting to LBRY API via cURL' :
'Error of unknown origin in sending Github dev credits' . var_export($response, true)
);
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Failed to send credits. This is an error on our side. Please email jeremy@lbry.io if it persists.');
Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR,
'Failed to send credits. This is an error on our side. Please email jeremy@lbry.io if it persists.');
}
}
Lock::freeLock($lock);
$lock = null;
}
}
}
return Controller::redirect('/developer-program');
return Controller::redirect(Request::getReferrer('/quickstart/credits'));
}
}

19
lib/thirdparty/LBRY.class.php vendored Normal file
View file

@ -0,0 +1,19 @@
<?php
class LBRY
{
public static function getApiUrl($endpoint)
{
return Config::get('lbry_api_server') . $endpoint;
}
public static function getLBCtoUSDRate()
{
$response = CurlWithCache::get(static::getApiUrl('/lbc/exchange_rate'), [], [
'cache' => 3600, //one hour
'json_response' => true
]);
return $response['rate'];
}
}

View file

@ -7,7 +7,7 @@
</h2>
<ul>
<li><h3>Learned the basics of the LBRY API.</h3></li>
<li><h3>Earned $5.67<sup>1</sup> for downloading a hilarious film starring David Cross.</h3></li>
<li><h3>Earned $<?php echo money_format('%i', $usdValue) ?><sup>1</sup> for downloading a hilarious film starring David Cross.</h3></li>
<li><h3>Irrevocably inscribed a piece of knowledge. Possibly of a cat.</h3>
<div class="meta">
<sup>1</sup>USD price equivalent of 250 LBC as received from the <a href="https://poloniex.com/exchange#btc_lbc" class="link-primary">Poloneix</a> exchange.

View file

@ -3,6 +3,9 @@
<a class="link-primary" href="/docs"><span class="icon-file-code-o icon-fw"></span><span class="btn-label">Documentation</span></a>
</div>
*/ ?>
<div class="spacer1">
<a href="/quickstart" class="link-primary"><span class="icon-code icon-fw"></span><span class="btn-label">Quickstart</span></a>
</div>
<div class="spacer1">
<a href="https://github.com/lbryio" class="link-primary"><span class="icon-github icon-fw"></span><span class="btn-label">{{social.github}}</span></a>
</div>