Skip to content

Commit

Permalink
added api caching to fix rate limiting problem
Browse files Browse the repository at this point in the history
added field limiting to vimeo checks

testing api cache with vimeo
  • Loading branch information
atarisafari committed Oct 11, 2019
1 parent ab8ec8f commit dbd4f3d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"description": "needed to use the Heroku configuration",
"value": "true"
},
"USE_API_CACHING": {
"description": "Flag to indicate whether or not to use the api caching feature",
"required": false
},
"CANVAS_NAV_ITEM_NAME" : {
"description": "The text displayed in the canvas navigation menu to launch this app",
"value": "UDOIT"
Expand Down
3 changes: 3 additions & 0 deletions config/herokuConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
/* Google Analytics Tracking Code */
define('GA_TRACKING_CODE', getenv('GA_TRACKING_CODE')?:'');

/* Flag for API Caching */
define('USE_API_CACHING', getenv('USE_API_CACHING')?:'true');

// Fix some issues caused by the heroku load balancer
// The OAUTH signature verification doesn't know it's using https w/o this
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
Expand Down
3 changes: 3 additions & 0 deletions config/localConfig.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
/* Google Analytics Tracking Code */
define('GA_TRACKING_CODE', '');

/* Flag for API Caching */
define('USE_API_CACHING', '');

/* Database Config */
$db_type = 'mysql'; // 'mysql' or 'pgsql'
$db_host = ''; // localhost or some other domain/ip
Expand Down
3 changes: 3 additions & 0 deletions config/localConfig.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
/* Course Language */
define('COURSE_LANGUAGE', 'TEST_COURSE_LANGUAGE');

/* Flag for API Caching */
define('USE_API_CACHING', 'false');

/* Database Config */
$db_type = 'test'; // 'mysql' or 'pgsql'
$db_host = ''; // localhost or ip
Expand Down
24 changes: 24 additions & 0 deletions lib/UdoitUtils.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php session_start();
use Httpful\Request;
/**
* Copyright (C) 2014 University of Central Florida, created by Jacob Bates, Eric Colon, Fenel Joseph, and Emily Sachs.
*
Expand Down Expand Up @@ -309,6 +310,29 @@ public function getCourseLocale($api_key, $course_id)
return false;
}

public function checkApiCache($api_url, $video_url, $api_key = NULL)
{
global $logger;
// Check if session var
// If so, grab response object from session var aka 'cache'
if(isset($_SESSION[$video_url]) && constant('USE_API_CACHING') != 'false') {
$response = $_SESSION[$video_url];
$logger->addInfo("Cached api response used");
} else {
// Else, make api call and cache response in a session var
if($api_key == NULL) {
$response = Request::get($api_url)->send();
} else {
// Vimeo requires the key be added as a header
$response = Request::get($api_url)->addHeader('Authorization', "Bearer $api_key")->send();
}
$_SESSION[$video_url] = $response;
}

// Return response
return $response;
}

protected function curlOauthToken($base_url, $post_data)
{
// @TODO - why not use Httpful here?
Expand Down
4 changes: 2 additions & 2 deletions lib/quail/quail/common/services/media/vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function captionsMissing($link_url)

if( $vimeo_id = $this->isVimeoVideo($link_url) ) {
$url = $url.$vimeo_id.'/texttracks';
$response = Request::get($url)->addHeader('Authorization', "Bearer $api_key")->send();
$response = UdoitUtils::instance()->checkApiCache($url, $link_url, $api_key);

// Response header code is used to determine if video exists, doesn't exist, or is unaccessible
// 400 means a video is private, 404 means a video doesn't exist, and 200 means the video exists
Expand Down Expand Up @@ -76,7 +76,7 @@ function captionsLanguage($link_url, $course_locale)

if( $vimeo_id = $this->isVimeoVideo($link_url) ) {
$url = $url.$vimeo_id.'/texttracks';
$response = Request::get($url)->addHeader('Authorization', "Bearer $api_key")->send();
$response = UdoitUtils::instance()->checkApiCache($url, $link_url, $api_key);

// Response header code is used to determine if video exists, doesn't exist, or is unaccessible
// 400 means a video is private, 404 means a video doesn't exist, and 200 means the video exists
Expand Down
8 changes: 3 additions & 5 deletions lib/quail/quail/common/services/media/youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class youtubeService extends mediaService
/**
* @var string The service point to request caption data from YouTube
*/
var $search_url = 'https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId=FcnrUf35LmQ&fields=items(snippet(trackKind,language))&key=';
var $search_url = 'https://www.googleapis.com/youtube/v3/captions?part=snippet&fields=items(snippet(trackKind,language))&videoId=';

/**
* Checks to see if a video is missing caption information in YouTube
Expand All @@ -49,8 +49,7 @@ function captionsMissing($link_url)

if( $youtube_id = $this->isYouTubeVideo($link_url) ) {
$url = $url.$youtube_id.'&key='.$api_key;
$response = Request::get($url)->send();
$logger->addError("Making request to youtube");
$response = UdoitUtils::instance()->checkApiCache($url, $link_url);

// If the video was pulled due to copyright violations, is unlisted, or is unavailable, the reponse header will be 404
if( $response->code === 404 ) {
Expand Down Expand Up @@ -103,8 +102,7 @@ function captionsLanguage($link_url, $course_locale)

if( $youtube_id = $this->isYouTubeVideo($link_url) ) {
$url = $url.$youtube_id.'&key='.$api_key;
$response = Request::get($url)->send();
$logger->addError("Making request to youtube");
$response = UdoitUtils::instance()->checkApiCache($url, $link_url);

// If the video was pulled due to copyright violations, is unlisted, or is unavailable, the response header will be 404
if( $response->code == 404) {
Expand Down
3 changes: 1 addition & 2 deletions public/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
$course_locale_raw = UdoitUtils::instance()->getCourseLocale($api_key, $course_id);

if (false === $course_locale_raw ||
!ctype_space($course_locale_raw) ||
ctype_space($course_locale_raw) ||
'' == $course_locale_raw) {
$course_locale = 'en';
$logger->addWarning('No course locale received, defaulting to en');
Expand All @@ -59,7 +59,6 @@
$logger->addInfo('Course Locale set to '.$course_locale);
}


// No content selected
if ('none' === $content) {
$logger->addInfo('no content selected');
Expand Down

0 comments on commit dbd4f3d

Please sign in to comment.